Skip to content

Instantly share code, notes, and snippets.

Laimonas Turauskas Laimiux

Block or report user

Report or block Laimiux

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View comment_form_model_contract.kt
fun createDataStream(
textChangedEvents: Flowable<TextChangedEvent>,
submitCommentEvents: Flowable<SubmitCommentEvent>
): Flowable<CommentFormData>
View comment_form_presenter.kt
class CommentFormPresenter(val viewModel: CommentViewModel) {
fun attach(view: CommentFormView) {
// Don't forget to unsubscribe
viewModel.viewStateStream().subscribe { state ->
view.setViewState(state)
}
}
}
View comment_view_model_integration.kt
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),
View comment_form_create_view_state.kt
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,
View comment_form_user_actions.kt
// User modifies the comment text field
data class TextChangedEvent(val enteredText: String)
// User clicks submit
data class SubmitCommentEvent(val comment: String)
View comment_form_data.kt
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>?
)
View comment_form_view.kt
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
View comment_form_view_state.kt
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 */)
View comment_form_final_reducer.kt
fun reduce(event: TextChangedEvent): Reducer<CommentFormData> {
// implementation
}
fun reduce(event: Lce<Comment>): Reducer<CommentFormData> {
// implementation
}
fun formState(
View comment_form_partial_reducer.kt
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)
}
}
You can’t perform that action at this time.