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.database.model.StickerRecord; import org.thoughtcrime.securesms.logging.Log; import org.thoughtcrime.securesms.util.Throttler; import java.util.List; final class StickerKeyboardPageViewModel extends ViewModel { static final String RECENT_PACK_ID = "RECENT"; private final Application application; private final StickerKeyboardRepository repository; private final MutableLiveData> stickers; private final Throttler observerThrottler; private final ContentObserver observer; private String packId; private StickerKeyboardPageViewModel(@NonNull Application application, @NonNull StickerKeyboardRepository repository) { this.application = application; this.repository = repository; this.stickers = new MutableLiveData<>(); this.observerThrottler = new Throttler(500); this.observer = new ContentObserver(new Handler()) { @Override public void onChange(boolean selfChange) { observerThrottler.publish(() -> getStickers(packId)); } }; application.getContentResolver().registerContentObserver(DatabaseContentProviders.Sticker.CONTENT_URI, true, observer); } LiveData> getStickers(@NonNull String packId) { this.packId = packId; if (RECENT_PACK_ID.equals(packId)) { repository.getRecentStickers(stickers::postValue); } else { repository.getStickersForPack(packId, stickers::postValue); } return stickers; } @Override protected void onCleared() { application.getContentResolver().unregisterContentObserver(observer); } static class Factory extends ViewModelProvider.NewInstanceFactory { private final Application application; private final StickerKeyboardRepository repository; Factory(@NonNull Application application, @NonNull StickerKeyboardRepository repository) { this.application = application; this.repository = repository; } @Override public @NonNull T create(@NonNull Class modelClass) { //noinspection ConstantConditions return modelClass.cast(new StickerKeyboardPageViewModel(application, repository)); } } }