Throttle conversation list update frequency.

Reduces the load on the database (and UI jankiness) while processing a
large message backlog.
This commit is contained in:
Greyson Parrelli 2021-02-22 11:47:57 -05:00
parent c04b5f2085
commit cc7617a302

View file

@ -24,6 +24,7 @@ import org.thoughtcrime.securesms.megaphone.Megaphones;
import org.thoughtcrime.securesms.net.PipeConnectivityListener;
import org.thoughtcrime.securesms.search.SearchRepository;
import org.thoughtcrime.securesms.util.Debouncer;
import org.thoughtcrime.securesms.util.ThrottledDebouncer;
import org.thoughtcrime.securesms.util.Util;
import org.thoughtcrime.securesms.util.livedata.LiveDataUtil;
import org.thoughtcrime.securesms.util.paging.Invalidator;
@ -42,7 +43,8 @@ class ConversationListViewModel extends ViewModel {
private final LiveData<Boolean> hasNoConversations;
private final SearchRepository searchRepository;
private final MegaphoneRepository megaphoneRepository;
private final Debouncer debouncer;
private final Debouncer searchDebouncer;
private final ThrottledDebouncer updateDebouncer;
private final DatabaseObserver.Observer observer;
private final Invalidator invalidator;
@ -54,7 +56,8 @@ class ConversationListViewModel extends ViewModel {
this.searchResult = new MutableLiveData<>();
this.searchRepository = searchRepository;
this.megaphoneRepository = ApplicationDependencies.getMegaphoneRepository();
this.debouncer = new Debouncer(300);
this.searchDebouncer = new Debouncer(300);
this.updateDebouncer = new ThrottledDebouncer(500);
this.invalidator = new Invalidator();
this.pagedData = PagedData.create(ConversationListDataSource.create(application, isArchived),
new PagingConfig.Builder()
@ -62,10 +65,12 @@ class ConversationListViewModel extends ViewModel {
.setBufferPages(2)
.build());
this.observer = () -> {
updateDebouncer.publish(() -> {
if (!TextUtils.isEmpty(getLastQuery())) {
searchRepository.query(getLastQuery(), searchResult::postValue);
}
pagedData.getController().onDataInvalidated();
});
};
this.hasNoConversations = LiveDataUtil.mapAsync(pagedData.getData(), conversations -> {
@ -135,7 +140,7 @@ class ConversationListViewModel extends ViewModel {
void updateQuery(String query) {
lastQuery = query;
debouncer.publish(() -> searchRepository.query(query, result -> {
searchDebouncer.publish(() -> searchRepository.query(query, result -> {
Util.runOnMain(() -> {
if (query.equals(lastQuery)) {
searchResult.setValue(result);
@ -151,7 +156,8 @@ class ConversationListViewModel extends ViewModel {
@Override
protected void onCleared() {
invalidator.invalidate();
debouncer.clear();
searchDebouncer.clear();
updateDebouncer.clear();
ApplicationDependencies.getDatabaseObserver().unregisterObserver(observer);
}