This file contains hidden or 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
| override fun evaluate( | |
| input: Unit, | |
| state: State, | |
| context: FormulaContext<State> | |
| ): Evaluation<StopwatchRenderModel> { | |
| return Evaluation( | |
| renderModel = ..., | |
| updates = context.updates { | |
| if (state.isRunning) { | |
| val incrementTimePassedEvents = RxStream.fromObservable { |
This file contains hidden or 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
| class StopwatchFormula : Formula<Unit, StopwatchFormula.State, StopwatchRenderModel> { | |
| ... | |
| override fun evaluate( | |
| input: Unit, | |
| state: State, | |
| context: FormulaContext<State> | |
| ): Evaluation<StopwatchRenderModel> { | |
| return Evaluation( |
This file contains hidden or 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
| class StopwatchFormula : Formula<Unit, StopwatchFormula.State, StopwatchRenderModel> { | |
| data class State( | |
| val timePassedInMillis: Long, | |
| val isRunning: Boolean | |
| ) | |
| override fun initialState(input: Unit): State = State( | |
| timePassedInMillis = 0, | |
| isRunning = false |
This file contains hidden or 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
| data class StopwatchState( | |
| val timePassedInMillis: Long, | |
| val isRunning: Boolean | |
| ) |
This file contains hidden or 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
| class StopwatchFormula : Formula<Unit, StopwatchState, StopwatchRenderModel> { | |
| override fun initialState(input: Unit) = StopwatchState( | |
| timePassedInMillis = 0, | |
| isRunning = false | |
| ) | |
| override fun evaluate( | |
| input: Unit, | |
| state: StopwatchState, |
This file contains hidden or 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
| data class StopwatchRenderModel( | |
| val timePassed: String, | |
| val startStopButton: ButtonRenderModel, | |
| val resetButton: ButtonRenderModel | |
| ) | |
| data class ButtonRenderModel( | |
| val text: String, | |
| val onSelected: () -> Unit | |
| ) |
This file contains hidden or 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
| class StopwatchRenderView(root: ViewGroup): RenderView<StopwatchRenderModel> { | |
| private val timePassed: TextView = root.findViewById(R.id.time_passed_text_view) | |
| private val startStopButton: Button = root.findViewById(R.id.start_stop_button) | |
| private val resetButton: Button = root.findViewById(R.id.reset_button) | |
| override val renderer: Renderer<StopwatchRenderModel> = Renderer.create { model -> | |
| timePassed.text = model.timePassed | |
| startStopButton.text = model.startStopButton.text | |
| startStopButton.setOnClickListener { |
This file contains hidden or 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
| fun createDataStream( | |
| textChangedEvents: Flowable<TextChangedEvent>, | |
| submitCommentEvents: Flowable<SubmitCommentEvent> | |
| ): Flowable<CommentFormData> |
This file contains hidden or 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
| class CommentFormPresenter(val viewModel: CommentViewModel) { | |
| fun attach(view: CommentFormView) { | |
| // Don't forget to unsubscribe | |
| viewModel.viewStateStream().subscribe { state -> | |
| view.setViewState(state) | |
| } | |
| } | |
| } |
This file contains hidden or 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
| 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), |