darkmoon_uk
04/27/2021, 12:26 AMStateFlow
after e.g. after performing regular Flow
transforming operations on it.
e.g. to map a StateFlow
and keep the output as a StateFlow
, I often employ a pattern like:
val isSwitchEnabledFlow: StateFlow<Boolean> by lazy {
fun isSwitchable(state: ScreenState) = state is SwitchableState // Any mapping
screenStateFlow
.map(::isSwitchable) // Apply mapping for new values
.stateIn(
scope = this,
started = SharingStarted.Eagerly,
initialValue = isSwitchable(screenStateFlow.value) // Apply mapping for initial value
)
}
...key point is defining the mapping as a privately scoped function to use in both the map
and initialValue
.
Any more concise form I've missed?