Koin
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
Why Koin with PreCompose
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.
Setup
Add Dependency
Add the dependency in your common module’s commonMain sourceSet
api("moe.tlaster:precompose-koin:$precompose_version")
Usage
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) }