Skip to content

Instantly share code, notes, and snippets.

@GargoyleLizy
Created March 7, 2019 01:32
Show Gist options
  • Save GargoyleLizy/9ca2c9e66d462605aea73a9994e1271e to your computer and use it in GitHub Desktop.
Save GargoyleLizy/9ca2c9e66d462605aea73a9994e1271e to your computer and use it in GitHub Desktop.
RxLiveData_filter
/**
* With Given LiveData, produce a new LiveData, which will filter given one's emitted item with
* supplied predicate.
*/
fun <T> LiveData<T>.filter(predicate: (T) -> Boolean): LiveData<T> {
val filtered = MediatorLiveData<T>()
filtered.addSource(this) {
if (predicate(it)) {
filtered.value = it
}
}
return filtered
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment