Hey! I’ve been working with Koin for Compose for a...
# koin-contributors
b
Hey! I’ve been working with Koin for Compose for a while now and have a proposal to improve how
KoinContext
management works in
@Composable
functions. I believe the configuration of the Application should be done in the top level composable. This way it is not dependent on Koin initialization in the application class, which allows for easy
KoinContext
replacement in
@Preview
composables and tests. It introduces a new API -
Koin
and
KoinScope
composables:
Copy code
@Composable
fun Koin(
    appDeclaration: KoinAppDeclaration? = null,
    content: @Composable () -> Unit
) {
    val koinApplication = koinApplication(appDeclaration)
    CompositionLocalProvider(LocalKoin provides koinApplication.koin) {
        content()
    }
}

@Composable
fun KoinScope(
    getScope: Koin.() -> Scope,
    content: @Composable () -> Unit
) {
    val koin = getKoin()
    val scope = remember {
        koin.getScope()
    }
    CompositionLocalProvider(LocalScope provides scope) {
        content()
    }
}
I’ve opened PR here with implementation and instrumented tests: https://github.com/InsertKoinIO/koin/pull/1178 On the screenshot there is an example how to use new API with Scope and Preview. Pros: - scopes works - easy
KoinContext
replacement in tests - easy
KoinContext
replacement in
@Preview
composables - no need to use application class (which may help with Compose Multiplatform support in the future) - backward compatibility (using GlobalContext as default value)
❤️ 2
a
Super interesting, let me check that 👍
❤️ 1
n
Looks great 💪
a
Hey @burnoo, do you propose a new project inside Koin family? I understand that you push to have more faster updates
I’ve seen that you forked out ...
b
I’ve created library (https://github.com/burnoo/cokoin), which includes changes proposed in the PR. I wanted to create library for Compose Multiplatform with frequent releases and separate artifacts for ViewModel and Navigation support. I think it doesn’t fit the current artifact
io.insert-koin:koin-androidx-compose
(which works only with Jetpack, and is bound to Koin not-frequent releases). I was wondering what could be done to best support Koin for Compose - to have both multiplatform frequent releases and community support. I came up with the idea that my library could be moved to insert-koin.io on GitHub and Maven Central. I would like to support this library and develop it together with the community. (I wrote about the benefits in this comment)
a
not-frequent releases)
it’s because I’m mostly alone ^^ , and was making conf talks those last months
if we have content, we can ship regularly that’s not a problem
the other thing is also to be sure shipped hypothesis are good.
Do you want to split compose and compose ViewModel ?