android-mvvm
android-mvvm copied to clipboard
Kotlin for cleaner ViewModels
Add a kotlin artifact android-mvvm-kotlin
with convenient extension functions for converting between Fields and Observables
fun <T> ObservableField<T>.toObservable(): Observable<T> = Observable.create { e ->
val initialValue = [email protected]()
if (initialValue != null) {
e.onNext(initialValue)
}
val callback = object : OnPropertyChangedCallback() {
override fun onPropertyChanged(observable: android.databinding.Observable, i: Int) {
e.onNext([email protected]())
}
}
[email protected](callback)
e.setCancellable { [email protected](callback) }
}
? Something like this?
You can use the FieldUtils.toObservable
inside the extension function. The code doesn't need to be repeated.
fun <T> ObservableField<T>.toObservable(): Observable<T> = FieldUtils.toObservable(this)
What... I didn't know that...