Skip to content

Instantly share code, notes, and snippets.

@uteke
Last active September 2, 2023 19:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save uteke/d7ac9662b1d4301b054fce038e67638a to your computer and use it in GitHub Desktop.
Save uteke/d7ac9662b1d4301b054fce038e67638a to your computer and use it in GitHub Desktop.
UserListViewModel implementation with reducers & processors
class UserListViewModel(
private val actionProcessors: Collection<ActionProcessor<Action, Mutation, Event>>,
private val reducers: Collection<Reducer<Mutation, ViewState>>,
private val initialState: ViewState = ViewState(),
): ViewModel() {
...
fun process(action: Action) {
viewModelScope.launch {
actionProcessors
.map { actionProcessor -> actionProcessor(action) }
.merge()
.collect { value ->
mutation?.let(::handleMutation)
event?.let(eventChannel::trySend)
}
}
}
private fun handleMutation(mutation: Mutation) {
reducers
.asIterable()
.forEach { reducer ->
_viewStateFlow.update { currentState ->
reducer(mutation, currentState)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment