Jacob Rhoda
11/01/2023, 6:07 PMPablichjenkov
11/01/2023, 6:12 PMMichael Langford
11/01/2023, 6:12 PMJacob Rhoda
11/01/2023, 6:19 PMMichael Langford
11/01/2023, 6:25 PMPablichjenkov
11/01/2023, 6:31 PMMichael Langford
11/01/2023, 6:34 PMPablichjenkov
11/01/2023, 6:35 PMMichael Langford
11/01/2023, 6:36 PMJacob Rhoda
11/01/2023, 6:39 PMMichael Langford
11/01/2023, 6:40 PMDarron Schall
11/01/2023, 6:40 PM@Observable
@ObservedObject
and the SwiftUI view pulls the uiState
from the model and responds accordingly (updating when the flow emits a new state).
At that point you're just writing SwiftUI like you normally would.Michael Langford
11/01/2023, 6:40 PMJacob Rhoda
11/01/2023, 6:42 PMMichael Langford
11/01/2023, 6:43 PMJacob Rhoda
11/01/2023, 7:08 PMMichael Langford
11/01/2023, 7:10 PMJacob Rhoda
11/01/2023, 7:15 PMMichael Langford
11/01/2023, 7:17 PMDarron Schall
11/01/2023, 7:32 PM@ObservedObject
not @Observable
.
There are various ways to achieve view model sharing. I suggest taking a look at the samples - https://www.jetbrains.com/help/kotlin-multiplatform-dev/multiplatform-samples.html - and finding the samples that share models / view models. Looking through the samples, you should be able to find an approach that resonates with you and your team.
Specifically for making a view model observable, you'll want a Swift extension that conforms your view model to the ObservableObject
protocol, and then a helper that receives the uiState flow and fires off objectWillChange.send()
. Rather than setting that all up by hand, I might suggest looking at a library that does it already, such as https://github.com/rickclephas/KMM-ViewModel