session-android/src/org/thoughtcrime/securesms/stickers/StickerPackPreviewViewModel.java
Greyson Parrelli 2a644437fb Add sticker support.
No sticker packs are available for use yet, but we now have the
latent ability to send and receive.
2019-05-30 01:08:01 -04:00

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));
}
}
}