Skip to content

Instantly share code, notes, and snippets.

@bdsaglam
Created September 24, 2019 13:53
Show Gist options
  • Save bdsaglam/b13fe0a6a4e3502e7303e5dbc4101ccc to your computer and use it in GitHub Desktop.
Save bdsaglam/b13fe0a6a4e3502e7303e5dbc4101ccc to your computer and use it in GitHub Desktop.
a LiveData only notifies when its value changes
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