https://kotlinlang.org logo
#multiplatform
Title
# multiplatform
d

Daniele B

09/01/2020, 12:42 AM
Schermata 2020-09-01 alle 02.36.48.png
👍 8
Also added the ViewState into the diagram
t

Thomas Skovsgaard

09/01/2020, 6:27 AM
Hi Daniele, we are currently working on an KMP app for iOS and Android, on the iOS side using SwiftUI, we are using StateFlow, but I haven't figured out how to use our shared Kotlin viewmodels without a middle layer (SwiftUI Viewmodel) where I translate StateFlow to SwiftUI bindings (Observable/Published) Do you have any example of how this could be done in a better way ?
d

dazza5000

09/01/2020, 10:43 AM
Does anyone have a reference app/project that uses this suggested implementation?
j

JCollardBovy

09/01/2020, 12:43 PM
Hi Thomas, have you figured out how to interact with flows inside Swift code? I ended up having a middle layer for transforming flow into callback (inside Kotlin code) and then another one to adapt it to SwiftUI bindings. I'd love to at least get rid of the flow-to-callback layer
👆 1
t

Thomas Skovsgaard

09/01/2020, 5:52 PM
Hi Jonathan, no I have a single StateFlow extension in the Kotlin part and in my SwiftUI Viewmodel i need to listen to changes in the shared Kotlin code. A simple example:
Copy code
class SwiftUILoginViewModel: ObservableObject {
@Published var isLoading: Bool = false

init() {
StateFlowExtension().listen(sharedViewModel.isLoading, dispatcherService: dispatcher) { newValue in
      self.isLoading = newValue as? Bool ?? false
    }
}
}
It would be awesome if we could use swiftUI bindings with the shared Kotlin viewModel, but for now I haven't figured out how to do it other than this way.
d

Daniele B

09/01/2020, 7:08 PM
@Thomas Skovsgaard I still haven’t deepened into the iOS side. I hope JetBrains will start giving some guidance on how to use StateFlow on iOS/SwiftUI.
👍 2
5 Views