Does anyone have a good article/repo/idea of how t...
# touchlab-tools
a
Does anyone have a good article/repo/idea of how to best share a ViewModel of sorts from KMP to iOS when using SKIE? I've looked at KAMPKIT. Would like to have a solution where a viewmodel can be "wrapped" simply and be given a coroutinescope etc.
f
Not really an article but some idea here
a
Thank you, that's great, will have a look at that!
c
@John O'Reilly Has several example apps. A few of them shares ViewModels between swiftUI and Compose clients:

https://www.youtube.com/watch?v=v9d436ByRkw&ab_channel=droidconLisbon

👍 1
a
I've seen the most common libraries, but since we are using SKIE to solve flows and coroutines I figured we could have some relatively thin custom solution/pattern without needing another lib. Although the solution in kampkit works, I'd prefer to have pattern where the ios side doesn't need to explicitly call a non-returning coroutine and cancel/cleanup method manually for each view.
👍 1
I've solved most of it but couldn't get the deinit part right where the view calls kmp to cancel the viewModelScope when it goes out of view
f
You have the answer in my playground for the
deinit/onCleared
of the ViewModel.
a
Btw, I am trying to solve it on Swift 6 to appease the ios devs. I have something like this,
f
Good luck with Swift 6. currently KMP t’s not really friendly with it
a
ah ok. Maybe that's not feasible then and I should try more on 5.x
f
Stick on Swift 5 until it more official, the current generated ObjC is not valid with Swift6 (I saw your @preconcurrency, it’s solving the @Sendable issue but not the concurrency completely)
a
Yeah, spot on, its is blocking me from accessing the viewModel reference on deinit.
f
There are not many ways to clean up correctly the KMP ViewModel
The dirty way is to cancel manually the viewmodelscope and call manually
onCleared()
method, but some stuff is missing like callback set on init.
The cleanest way, using
ViewModelStore
on the swift side and synch with the lifecycle of the SwiftUI View.
j
Away from computer but think using that approach in one of the samples
Think it's FantasyPremierLeague one
f
@John O'Reilly Yes, we shared an interesting discussion, remember 😄 ?
it’s even linked in my playground
j
Or at least had started exploring approach there
a
pardon my ignorance, where's ViewModelStore coming from in your swift code? Can't seem to find what that is
f
My playground 😄
a
Right, but I can only see usages, not the definition
f
The ViewModelStore come from your shared library, we ask the compiler to
export
the class to the ObjC header, so it will be accessible in your iOS app.
The same ViewModelStore as you’re using on the Android Side
a
ohhh. ok. so it's in combination with jetpack viewmodel
f
The view model you’re using should be
androidx.lifecycle:lifecycle-viewmodel
?
a
That's a bit too experimental for my liking for now. Would settle with a custom viewmodel class, although it can inherit from androidx viewmodel on android via expect/actual.
f
androidx.lifecycle:lifecycle-viewmodel
is officially KMP since last year and not considered as experimental
a
Maybe i'm mixing it up with compose multiplatform viewmodel
f
the CMP one is
org.jetbrains.androidx.lifecycle:lifecycle-viewmodel-compose
the Android/KMP one is
androidx.lifecycle:lifecycle-viewmodel
a
Thanks for clarifying that. I can see how using ViewModelStore seems cleaner, although .put(ViewModel) isn't a public api (?). I guess you're thinking of the way the ViewModelStore calls onCleared to clean up the ViewModel?
f
I guess you’re thinking of the way the ViewModelStore calls onCleared to clean up the ViewModel?
Yes and other properties like the viemodelscope, callback ... We simply reproduce what it’s done on Android when using the ViewModel + Navigation. The difference here : 1 ViewModelStore = 1 SwiftUI screen.
a
I can see now that https://github.com/joreilly/FantasyPremierLeague/blob/main/ios/FantasyPremierLeague/FantasyPremierLeague/SharedViewModelStoreOwner.swift is doing pretty much this, except for forcing a single stateflow and collecting/putting it in a new @State. That's interesting. I also read your issue linked in the playground. Thank you for the help, I'll experiment more with this with my iOS dev next week.
🚀 1
f
Thanks, I’m looking for usage of the KMP ViewModel into Swift App 😄, I have proposed different ways in my playground. Still looking for more.