Skip to content

Instantly share code, notes, and snippets.

@kaszabimre
Last active July 7, 2023 14:10
Show Gist options
  • Save kaszabimre/c790f2c343e38b512f934efb9d3f6b54 to your computer and use it in GitHub Desktop.
Save kaszabimre/c790f2c343e38b512f934efb9d3f6b54 to your computer and use it in GitHub Desktop.
Reducer implementation v1
interface UiState
interface UiEvent
abstract class Reducer<S : UiState, E : UiEvent>(initialVal: S) : ViewModel() {
//...
private val _state: MutableStateFlow<S> = MutableStateFlow(initialVal)
val state: StateFlow<S>
get() = _state
private val _error: MutableStateFlow<String?> = MutableStateFlow(null)
val error: StateFlow<String?>
get() = _error
val stateFlowAdapter = state.asCallbacks()
val errorFlowAdapter = error.asCallbacks()
private fun <T : Any?> StateFlow<T>.asCallbacks() = FlowAdapter(viewModelScope, this)
//...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment