mirror of
https://github.com/oxen-io/session-android.git
synced 2025-12-03 10:32:39 +00:00
63 lines
2.3 KiB
Java
63 lines
2.3 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.StickerKeyboardRepository.PackListResult;
|
||
|
|
import org.thoughtcrime.securesms.util.Throttler;
|
||
|
|
|
||
|
|
final class StickerKeyboardViewModel extends ViewModel {
|
||
|
|
|
||
|
|
private final Application application;
|
||
|
|
private final MutableLiveData<PackListResult> packs;
|
||
|
|
private final Throttler observerThrottler;
|
||
|
|
private final ContentObserver observer;
|
||
|
|
|
||
|
|
private StickerKeyboardViewModel(@NonNull Application application, @NonNull StickerKeyboardRepository repository) {
|
||
|
|
this.application = application;
|
||
|
|
this.packs = new MutableLiveData<>();
|
||
|
|
this.observerThrottler = new Throttler(500);
|
||
|
|
this.observer = new ContentObserver(new Handler()) {
|
||
|
|
@Override
|
||
|
|
public void onChange(boolean selfChange) {
|
||
|
|
observerThrottler.publish(() -> repository.getPackList(packs::postValue));
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
repository.getPackList(packs::postValue);
|
||
|
|
application.getContentResolver().registerContentObserver(DatabaseContentProviders.StickerPack.CONTENT_URI, true, observer);
|
||
|
|
}
|
||
|
|
|
||
|
|
@NonNull LiveData<PackListResult> getPacks() {
|
||
|
|
return packs;
|
||
|
|
}
|
||
|
|
|
||
|
|
@Override
|
||
|
|
protected void onCleared() {
|
||
|
|
application.getContentResolver().unregisterContentObserver(observer);
|
||
|
|
}
|
||
|
|
|
||
|
|
public static final class Factory extends ViewModelProvider.NewInstanceFactory {
|
||
|
|
private final Application application;
|
||
|
|
private final StickerKeyboardRepository repository;
|
||
|
|
|
||
|
|
public Factory(@NonNull Application application, @NonNull StickerKeyboardRepository 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 StickerKeyboardViewModel(application, repository));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|