Replies: 2 comments
-
This might help? https://gist.github.com/CXwudi/2c4084eb0e82c05fd943b040fc73c2f9#file-decomposevalueutil-kt-L72-L85 |
Beta Was this translation helpful? Give feedback.
0 replies
-
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.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
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