Skip to content

Instantly share code, notes, and snippets.

@charlag
Created September 10, 2017 15:08
Show Gist options
  • Save charlag/aacc4f88c979d00802471658903b9449 to your computer and use it in GitHub Desktop.
Save charlag/aacc4f88c979d00802471658903b9449 to your computer and use it in GitHub Desktop.
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