With Google adding KMP support to the AndroidX Jetpack ViewModels and DataStore, I was excited to try this on a Kotlin Multiplatform app using SwiftUI and Jetpack Compose on iOS and Android.
Philipp Lackner created a video focusing on Compose Multiplatform: How to Share ViewModels in Compose Multiplatform (with Dependency Injection), which I used as the inspiration for this demo app.
Arnaud Giuliani forked Philipp’s example and updated it to use koinViewModel, which is available from Koin 3.6.0-Beta4; see https://x.com/arnogiu/status/1791511039810638287.
I created a new project using the Kotlin Multiplatform Wizard and initially used the MainViewModel from Philipp’s example. Later, this became the DetailViewModel as I later wanted to explore how to hook up the lifecycle and needed to be able to navigate away from the View to implement the lifecycle on both platforms.
I used the Jetpack DataStore to implement a TimerRepository to save the timer value, which is as simple as an integer value; Room also has KMP support for more complex data scenarios.