mirror of
https://github.com/oxen-io/session-android.git
synced 2024-12-11 10:44:21 +00:00
46 lines
1.1 KiB
Java
46 lines
1.1 KiB
Java
|
package org.thoughtcrime.securesms.util;
|
||
|
|
||
|
import android.arch.lifecycle.MutableLiveData;
|
||
|
import android.database.ContentObserver;
|
||
|
import android.support.annotation.NonNull;
|
||
|
|
||
|
import org.thoughtcrime.securesms.database.ObservableContent;
|
||
|
|
||
|
import java.io.Closeable;
|
||
|
|
||
|
/**
|
||
|
* Implementation of {@link android.arch.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);
|
||
|
Util.close(previous);
|
||
|
}
|
||
|
|
||
|
value.registerContentObserver(observer);
|
||
|
|
||
|
super.setValue(value);
|
||
|
}
|
||
|
|
||
|
public void close() {
|
||
|
E value = getValue();
|
||
|
|
||
|
if (value != null) {
|
||
|
value.unregisterContentObserver(observer);
|
||
|
Util.close(value);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public void registerContentObserver(@NonNull ContentObserver observer) {
|
||
|
this.observer = observer;
|
||
|
}
|
||
|
}
|