You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I wanna combine multiplatform decompse combined with KMP-ObservableViewModel library. The advantage should be that I won't have to implement the constructor manually in each view. Also my models are already done using that library, so I would prefer not having to rework them all.
And for that I would wanna use StateFlow instead of Value for the child stack or child slot. What is the best way to do it? I came up with the following code, but I am not sure if doing local MutableStateFlow is ok.
abstract class ObservableComponent(
componentContext: ComponentContext,
) : ViewModel(), ComponentContext by componentContext {
protected inline fun <reified T : Any> Value<T>.toStateFlow(mode: ObserveLifecycleMode = ObserveLifecycleMode.START_STOP): StateFlow<T> {
val state = MutableStateFlow(viewModelScope, this.value)
this.subscribe(lifecycle, mode) { value ->
state.value = value
}
return state.asStateFlow()
}
}
Usage:
(of course the toStateFlow could be hidden inside a childSlot overload)
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Hello,
I wanna combine multiplatform decompse combined with KMP-ObservableViewModel library. The advantage should be that I won't have to implement the constructor manually in each view. Also my models are already done using that library, so I would prefer not having to rework them all.
And for that I would wanna use
StateFlow
instead ofValue
for the child stack or child slot. What is the best way to do it? I came up with the following code, but I am not sure if doing localMutableStateFlow
is ok.Usage:
(of course the
toStateFlow
could be hidden inside achildSlot
overload)Beta Was this translation helpful? Give feedback.
All reactions