Skip to content

Instantly share code, notes, and snippets.

@mahdit83
Created January 15, 2021 07:42
Show Gist options
  • Save mahdit83/375c3e8ed1ba07f23c47e9be8f74442a to your computer and use it in GitHub Desktop.
Save mahdit83/375c3e8ed1ba07f23c47e9be8f74442a to your computer and use it in GitHub Desktop.
Double trigger Live data
fun <A, B> DoubleTriggerLiveData(a: LiveData<A>, b: LiveData<B>): LiveData<Pair<A, B>> {
return MediatorLiveData<Pair<A, B>>().apply {
var lastA: A? = null
var lastB: B? = null
fun update() {
val localLastA = lastA
val localLastB = lastB
if (localLastA != null && localLastB != null)
this.value = Pair(localLastA, localLastB)
}
addSource(a) {
lastA = it
update()
}
addSource(b) {
lastB = it
update()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment