Created
September 10, 2017 15:08
-
-
Save charlag/aacc4f88c979d00802471658903b9449 to your computer and use it in GitHub Desktop.
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
val eventsFromView = viewEvents.map { viewEvent -> | |
when (viewEvent) { | |
is SampleViewEvent.TodoCheckedViewEvent -> | |
Event.TodoCheckedEvent(viewEvent.id, viewEvent.isChecked) | |
is SampleViewEvent.TodoAddedViewEvent -> | |
Event.NewTodoEvent(viewEvent.text) | |
} | |
} | |
val dbEvents = db.todoDao().getAll().toObservable().map(::DbUpdateEvent) | |
val externalEvents = Observable.merge(eventsFromView, dbEvents) | |
val transformer = Transformer<State, Event, Event> { upstream -> | |
upstream | |
.doOnNext { | |
Log.d("SampleViewModel", "Event: ${it.third}") | |
Log.d("SampleViewModel", "State: ${it.second}") | |
} | |
.publish { shared -> | |
Observable.merge( | |
shared.compose(addNewTodoEffect), | |
shared.compose(checkTodoEffect), | |
shared.compose(viewStateEffect) | |
) | |
} | |
} | |
val events = createConnectableKnot( | |
State(listOf()), | |
externalEvents, | |
this::reducer, | |
transformer | |
) | |
viewState = events.ofType(Event.ViewStateEvent::class.java) | |
.map { it.viewState } | |
.replay() | |
.autoConnect(-1) | |
events.connect() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment