Skip to content

Instantly share code, notes, and snippets.

@halcyonmobiledev
Last active October 2, 2021 18:09
Show Gist options
  • Save halcyonmobiledev/79737cfaa9413791dc78bfb3e79d9e67 to your computer and use it in GitHub Desktop.
Save halcyonmobiledev/79737cfaa9413791dc78bfb3e79d9e67 to your computer and use it in GitHub Desktop.
Data Binding observables and Kotlin
// 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
}
@RobertPal95
Copy link

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