Created
August 13, 2022 07:01
-
-
Save motorro/7248355b6398c20c8f7c602cd655b1d5 to your computer and use it in GitHub Desktop.
Lifecycle-aware view-model
This file contains 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 WithIdleViewModel : ViewModel() { | |
/** | |
* Creates initial state for state-machine | |
* You could process a deep-link here or restore from a saved state | |
*/ | |
private fun initStateMachine(): CommonMachineState<SomeGesture, SomeUiState> = InitialState() | |
/** | |
* State-machine instance | |
*/ | |
private val stateMachine = FlowStateMachine(::initStateMachine) | |
/** | |
* UI State | |
*/ | |
val state: SharedFlow<SomeUiState> = stateMachine.uiState | |
init { | |
// Subscribes to active subscribers count and updates state machine with corresponding gestures | |
stateMachine.mapUiSubscriptions( | |
viewModelScope, | |
onActive = { SomeGesture.OnActive }, | |
onInactive = { SomeGesture.OnInactive } | |
) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment