Skip to content

Commit

Permalink
Merge pull request #1723 from InsertKoinIO/fix/compose_api_remember_u…
Browse files Browse the repository at this point in the history
…sage

Fix remember usage when retrieving Koin context
  • Loading branch information
arnaudgiuliani authored Nov 28, 2023
2 parents 56bb9a9 + e0b7df7 commit 1e14e0d
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ inline fun <reified T> koinInject(
scope: Scope = currentKoinScope(),
noinline parameters: ParametersDefinition? = null,
): T {
val st = rememberStableParametersDefinition(parameters)
val st = parameters?.let { rememberStableParametersDefinition(parameters) }
return remember(qualifier, scope) {
scope.get(qualifier, st.parametersDefinition)
scope.get(qualifier, st?.parametersDefinition)
}
}

Expand All @@ -51,13 +51,14 @@ inline fun <reified T> koinInject(
* @author Arnaud Giuliani
*/
@Composable
@Deprecated("")
inline fun <reified T> rememberKoinInject(
qualifier: Qualifier? = null,
scope: Scope = rememberCurrentKoinScope(),
noinline parameters: ParametersDefinition? = null,
): T {
val st = rememberStableParametersDefinition(parameters)
val st = parameters?.let { rememberStableParametersDefinition(parameters) }
return remember(qualifier, scope) {
scope.get(qualifier, st.parametersDefinition)
scope.get(qualifier, st?.parametersDefinition)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,49 +25,44 @@ 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 getKoinContext() = KoinPlatformTools.defaultContext().get()
private fun getDefaultKoinContext() = KoinPlatformTools.defaultContext().get()

/**
* Retrieve the current Koin application from the composition.
*
* @author @author jjkester
*/
@OptIn(InternalComposeApi::class)
@Composable
fun getKoin(): Koin = currentComposer.run {
remember {
try {
consume(LocalKoinApplication)
} catch (_: UnknownKoinContext) {
val ctx = getKoinContext()
ctx.warnNoContext()
ctx
}
}
return LocalKoinApplication.current
}

/**
Expand All @@ -76,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 = getKoinContext()
ctx.warnNoContext()
getKoinContext().scopeRegistry.rootScope
}
return LocalKoinScope.current
}

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

@OptIn(KoinInternalApi::class)
private fun Koin.warnNoContext() {
logger.debug("[Warning] - No Compose Koin context setup, taking default. Use KoinContext(), KoinAndroidContext() or KoinApplication() function to setup or create Koin context 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 All @@ -130,10 +113,9 @@ fun KoinApplication(
) {
val koinApplication = remember(application) {
val alreadyExists = KoinPlatformTools.defaultContext().getOrNull() != null
if (alreadyExists){
if (alreadyExists) {
throw ApplicationAlreadyStartedException("Trying to run new Koin Application whereas Koin is already started. Use 'KoinContext()' instead of check for any 'startKoin' usage. ")
}
else {
} else {
startKoin(application)
}
}
Expand All @@ -146,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 @@ -155,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
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import androidx.compose.ui.window.Window
import androidx.compose.ui.window.application
import org.koin.compose.KoinApplication
import org.koin.compose.koinInject
import org.koin.core.logger.Level
import org.koin.dsl.module
import java.util.UUID

Expand All @@ -30,6 +31,7 @@ fun App() {
KoinApplication(
application = {
modules(mod)
printLogger(Level.DEBUG)
}
) {
var text by remember { mutableStateOf("Hello, World!") }
Expand Down

0 comments on commit 1e14e0d

Please sign in to comment.