Skip to content

Commit

Permalink
Avoid throwing exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
arnaudgiuliani committed Nov 28, 2023
1 parent 6b9fd6b commit e0b7df7
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,28 +25,32 @@ import androidx.compose.runtime.ProvidableCompositionLocal
import androidx.compose.runtime.compositionLocalOf
import androidx.compose.runtime.currentComposer
import androidx.compose.runtime.remember
import org.koin.compose.error.UnknownKoinContext
import org.koin.core.Koin
import org.koin.core.KoinApplication
import org.koin.core.annotation.KoinInternalApi
import org.koin.core.context.startKoin
import org.koin.core.error.ApplicationAlreadyStartedException
import org.koin.core.scope.Scope
import org.koin.dsl.KoinAppDeclaration
import org.koin.mp.KoinPlatform
import org.koin.mp.KoinPlatformTools

/**
* Current Koin Application context
*/
val LocalKoinApplication: ProvidableCompositionLocal<Koin> = compositionLocalOf {
throw UnknownKoinContext()
getDefaultKoinContext().apply {
warnNoContext()
}
}

/**
* Current Koin Scope
*/
val LocalKoinScope: ProvidableCompositionLocal<Scope> = compositionLocalOf {
throw UnknownKoinContext()
getDefaultKoinContext().apply {
warnNoContext()
}.scopeRegistry.rootScope
}

private fun getDefaultKoinContext() = KoinPlatformTools.defaultContext().get()
Expand All @@ -56,16 +60,9 @@ private fun getDefaultKoinContext() = KoinPlatformTools.defaultContext().get()
*
* @author @author jjkester
*/
@OptIn(InternalComposeApi::class)
@Composable
fun getKoin(): Koin = currentComposer.run {
try {
consume(LocalKoinApplication)
} catch (_: UnknownKoinContext) {
val ctx = getDefaultKoinContext()
ctx.warnNoContext()
ctx
}
return LocalKoinApplication.current
}

/**
Expand All @@ -74,16 +71,9 @@ fun getKoin(): Koin = currentComposer.run {
* @author @author jjkester
*
*/
@OptIn(InternalComposeApi::class)
@Composable
fun currentKoinScope(): Scope = currentComposer.run {
try {
consume(LocalKoinScope)
} catch (_: UnknownKoinContext) {
val ctx = getDefaultKoinContext()
ctx.warnNoContext()
getDefaultKoinContext().scopeRegistry.rootScope
}
return LocalKoinScope.current
}

/**
Expand All @@ -96,18 +86,13 @@ fun currentKoinScope(): Scope = currentComposer.run {
@Composable
fun rememberCurrentKoinScope(): Scope = currentComposer.run {
remember {
try {
consume(LocalKoinScope)
} catch (_: UnknownKoinContext) {
val ctx = getDefaultKoinContext()
ctx.warnNoContext()
getDefaultKoinContext().scopeRegistry.rootScope
}
consume(LocalKoinScope)
}
}

@OptIn(KoinInternalApi::class)
private fun Koin.warnNoContext() {
logger.debug("[Warning] - No Koin context defined in Compose, falling back with Default Koin Context.\nUse KoinContext(), KoinAndroidContext() or KoinApplication() function to setup or create Koin context with Compose and avoid such message.")
logger.info("[Warning] - No Koin context defined in Compose, fallback to default Koin context.\nUse KoinContext(), KoinAndroidContext() or KoinApplication() to setup or create Koin context with Compose and avoid such message.")
}

/**
Expand Down Expand Up @@ -143,7 +128,7 @@ fun KoinApplication(
}

/**
* Use Compose with existing Koin context, by default 'KoinPlatformTools.defaultContext()'
* Use Compose with current existing Koin context, by default 'KoinPlatform.getKoin()'
*
* @see KoinPlatformTools.defaultContext()
* @param content - following compose function
Expand All @@ -152,7 +137,7 @@ fun KoinApplication(
*/
@Composable
fun KoinContext(
context: Koin = KoinPlatformTools.defaultContext().get(),
context: Koin = KoinPlatform.getKoin(),
content: @Composable () -> Unit
) {
CompositionLocalProvider(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class MainActivity : ScopeActivity() {
assert(getKoin().getOrNull<SDKData>() == null)

setContent {
KoinContext {
KoinAndroidContext {
MaterialTheme {
App()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ class MainApplication : Application() {
super.onCreate()

startKoin {
androidLogger(Level.DEBUG)
// androidLogger(Level.DEBUG)
printLogger(Level.DEBUG)
androidContext(this@MainApplication)
modules(appModule)
}
Expand Down

0 comments on commit e0b7df7

Please sign in to comment.