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
typealias Reducer<T> = (T) -> T |
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 changes text entry | |
data class TextChangedEvent(val newText: String) | |
// User clicks create | |
data class CreateTodoEvent(val text: 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 TodoFormState( | |
val enteredText: String, | |
// After user clicks create, we send a post request. | |
val createTodoRequest: CreateTodoRequestState?) |
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 can submit the comment | |
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
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
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
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
// 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
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
fun dataStream( | |
textChangedEvents: Flowable<TextChangedEvent>, | |
submitCommentEvents: Flowable<SubmitCommentEvent> | |
): Flowable<CommentFormData> { | |
// We need to start with something | |
val initialFormState = CommentFormData(comment = "", isCommentValid = false, submitRequest = null) | |
// RxJava scan operator takes initial state and a reduce function | |
// This operator manages the state for us, where it will keep track | |
// of the latest state and provide it for next incoming event | |
return textChangedEvents.scan(initialFormState) { event, currentState -> |