Hey guys, so I'm very new to SwiftUI and not sure ...
# ios
a
Hey guys, so I'm very new to SwiftUI and not sure how to make it work with Kotlin flows. I've created all my screens with compose multiplatform with the viewmodels shared as well, all my viewmodels have shared flows for actions and state flows for state 🧵
for example my view model would look like this:
Copy code
class MyViewModel {
    val state: MutableStateFlow<...> = ...
    val action: MutableSharedFlow<...> = ...
}
and my shared ui would look like this:
Copy code
@Composable
fun MyUI(
    state: StateData,
    onStateChange: (StateData) -> Unit,
    performAction: (ActionData) -> Unit,
    modifier: Modifier = Modifier
)
to use that with SwiftUI I wrap
MyUI
in a
ComposeUIViewController
which's wrapped inside a
UIViewControllerRepresentable
which enables me to use it as a SwiftUI view, so I have a few questions: 1. where's the best place to create my viewmodel? is it directly inside the swiftui view as follows?
Copy code
struct MySwiftView: View {
    let model = MyViewModel(...)
    var body: some View {
        MyComposeView()
    }
}
2. how do I collect the state flow to use with swiftui, I don't expect it to react to changes in
flow.value
out of the box? does it? 3. where do I collect my action flow? on Android I would just collect the action flow in a LaunchedEffect with a Unit key. My current candidate is
onAppear
but i'm not sure about it. my action flows mostly contain navigation actions like navigating up and navigating to the next screen which i'll be using with navigation links with tags and selections. Thank you!
j
t
You might also like SKIE (https://github.com/touchlab/SKIE) that exposes Flow types as AsyncSequence so you can use them from Swift directly 😊
👍 1