burnoo
09/13/2021, 4:20 PMKoinContext 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:
@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)arnaud.giuliani
09/14/2021, 8:14 AMNacho Ruiz Martin
09/15/2021, 6:44 PMarnaud.giuliani
11/02/2021, 2:42 PMarnaud.giuliani
11/02/2021, 2:57 PMburnoo
11/02/2021, 5:20 PMio.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)arnaud.giuliani
11/02/2021, 5:40 PMnot-frequent releases)it’s because I’m mostly alone ^^ , and was making conf talks those last months
arnaud.giuliani
11/02/2021, 5:40 PMarnaud.giuliani
11/02/2021, 5:42 PMarnaud.giuliani
11/02/2021, 5:42 PM