Koin is a popular dependency injection framework for Kotlin, and it's also a Kotlin Multiplatform project. For more information: https://github.com/InsertKoinIO/koin
When using PreCompose ViewModel with Koin, you do have to manage ViewModel's lifecycle, a simple 'inject()' from Koin does not handle the ViewModel's lifecycle.
Add the dependency in your common module's commonMain sourceSet
api("moe.tlaster:precompose-koin:$precompose_version")
First you can define your ViewModel in Koin module like this:
factory { (initialCount: Int) ->
CounterViewModel(
initialCount = initialCount,
counterRepository = get(),
)
}
And you can use your ViewModel in Compose like this:
val viewModel = koinViewModel<CounterViewModel> { parametersOf(initialCount) }
NOTE: If you're using Kotlin/Native target, please use viewModel with vmClass parameter instead.
val viewModel = koinViewModel(vmClass = CounterViewModel::class) { parametersOf(initialCount) }