Skip to content

Latest commit

 

History

History
35 lines (29 loc) · 1.25 KB

koin.md

File metadata and controls

35 lines (29 loc) · 1.25 KB

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

Maven Central

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) }