Skip to content

Instantly share code, notes, and snippets.

@Laimiux
Created March 27, 2018 06:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Laimiux/bfbc4ffef0f9436cf7925fc76852e7f0 to your computer and use it in GitHub Desktop.
Save Laimiux/bfbc4ffef0f9436cf7925fc76852e7f0 to your computer and use it in GitHub Desktop.
class CommentViewModel(val model: CommentFormModel) {
fun viewStateStream(): Flowable<CommentFormViewState> {
// define the relays that will allow to complete unidirectional data flow
val textChangedEventRelay = PublishRelay.create<TextChangedEvent>()
val submitEventRelay = PublishRelay.create<SubmitCommentEvent>()
return model
.dataStream(
// We pass the user action flowables to the CommentFormModel
textChangedEvents = textChangedEventRelay.toFlowable(Backpressure.LATEST),
submitEventRelay = submitEventRelay.toFlowable(Backpressure.LATEST)
)
.map { data ->
// creating view state
createViewStata(
data = data,
onTextChanged = { event ->
// Pass the event to the relay
textChangedEventRelay.accept(event)
},
onSubmitSelected = { event ->
// Pass the event to the relay
submitEventRelay.accept(event)
})
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment