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
interface CommentRepo { | |
fun submitComment(comment: String): Observable<Lce<Comment>> | |
} |
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
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 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 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 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 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, state: CommentFormData): CommentFormData { | |
// 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 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 CommentFormModel(val commentService: CommentService) { | |
fun formState( | |
textChangedEvents: Observable<TextChangedEvent>, | |
submitEvents: Observable<SubmitCommentEvent> | |
) : Observable<CommentFormData> { | |
// Explicitly declaring the type, for clarity sake | |
val requestStateEvents: Observable<Lce<Comment>> = submitEvents | |
.switchMap { event -> commentService.submit(event.comment) } |
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
fun createDataStream( | |
textChangedEvents: Flowable<TextChangedEvent>, | |
submitCommentEvents: Flowable<SubmitCommentEvent> | |
): Flowable<CommentFormData> |
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
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 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( |