Last active
June 18, 2021 07:42
-
-
Save abdalin/9db979191b044ba16e6be64fc968bc6b 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
import androidx.lifecycle.LiveData | |
import androidx.lifecycle.MediatorLiveData | |
class BiSourceMergerLiveData<T, U, R>( | |
source1: LiveData<T>, | |
source2: LiveData<U>, | |
block: (T?, U?) -> R? | |
) : MediatorLiveData<R>() { | |
init { | |
addSource(source1) { | |
block(it, source2.value) | |
?.run { value = this } | |
} | |
addSource(source2) { | |
block(source1.value, it) | |
?.run { value = this } | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment