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
| sealed class CommentFormUserEvent { | |
| data class TextChangedEvent(..): CommentFormUserEvent() | |
| data class SubmitCommentRequestEvent(..): CommentFormUserEvent() | |
| } | |
| sealed class LoginFormUserEvent { | |
| // We cannot use TextChangedEvent from CommentFormUserEvent | |
| data class TextChangedEvent(..): LoginFormUserEvent() | |
| data class LoginRequestEvent(..): LoginFormUserEvent() | |
| } |
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 reduce(event: TextChangedEvent): (CommentFormData) -> CommentFormData { | |
| // implementation | |
| return { state -> | |
| // Let's check comment validity | |
| val isValid = event.enteredText.length > 5 | |
| // Update the current state with the changes | |
| return state.copy(comment = event.enteredText, isCommentValid = isValid) | |
| } | |
| } |
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 reduce(event: TextChangedEvent): Reducer<CommentFormData> { | |
| // implementation | |
| } | |
| fun reduce(event: Lce<Comment>): Reducer<CommentFormData> { | |
| // implementation | |
| } | |
| fun formState( |
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 CommentFormViewState( | |
| val textEntered: String, | |
| val isSubmitButtonEnabled: Boolean, | |
| val onTextEntered: (String) -> Unit, /* Callback when user modifies the text */ | |
| val onSubmitButtonSelected: () -> Unit /* Callback when user clicks submit button */) |
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 CommentFormView(val rootView: View) { | |
| // View binding logic | |
| val commentTextField: EditText = rootView.findViewById(R.id.comment_text_field) | |
| val submitButton: Button = rootView.findViewById(R.id.submit_button) | |
| // Our view update function, takes a view state | |
| // snapshot and updates the android views | |
| fun setViewState(state: CommentFormViewState) { | |
| commentTextField.text = state.textEntered | |
| submitButton.isEnabled = state.isSubmitButtonEnabled |
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 CommentFormData( | |
| // defines what user has entered | |
| val comment: String, | |
| // defines if the entered comment is valid for submission | |
| val isCommentValid: Boolean, | |
| // defines if there is a submit request in progress | |
| val submitRequest: Lce<Comment>? | |
| ) |
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
| // User modifies the comment text field | |
| data class TextChangedEvent(val enteredText: String) | |
| // User clicks submit | |
| data class SubmitCommentEvent(val comment: String) |
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 CommentFormViewModel { | |
| // Let's create a view state from the data provided. | |
| fun createViewState( | |
| data: CommentFormData, | |
| onTextChanged: (TextChangedEvent) -> Unit, | |
| onSubmitSelected: (SubmitCommentEvent) -> Unit | |
| ): CommentFormViewState{ | |
| val isSubmitting = data.submitRequest?.let { it.isLoading} ?: false | |
| return CommentFormViewState( | |
| textEntered = data.comment, |
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), |
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) | |
| } | |
| } | |
| } |