Skip to content

Instantly share code, notes, and snippets.

@abdalin
Last active June 18, 2021 07:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abdalin/9db979191b044ba16e6be64fc968bc6b to your computer and use it in GitHub Desktop.
Save abdalin/9db979191b044ba16e6be64fc968bc6b to your computer and use it in GitHub Desktop.
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