Created
January 15, 2021 07:42
-
-
Save mahdit83/375c3e8ed1ba07f23c47e9be8f74442a to your computer and use it in GitHub Desktop.
Double trigger Live data
This file contains hidden or 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 <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