mirror of
https://github.com/oxen-io/session-android.git
synced 2025-01-08 16:58:04 +00:00
81 lines
2.7 KiB
Java
81 lines
2.7 KiB
Java
|
package org.thoughtcrime.securesms.stickers;
|
||
|
|
||
|
import android.app.Application;
|
||
|
import android.arch.lifecycle.LiveData;
|
||
|
import android.arch.lifecycle.MutableLiveData;
|
||
|
import android.arch.lifecycle.ViewModel;
|
||
|
import android.arch.lifecycle.ViewModelProvider;
|
||
|
import android.database.ContentObserver;
|
||
|
import android.os.Handler;
|
||
|
import android.support.annotation.NonNull;
|
||
|
|
||
|
import org.thoughtcrime.securesms.database.DatabaseContentProviders;
|
||
|
import org.thoughtcrime.securesms.stickers.StickerManagementRepository.PackResult;
|
||
|
|
||
|
final class StickerManagementViewModel extends ViewModel {
|
||
|
|
||
|
private final Application application;
|
||
|
private final StickerManagementRepository repository;
|
||
|
private final MutableLiveData<PackResult> packs;
|
||
|
private final ContentObserver observer;
|
||
|
|
||
|
private StickerManagementViewModel(@NonNull Application application, @NonNull StickerManagementRepository repository) {
|
||
|
this.application = application;
|
||
|
this.repository = repository;
|
||
|
this.packs = new MutableLiveData<>();
|
||
|
this.observer = new ContentObserver(new Handler()) {
|
||
|
@Override
|
||
|
public void onChange(boolean selfChange) {
|
||
|
repository.deleteOrphanedStickerPacks();
|
||
|
repository.getStickerPacks(packs::postValue);
|
||
|
}
|
||
|
};
|
||
|
|
||
|
application.getContentResolver().registerContentObserver(DatabaseContentProviders.StickerPack.CONTENT_URI, true, observer);
|
||
|
}
|
||
|
|
||
|
void init() {
|
||
|
repository.deleteOrphanedStickerPacks();
|
||
|
repository.fetchUnretrievedReferencePacks();
|
||
|
}
|
||
|
|
||
|
void onVisible() {
|
||
|
repository.deleteOrphanedStickerPacks();
|
||
|
}
|
||
|
|
||
|
@NonNull LiveData<PackResult> getStickerPacks() {
|
||
|
repository.getStickerPacks(packs::postValue);
|
||
|
return packs;
|
||
|
}
|
||
|
|
||
|
void onStickerPackUninstallClicked(@NonNull String packId, @NonNull String packKey) {
|
||
|
repository.uninstallStickerPack(packId, packKey);
|
||
|
}
|
||
|
|
||
|
void onStickerPackInstallClicked(@NonNull String packId, @NonNull String packKey) {
|
||
|
repository.installStickerPack(packId, packKey);
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
protected void onCleared() {
|
||
|
application.getContentResolver().unregisterContentObserver(observer);
|
||
|
}
|
||
|
|
||
|
static class Factory extends ViewModelProvider.NewInstanceFactory {
|
||
|
|
||
|
private final Application application;
|
||
|
private final StickerManagementRepository repository;
|
||
|
|
||
|
Factory(@NonNull Application application, @NonNull StickerManagementRepository repository) {
|
||
|
this.application = application;
|
||
|
this.repository = repository;
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public @NonNull <T extends ViewModel> T create(@NonNull Class<T> modelClass) {
|
||
|
//noinspection ConstantConditions
|
||
|
return modelClass.cast(new StickerManagementViewModel(application, repository));
|
||
|
}
|
||
|
}
|
||
|
}
|