mirror of
https://github.com/oxen-io/session-android.git
synced 2024-11-29 04:55:15 +00:00
36 lines
742 B
Java
36 lines
742 B
Java
|
package org.thoughtcrime.securesms.util;
|
||
|
|
||
|
import android.arch.lifecycle.MutableLiveData;
|
||
|
|
||
|
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 CloseableLiveData<E extends Closeable> extends MutableLiveData<E> {
|
||
|
|
||
|
@Override
|
||
|
public void setValue(E value) {
|
||
|
setValue(value, true);
|
||
|
}
|
||
|
|
||
|
public void setValue(E value, boolean closePrevious) {
|
||
|
E previous = getValue();
|
||
|
|
||
|
if (previous != null && closePrevious) {
|
||
|
Util.close(previous);
|
||
|
}
|
||
|
|
||
|
super.setValue(value);
|
||
|
}
|
||
|
|
||
|
public void close() {
|
||
|
E value = getValue();
|
||
|
|
||
|
if (value != null) {
|
||
|
Util.close(value);
|
||
|
}
|
||
|
}
|
||
|
}
|