Created
August 14, 2020 10:32
-
-
Save shiehnpin/ce13e340f64774c044d11c9e8c138f0c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun <T1, T2, R> zip( | |
source1 : LiveData<T1?>, | |
source2 : LiveData<T2?>, | |
source1Name: String = "first", | |
source2Name: String = "second", | |
zipFunction: (t1:T1?, t2:T2?, emitter:(R)->Unit) -> Unit | |
): LiveData<R>{ | |
return object : MediatorLiveData<R>(){ | |
var t1: T1? = null | |
var t2: T2? = null | |
var emit:(r: R)-> Unit = { r -> | |
value = r | |
} | |
init { | |
addSource(source1){ | |
Timber.d("zip by $source1Name:$it") | |
t1 = it | |
zipFunction(t1, t2, emit) | |
} | |
addSource(source2){ | |
Timber.d("zip by $source2Name:$it") | |
t2 = it | |
zipFunction(t1, t2, emit) | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment