Created
April 13, 2022 17:15
-
-
Save linean/97326566177207aa6ff59487d2a90634 to your computer and use it in GitHub Desktop.
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 ViewModel { | |
private val events = Channel<Event>() | |
val state = events.receiveAsFlow() | |
.runningFold(State(), ::reduceState) | |
.stateIn(viewModelScope, Eagerly, State()) | |
fun handleEvent(event: Event) { | |
events.trySend(event) | |
} | |
private fun reduceState(currentState: State, event: Event): State { | |
return when (event) { | |
is Increment -> currentState.copy(counter = currentState.counter + 1) | |
is Decrement -> currentState.copy(counter = currentState.counter - 1) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Article: https://maciej-sady.medium.com/all-you-need-for-mvi-is-kotlin-how-to-reduce-without-reducer-5e986856610f
Project: https://github.com/linean/StateReducerFlow