mirror of
https://github.com/oxen-io/session-android.git
synced 2024-11-25 11:05:25 +00:00
91 lines
3.5 KiB
Java
91 lines
3.5 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 android.text.TextUtils;
|
||
|
|
||
|
import org.thoughtcrime.securesms.database.DatabaseContentProviders;
|
||
|
import org.thoughtcrime.securesms.stickers.StickerPackPreviewRepository.StickerManifestResult;
|
||
|
import org.whispersystems.libsignal.util.guava.Optional;
|
||
|
|
||
|
final class StickerPackPreviewViewModel extends ViewModel {
|
||
|
|
||
|
private final Application application;
|
||
|
private final StickerPackPreviewRepository previewRepository;
|
||
|
private final StickerManagementRepository managementRepository;
|
||
|
private final MutableLiveData<Optional<StickerManifestResult>> stickerManifest;
|
||
|
private final ContentObserver packObserver;
|
||
|
|
||
|
private String packId;
|
||
|
private String packKey;
|
||
|
|
||
|
private StickerPackPreviewViewModel(@NonNull Application application,
|
||
|
@NonNull StickerPackPreviewRepository previewRepository,
|
||
|
@NonNull StickerManagementRepository managementRepository)
|
||
|
{
|
||
|
this.application = application;
|
||
|
this.previewRepository = previewRepository;
|
||
|
this.managementRepository = managementRepository;
|
||
|
this.stickerManifest = new MutableLiveData<>();
|
||
|
this.packObserver = new ContentObserver(new Handler()) {
|
||
|
@Override
|
||
|
public void onChange(boolean selfChange) {
|
||
|
if (!TextUtils.isEmpty(packId) && !TextUtils.isEmpty(packKey)) {
|
||
|
previewRepository.getStickerManifest(packId, packKey, stickerManifest::postValue);
|
||
|
}
|
||
|
}
|
||
|
};
|
||
|
|
||
|
application.getContentResolver().registerContentObserver(DatabaseContentProviders.StickerPack.CONTENT_URI, true, packObserver);
|
||
|
}
|
||
|
|
||
|
LiveData<Optional<StickerManifestResult>> getStickerManifest(@NonNull String packId, @NonNull String packKey) {
|
||
|
this.packId = packId;
|
||
|
this.packKey = packKey;
|
||
|
|
||
|
previewRepository.getStickerManifest(packId, packKey, stickerManifest::postValue);
|
||
|
|
||
|
return stickerManifest;
|
||
|
}
|
||
|
|
||
|
void onInstallClicked() {
|
||
|
managementRepository.installStickerPack(packId, packKey);
|
||
|
}
|
||
|
|
||
|
void onRemoveClicked() {
|
||
|
managementRepository.uninstallStickerPack(packId, packKey);
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
protected void onCleared() {
|
||
|
application.getContentResolver().unregisterContentObserver(packObserver);
|
||
|
}
|
||
|
|
||
|
static class Factory extends ViewModelProvider.NewInstanceFactory {
|
||
|
private final Application application;
|
||
|
private final StickerPackPreviewRepository previewRepository;
|
||
|
private final StickerManagementRepository managementRepository;
|
||
|
|
||
|
Factory(@NonNull Application application,
|
||
|
@NonNull StickerPackPreviewRepository previewRepository,
|
||
|
@NonNull StickerManagementRepository managementRepository)
|
||
|
{
|
||
|
this.application = application;
|
||
|
this.previewRepository = previewRepository;
|
||
|
this.managementRepository = managementRepository;
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public @NonNull<T extends ViewModel> T create(@NonNull Class<T> modelClass) {
|
||
|
//noinspection ConstantConditions
|
||
|
return modelClass.cast(new StickerPackPreviewViewModel(application, previewRepository, managementRepository));
|
||
|
}
|
||
|
}
|
||
|
}
|