mirror of
https://github.com/oxen-io/session-android.git
synced 2025-12-31 10:56:09 +00:00
LiveDataUtil combineLatest.
This commit is contained in:
committed by
Greyson Parrelli
parent
3c5ad519dd
commit
33e3f78be6
@@ -11,6 +11,7 @@ public class DefaultValueLiveData<T> extends MutableLiveData<T> {
|
||||
private final T defaultValue;
|
||||
|
||||
public DefaultValueLiveData(@NonNull T defaultValue) {
|
||||
super(defaultValue);
|
||||
this.defaultValue = defaultValue;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
package org.thoughtcrime.securesms.util.livedata;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.lifecycle.LiveData;
|
||||
import androidx.lifecycle.MediatorLiveData;
|
||||
|
||||
public final class LiveDataUtil {
|
||||
|
||||
private LiveDataUtil() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Once there is non-null data on both input {@link LiveData}, the {@link Combine} function is run
|
||||
* and produces a live data of the combined data.
|
||||
* <p>
|
||||
* As each live data changes, the combine function is re-run, and a new value is emitted always
|
||||
* with the latest, non-null values.
|
||||
*/
|
||||
public static <A, B, R> LiveData<R> combineLatest(@NonNull LiveData<A> a,
|
||||
@NonNull LiveData<B> b,
|
||||
@NonNull Combine<A, B, R> combine) {
|
||||
return new CombineLiveData<>(a, b, combine);
|
||||
}
|
||||
|
||||
public interface Combine<A, B, R> {
|
||||
@NonNull R apply(@NonNull A a, @NonNull B b);
|
||||
}
|
||||
|
||||
private static final class CombineLiveData<A, B, R> extends MediatorLiveData<R> {
|
||||
private A a;
|
||||
private B b;
|
||||
|
||||
CombineLiveData(LiveData<A> liveDataA, LiveData<B> liveDataB, Combine<A, B, R> combine) {
|
||||
if (liveDataA == liveDataB) {
|
||||
|
||||
addSource(liveDataA, (a) -> {
|
||||
if (a != null) {
|
||||
this.a = a;
|
||||
//noinspection unchecked: A is B if live datas are same instance
|
||||
this.b = (B) a;
|
||||
setValue(combine.apply(a, b));
|
||||
}
|
||||
});
|
||||
|
||||
} else {
|
||||
|
||||
addSource(liveDataA, (a) -> {
|
||||
if (a != null) {
|
||||
this.a = a;
|
||||
if (b != null) {
|
||||
setValue(combine.apply(a, b));
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
addSource(liveDataB, (b) -> {
|
||||
if (b != null) {
|
||||
this.b = b;
|
||||
if (a != null) {
|
||||
setValue(combine.apply(a, b));
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user