Created
March 7, 2019 01:32
-
-
Save GargoyleLizy/9ca2c9e66d462605aea73a9994e1271e to your computer and use it in GitHub Desktop.
RxLiveData_filter
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
| /** | |
| * 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