Skip to content

Commit

Permalink
Clean up KoinApplication
Browse files Browse the repository at this point in the history
  • Loading branch information
arnaudgiuliani committed Jan 10, 2025
1 parent 38066ce commit bbb1698
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import org.koin.dsl.includes

@Composable
internal actual fun composeMultiplatformConfiguration(loggerLevel : Level, config : KoinApplication.() -> Unit) : KoinConfiguration {
val appContext = LocalContext.current.applicationContext ?: error("Android ApplicationContext not found!")
val appContext = LocalContext.current.applicationContext ?: error("Android ApplicationContext not found in current Compose context!")
return koinConfiguration {
androidContext(appContext)
androidLogger(loggerLevel)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ import org.koin.core.logger.Level
import org.koin.core.scope.Scope
import org.koin.dsl.KoinAppDeclaration
import org.koin.dsl.KoinConfiguration
import org.koin.dsl.koinApplication
import org.koin.mp.KoinPlatform
import org.koin.mp.KoinPlatformTools

Expand Down Expand Up @@ -105,9 +104,11 @@ private fun warningNoContext(ctx: Koin) {
}

/**
* Start a new Koin Application and associate it for Compose context
* if Koin's Default Context is already set,
* Start a new Koin Application context and setup Compose context
* if Koin's Default Context is already set, throw an error
*
* Note: KoinApplication is calling composeMultiplatformConfiguration to help prepare/anticipate context setup, and avoid to have different configuration in KMP app
* this function takes care to setup Android context (androidContext, androidLogger) for you
* @see composeMultiplatformConfiguration()
*
* @param application - Koin Application declaration lambda
Expand All @@ -121,12 +122,11 @@ private fun warningNoContext(ctx: Koin) {
@OptIn(KoinInternalApi::class)
@Composable
fun KoinApplication(
application: KoinAppDeclaration,
application: KoinAppDeclaration, //Better to directly use KoinConfiguration class
logLevel : Level = Level.INFO,
content: @Composable () -> Unit
) {
val configuration = composeMultiplatformConfiguration(logLevel, config = application)
val koin = rememberKoinApplication(koinApplication(configuration))
val koin = rememberKoinApplication(application,logLevel)
KoinContext(koin,content)
}

Expand All @@ -135,6 +135,7 @@ fun KoinApplication(
* - Help handle automatically Android Logger Anticipate Android context injection, to having to setup androidContext() and androidLogger
*/
@Composable
@PublishedApi
internal expect fun composeMultiplatformConfiguration(loggerLevel : Level = Level.INFO, config : KoinApplication.() -> Unit) : KoinConfiguration

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,12 @@ package org.koin.compose.application

import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import org.koin.compose.composeMultiplatformConfiguration
import org.koin.core.Koin
import org.koin.core.KoinApplication
import org.koin.core.annotation.KoinInternalApi
import org.koin.core.logger.Level
import org.koin.dsl.KoinAppDeclaration
import org.koin.dsl.koinApplication

/**
* Remember Koin Application to handle its closure if necessary
Expand All @@ -32,9 +35,10 @@ import org.koin.core.annotation.KoinInternalApi
*/
@OptIn(KoinInternalApi::class)
@Composable
inline fun rememberKoinApplication(koinApplication: KoinApplication): Koin {
val wrapper = remember {
CompositionKoinApplicationLoader(koinApplication)
inline fun rememberKoinApplication(noinline koinAppDeclaration: KoinAppDeclaration, logLevel: Level): Koin {
val configuration = composeMultiplatformConfiguration(logLevel, config = koinAppDeclaration)
val wrapper = remember(koinAppDeclaration,logLevel) {
CompositionKoinApplicationLoader(koinApplication(configuration))
}
return wrapper.koin ?: error("Koin context has not been initialized in rememberKoinApplication")
}

0 comments on commit bbb1698

Please sign in to comment.