1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
package it.niedermann.android.reactivelivedata.combinator;
import androidx.annotation.NonNull;
import androidx.arch.core.util.Function;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MediatorLiveData;
import androidx.lifecycle.Observer;
import kotlin.Triple;
class TripleCombinatorObserver<T, Y, Z> implements Observer<T> {
private final MediatorLiveData<Triple<T, Y, Z>> mediator;
private final Function<T, LiveData<Y>> secondSourceFunction;
private final Function<T, LiveData<Z>> thirdSourceFunction;
private T value1;
private Y value2;
private Z value3;
private LiveData<Y> secondSource;
private LiveData<Z> thirdSource;
private boolean value1emitted = false;
private boolean value2emitted = false;
private boolean value3emitted = false;
public TripleCombinatorObserver(@NonNull MediatorLiveData<Triple<T, Y, Z>> mediator, @NonNull Function<T, LiveData<Y>> secondSourceFunction, @NonNull Function<T, LiveData<Z>> thirdSourceFunction) {
this.mediator = mediator;
this.secondSourceFunction = secondSourceFunction;
this.thirdSourceFunction = thirdSourceFunction;
}
@Override
public void onChanged(T emittedValue1) {
value1 = emittedValue1;
value1emitted = true;
if (value2emitted && value3emitted) {
mediator.setValue(new Triple<>(value1, value2, value3));
}
if (secondSource == null) {
secondSource = secondSourceFunction.apply(emittedValue1);
mediator.addSource(secondSource, val2 -> {
value2 = val2;
value2emitted = true;
if (value1emitted && value3emitted) {
mediator.setValue(new Triple<>(value1, value2, value3));
}
});
}
if (thirdSource == null) {
thirdSource = thirdSourceFunction.apply(emittedValue1);
mediator.addSource(thirdSource, val3 -> {
value3 = val3;
value3emitted = true;
if (value1emitted && value2emitted) {
mediator.setValue(new Triple<>(value1, value2, value3));
}
});
}
}
}
|