Skip to content

Instantly share code, notes, and snippets.

@amay077
Created June 11, 2018 11:41
Show Gist options
  • Save amay077/f17315483d63e76fe9adf252b3e02b1a to your computer and use it in GitHub Desktop.
Save amay077/f17315483d63e76fe9adf252b3e02b1a to your computer and use it in GitHub Desktop.
package your.awesome.domain
import android.arch.lifecycle.LiveData
import android.arch.lifecycle.MediatorLiveData
fun <T> LiveData<T>.filter(matcher: (T?)->Boolean): LiveData<T> {
val result = MediatorLiveData<T>()
result.addSource(this, {
if (matcher.invoke(it)) {
result.value = it
}
})
if (matcher.invoke(this.value)) {
result.value = this.value
}
return result;
}
fun <T1, T2, S> LiveData<T1>.combineLatest(source2: LiveData<T2>,
func: (T1?, T2?) -> S?): LiveData<S> {
val result = MediatorLiveData<S>()
result.addSource(this, {
result.value = func.invoke(this.value, source2.value)
})
result.addSource(source2, {
result.value = func.invoke(this.value, source2.value)
})
result.value = func.invoke(this.value, source2.value)
return result
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment