MavericksViewModel

abstract class MavericksViewModel<S : MavericksState>(initialState: S)

All Mavericks ViewModels must extend this class. In Mavericks, ViewModels are generic on a single state class. The ViewModel owns all state modifications via setState and other classes may observe the state.

From a MavericksView/Fragment, using the view model provider delegates will automatically subscribe to state updates in a lifecycle-aware way and call MavericksView.invalidate whenever it changes.

Other classes can observe the state via stateFlow.

Constructors

MavericksViewModel
Link copied to clipboard
fun <S : MavericksState> MavericksViewModel(initialState: S)

Functions

awaitState
Link copied to clipboard
suspend fun awaitState(): S

Calling this function suspends until all pending setState reducers are run and then returns the latest state. As a result, it is safe to call setState { } and assume that the result from a subsequent awaitState() call will have that state.

onCleared
Link copied to clipboard
open fun onCleared()
toString
Link copied to clipboard
open override fun toString(): String

Properties

config
Link copied to clipboard
val config: MavericksViewModelConfig<S>
stateFlow
Link copied to clipboard
val stateFlow: Flow<S>

Return the current state as a Flow. For certain situations, this may be more convenient than subscribe and selectSubscribe because it can easily be composed with other coroutines operations and chained with operators.

viewModelScope
Link copied to clipboard
val viewModelScope: CoroutineScope