Convert all database notifiers to use DatabaseObserver.

Lots of red in this diff to celebrate the release of Red (Taylor's Version).
This commit is contained in:
Greyson Parrelli
2021-11-12 12:14:59 -05:00
committed by Cody Henthorne
parent ab55fec6bd
commit 658de3b6e7
30 changed files with 179 additions and 729 deletions

View File

@@ -1,47 +0,0 @@
package org.thoughtcrime.securesms.util;
import android.database.ContentObserver;
import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;
import org.signal.core.util.StreamUtil;
import org.thoughtcrime.securesms.database.ObservableContent;
import java.io.Closeable;
/**
* Implementation of {@link androidx.lifecycle.LiveData} that will handle closing the contained
* {@link Closeable} when the value changes.
*/
public class ObservingLiveData<E extends ObservableContent> extends MutableLiveData<E> {
private ContentObserver observer;
@Override
public void setValue(E value) {
E previous = getValue();
if (previous != null) {
previous.unregisterContentObserver(observer);
StreamUtil.close(previous);
}
value.registerContentObserver(observer);
super.setValue(value);
}
public void close() {
E value = getValue();
if (value != null) {
value.unregisterContentObserver(observer);
StreamUtil.close(value);
}
}
public void registerContentObserver(@NonNull ContentObserver observer) {
this.observer = observer;
}
}