Created
September 24, 2019 13:53
-
-
Save bdsaglam/b13fe0a6a4e3502e7303e5dbc4101ccc to your computer and use it in GitHub Desktop.
a LiveData only notifies when its value changes
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
class LiveChange<T>(val source: LiveData<T>, ignoreFirst: Boolean = false) : MediatorLiveData<T>() { | |
private var shouldIgnoreNext: Boolean = ignoreFirst | |
init { | |
addSource(source) { | |
if (shouldIgnoreNext) { | |
shouldIgnoreNext = false | |
} else { | |
if (value != it) value = it | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment