Replies: 3 comments 5 replies
-
|
This might help? https://gist.github.com/CXwudi/2c4084eb0e82c05fd943b040fc73c2f9#file-decomposevalueutil-kt-L72-L85 |
Beta Was this translation helpful? Give feedback.
-
|
Oops, seems like I missed this question. Sorry about that. import com.arkivanov.decompose.value.Value
import kotlinx.coroutines.flow.FlowCollector
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
fun <T : Any> Value<T>.asStateFlow(): StateFlow<T> =
ValueStateFlow(this)
private class ValueStateFlow<out T : Any>(
private val v: Value<T>,
) : StateFlow<T> {
override val value: T get() = v.value
override val replayCache: List<T> get() = listOf(v.value)
override suspend fun collect(collector: FlowCollector<T>): Nothing {
val stateFlow = MutableStateFlow(v.value)
val cancellation = v.subscribe { stateFlow.value = it }
try {
stateFlow.collect(collector)
} finally {
cancellation.cancel()
}
}
} |
Beta Was this translation helpful? Give feedback.
-
|
Oh I also completely forgot about this question. Well, as I said in the original post, I wanted to do this only because of the ObservableViewModel library. It turned out (perhaps predictably) that it's not a great idea. It basically broke down because the navigation is StateFlow of ViewModels which requires special handling anyway in the ObservableViewModel library. Besides, Decompose pretty much removes the need for the other library and generally makes the whole architecture much cleaner, so I ended up essentially replacing ObservableViewModel with Decompose and never looked back. After working with Decompose for a bit longer now, I actually pretty like having a Value separate from the StateFlow. I use Value exclusively for the component-view binding and state flow inside the apps logic to essentially cache the flows last state. I think this naming separation works pretty well. While on the subject though, there are 2 issues with Value that I find a little annoying, but they are not necessarily related to or solvable by using StateFlow.
Those are pretty much my only gripes with Value as it is. But the fact that the whole Decompose (not just Value) is so lightweight and easily extensible means that I can just implement the stuff I need myself and it's not an issue. Generally I think the library is pretty much a must on KMP and is very well designed. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
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
StateFlowinstead ofValuefor 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 localMutableStateFlowis ok.Usage:
(of course the
toStateFlowcould be hidden inside achildSlotoverload)Beta Was this translation helpful? Give feedback.
All reactions