Created
June 11, 2018 11:41
-
-
Save amay077/f17315483d63e76fe9adf252b3e02b1a to your computer and use it in GitHub Desktop.
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
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