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 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 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 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 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): Reducer<CommentFormData> { | |
// implementation | |
} | |
fun reduce(event: Lce<Comment>): Reducer<CommentFormData> { | |
// implementation | |
} | |
fun formState( |
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
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 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 UserEvent { | |
data class TextChangedEvent(...): UserEvent() | |
data class SubmitCommentRequestEvent(...): UserEvent() | |
} |
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>> | |
} |