-
-
Save fangzhzh/7bea917d94dd0f9e589d44af87d9c5f7 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 | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment