Skip to content

Instantly share code, notes, and snippets.

@ShivamKumarJha
Created April 3, 2023 06:35
Show Gist options
  • Save ShivamKumarJha/9a992eb7c476e2cc065a5423559f330b to your computer and use it in GitHub Desktop.
Save ShivamKumarJha/9a992eb7c476e2cc065a5423559f330b to your computer and use it in GitHub Desktop.
import androidx.lifecycle.LiveData
import androidx.lifecycle.MediatorLiveData
fun <T, K, R> LiveData<T>.combineWith(
liveData: LiveData<K>,
block: (T?, K?) -> R
): LiveData<R> {
val result = MediatorLiveData<R>()
result.addSource(this) {
result.value = block(this.value, liveData.value)
}
result.addSource(liveData) {
result.value = block(this.value, liveData.value)
}
return result
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment