Skip to content

Instantly share code, notes, and snippets.

@VladimirMi
Created January 28, 2018 07:41
Show Gist options
  • Save VladimirMi/2d2416a952397edf14f9f2c978198351 to your computer and use it in GitHub Desktop.
Save VladimirMi/2d2416a952397edf14f9f2c978198351 to your computer and use it in GitHub Desktop.
Kotlin bind view reactive
fun <T> bind(lifecycle: Observable<Lifecycle>,
mainThreadScheduler: Scheduler,
state: Observable<T>,
viewAction: (T) -> Unit) {
lifecycle
.filter { CREATE }
.switchMap { state }
.observeOn(mainThreadScheduler)
.takeUntil(lifecycle.filter { DESTROY })
.subscribe(viewAction)
}
fun bindData(lifecycle: Observable<Lifecycle>) {
fun <T> bindSimple(state: Observable<T>, viewAction: (T) -> Unit) =
bind(lifecycle, AndroidSchedulers.mainThread(), state, viewAction)
bindSimple(Observable.just(1), ::action)
}
fun action(int: Int) {
// do smth
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment