Skip to content

Multiplatform ViewModel#683

Open
ElviraMustafina wants to merge 6 commits intomasterfrom
viewmodel-guide
Open

Multiplatform ViewModel#683
ElviraMustafina wants to merge 6 commits intomasterfrom
viewmodel-guide

Conversation

@ElviraMustafina
Copy link
Copy Markdown
Collaborator

No description provided.

@ElviraMustafina ElviraMustafina requested a review from a team as a code owner May 6, 2026 12:49
Copy link
Copy Markdown
Collaborator

@zamulla zamulla left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a big one. I left surface-level comments generally, my main problem is that there is no ready-to-go sample, and applying example code to nav_cupcake was not straightforward for me — although it did work in the end.

androidx-lifecycle-viewmodel-compose = { module = "org.jetbrains.androidx.lifecycle:lifecycle-viewmodel-compose", version.ref = "androidx-viewmodel" }
androidx-lifecycle-viewmodel-navigation3 = { module = "androidx.lifecycle:lifecycle-viewmodel-navigation3", version.ref = "androidx-viewmodel" }
```
> You can track changes to the multiplatform ViewModel implementation in our [What's new](https://www.jetbrains.com/help/kotlin-multiplatform-dev/whats-new-compose.html)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be an empty line after the code block.

```
{initial-collapse-state="collapsed" collapsible="true" collapsed-title="implementation(libs.androidx.lifecycle.viewmodel.compose)"}

3. If you have a desktop target, add the `kotlinx-coroutines-swing` dependency as well.
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this is the last list item, I would make it not a list item.

If you have a desktop target <...>:

  1. In the Gradle version catalog:
  2. In the build.gradle.kts:

This way the structure also mirrors adding the dependency to commonMain earlier.


Compose Multiplatform implements the common `ViewModelStoreOwner` interface, so in general using the `ViewModel` class
Compose Multiplatform provides a common `ViewModelStoreOwner` implementation, so using the `ViewModel` class
in common code is not much different from Android best practices.
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should probably link the Android best practices for those who start with ViewModels here.

```

> This example uses explicit backing fields, stabilized in Kotlin 2.4.0. When using earlier versions,
> add the `-Xexplicit-backing-fields` compiler option or use the old backing fields pattern with `.asStateFlow()` instead.
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still feel it's useful to link the explanation here https://kotlinlang.org/docs/whatsnew23.html#explicit-backing-fields


A dependency injection (DI) framework allows you to inject different dependencies into components
based on the current environment or target platform.
To manage ViewModels, you can use Koin, Metro, or another DI framework that supports Kotlin Multiplatform.
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
To manage ViewModels, you can use Koin, Metro, or another DI framework that supports Kotlin Multiplatform.
To manage ViewModels, you can use Koin, Metro, or any other DI framework that supports Kotlin Multiplatform.

In this approach, the `ViewModel` (business logic) is shared, but platforms have native UI implementations.
Learn more in [Set up ViewModel for Kotlin Multiplatform](https://developer.android.com/kotlin/multiplatform/viewmodel).

1. Since the UI is not shared in this case, you can switch from the Compose Multiplatform version of the ViewModel library to the `androidx.lifecycle` library.
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exactly since the UI is not shared, I would separate the Android instructions from iOS, because they can be implemented independently. So, #### Android and #### iOS sections. This also gets rid of the nested list for iOS.

Let's define a ViewModel and wire it into a composable:

1. Define an `OrderViewModel` class,
based on the [navigation sample](https://github.com/JetBrains/compose-multiplatform/tree/0e38f58b42d23ff6d0ad30b119d34fa1cd6ccedb/examples/nav_cupcake):
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The sample is a bit on the older side now, and didn't let me smoothly follow the instructions after simply checking out and importing it (if that's what's implied). Ideally we update it to use the backing fields and newer dependencies so that there's less friction on the way to making it work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants