What is the official recommended way to keep viewm...
# multiplatform
h
What is the official recommended way to keep viewmodels? common code or platform specific(iosApp,androidApp) ?
l
There’s several ways to keep view models common. You can use moko-mvvm, stately, decompose, or others depending on what you need.
h
@Landry Norris so there's no official guide about this?
c
@Hasan Nagizade not really... KMM's pages likes to encourages shared business logic code and core. I have a personal preference that presentation logic must be in their own platform for now since I can use the full power of each without depending on "beta" stuff. Plus the communication between Swift <> Coroutines is way ugly and can pollute your codebase, so having your own iOS/Android VM will help in abstracting your shared code. The downside of having separate presentation logic is indeed the duplicated code.
I haven't tried it to have a shared VM but I have the feeling it can speed up the development, you can checkout the droidcon app for a pure shared VM code. I feel this one is a good source and I guess they don't use external libraries. 😄 Answering your question... yeah, no guidance at all.
h
@Corey thank you very much for explanation.Right now my viewmodels are just kotlin classes used as KoinComponents in common code and initialised via Koin. I guess I will have to move them to androidApp and use Android's Viewmodel with it's pros and cons. I am still learning iOS so iOSApp part is empty for now
Thanks for the repository example too
c
@Hasan Nagizade You can keep your VM in the common, if you want to use the Androidx ViewModel, then you can use
expect
and
actual
Since it is only Android for now, you can leave the iOS part empty. I'm doing the same, although I don't have a common VM, I've started with iOS only. Android is fully empty. 😄 😄
h
@Corey what a nice coincidence. 😄 I hope you will like Jetpack Compose when you move to this side. 😁
s
Common VMs work great assuming you're strict with your architecture, been using them with no issues
d
What is the official recommended way to keep viewmodels?
There's no official recommendation on this, and nor should there be: Your UI architecture is Application specific and this is out-of-scope of Kotlin Multiplatform. KMP is there to support code-sharing, whichever UI architecture you adopt. Generally though, if you're not attempting to share any of your presentation logic at all, then IMO you're missing out on the full benefit of Kotlin Multiplatform. Most of the time (KMM scenario) the View layer requirements are similar enough that it's straightforward to achieve a common (MVVM) ViewModel. Even where the UI differs greatly - for example, where in one of my projects I have a CLI client - you can still use KMP's 'hierarchical source-sets' to achieve e.g.
graphicalMain
that provides common VM sources for `ios`/`android` graphical targets, while the
cli
presentation layer can be separate and different.