Skip to content

Instantly share code, notes, and snippets.

@guness
Last active May 15, 2023 14:10
Show Gist options
  • Save guness/0a96d80bc1fb969fa70a5448aa34c215 to your computer and use it in GitHub Desktop.
Save guness/0a96d80bc1fb969fa70a5448aa34c215 to your computer and use it in GitHub Desktop.
LiveData merger that takes two live data inputs and a merger function. Merges two results using merger function, and returning result allowing null inputs and outputs. Input and out types are parametric. However only supports two live data inputs for now.
import androidx.annotation.NonNull;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MediatorLiveData;
import androidx.lifecycle.Observer;
import java.util.function.BiFunction;
public class CombinedLiveData<T, K, S> extends MediatorLiveData<S> {
private T data1;
private K data2;
public CombinedLiveData(LiveData<T> source1, LiveData<K> source2, BiFunction<T, K, S> combine) {
super.addSource(source1, it -> {
data1 = it;
setValue(combine.apply(data1, data2));
});
super.addSource(source2, it -> {
data2 = it;
setValue(combine.apply(data1, data2));
});
}
@Override
public <S1> void addSource(@NonNull LiveData<S1> source, @NonNull Observer<? super S1> onChanged) {
throw new UnsupportedOperationException();
}
@Override
public <S1> void removeSource(@NonNull LiveData<S1> toRemote) {
throw new UnsupportedOperationException();
}
}
import androidx.lifecycle.LiveData
import androidx.lifecycle.MediatorLiveData
import androidx.lifecycle.Observer
class CombinedLiveData<T, K, S>(source1: LiveData<T>, source2: LiveData<K>, private val combine: (data1: T?, data2: K?) -> S) : MediatorLiveData<S>() {
private var data1: T? = null
private var data2: K? = null
init {
super.addSource(source1) {
data1 = it
value = combine(data1, data2)
}
super.addSource(source2) {
data2 = it
value = combine(data1, data2)
}
}
override fun <T : Any?> addSource(source: LiveData<T>, onChanged: Observer<in T>) {
throw UnsupportedOperationException()
}
override fun <T : Any?> removeSource(toRemote: LiveData<T>) {
throw UnsupportedOperationException()
}
}
@asalha
Copy link

asalha commented Jun 15, 2019

Can you please translate this to Java? I don't know Kotlin
Thanks in advance

@KingsleyUsoroeno
Copy link

These might be difficult to convert to Java as Java does not have Higher other functions as does kotlin

@guness
Copy link
Author

guness commented Oct 1, 2020

Java versions are added. FYI.

Copy link

ghost commented Apr 8, 2021

Why you no use lifecycle owner?

@guness
Copy link
Author

guness commented Apr 8, 2021

Why you no use lifecycle owner?

I don't remember why now, but maybe MediatorLiveData handles it internally?

@utsmannn
Copy link

Why you no use lifecycle owner?

I don't remember why now, but maybe MediatorLiveData handles it internally?

The MediatorLiveData can be observed with lifecycle owner, so don't need the lifecycle owner for create it

Copy link

ghost commented Apr 19, 2021

The MediatorLiveData can be observed with lifecycle owner, so don't need the lifecycle owner for create it
yes, that's true. Sorry, my bad )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment