Last active
October 2, 2021 18:09
-
-
Save halcyonmobiledev/79737cfaa9413791dc78bfb3e79d9e67 to your computer and use it in GitHub Desktop.
Data Binding observables and Kotlin
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
// with just a simple extension function for the Data Binding ObservableField | |
inline fun <R> ObservableField<R>.observe(crossinline callback: (R) -> Unit) { | |
this.addOnPropertyChangedCallback(object : Observable.OnPropertyChangedCallback() { | |
override fun onPropertyChanged(p0: Observable?, p1: Int) { | |
callback(get()) | |
} | |
}) | |
} | |
... | |
// observing the change of data is simple, clean and readable. | |
viewModel.user.observe { | |
//TODO use whatever with the observed data | |
} | |
BryanJBryce
commented
Dec 13, 2018
Just add !! after get() and it will work fine.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment