Skip to content

Commit 3acd78e

Browse files
adjust check & error messages
1 parent f946362 commit 3acd78e

7 files changed

Lines changed: 23 additions & 14 deletions

File tree

projects/compose/koin-compose-viewmodel-navigation/src/commonMain/kotlin/org/koin/compose/scope/KoinNavigationScope.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ internal class CompositionKoinNavigationScopeLoader(
102102

103103
private fun close() {
104104
if (!scope.isRoot && !scope.closed){
105-
scope.logger.debug("CompositionKoinScopeLoader close scope: '${scope.id}'")
105+
scope.logger.debug("CompositionKoinNavigationScopeLoader close scope: '${scope.id}'")
106106
scope.close()
107107
}
108108
}

projects/compose/koin-compose-viewmodel-navigation/src/commonMain/kotlin/org/koin/compose/viewmodel/NavViewModel.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ import org.koin.viewmodel.resolveViewModel
4747
*/
4848
@OptIn(KoinInternalApi::class)
4949
@Deprecated(
50-
message = "koinNavViewModel is deprecated. Use koinViewModel instead.",
50+
message = "koinNavViewModel is deprecated. Use koinViewModel instead with the same parameters.",
5151
replaceWith = ReplaceWith(
52-
expression = "koinViewModel()",
52+
expression = "koinViewModel(qualifier, viewModelStoreOwner, key, extras, scope, parameters)",
5353
imports = ["org.koin.compose.viewmodel.koinViewModel"]
5454
)
5555
)

projects/compose/koin-compose-viewmodel/src/androidMain/kotlin/org/koin/compose/viewmodel/ViewModelExt.kt

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,18 @@ import org.koin.core.qualifier.Qualifier
2626
import org.koin.core.scope.Scope
2727

2828
/**
29-
* Resolve ViewModel instance
29+
* Resolves a [ViewModel] instance scoped to the current Activity.
3030
*
31-
* @param qualifier
32-
* @param parameters
31+
* This function retrieves a ViewModel using the current [ComponentActivity] as the ViewModelStoreOwner,
32+
* ensuring the ViewModel is scoped to the Activity's lifecycle.
33+
*
34+
* @param T The type of ViewModel to resolve.
35+
* @param qualifier Optional [Qualifier] to distinguish between multiple definitions of the same type.
36+
* @param key Optional key to identify the ViewModel instance.
37+
* @param scope The Koin [Scope] to resolve from. Defaults to the current Koin scope.
38+
* @param parameters Optional [ParametersDefinition] to pass dynamic parameters to the ViewModel.
39+
* @return The resolved ViewModel instance of type [T].
40+
* @throws IllegalStateException if the current Activity is not a [ComponentActivity].
3341
*
3442
* @author Arnaud Giuliani
3543
*/
@@ -41,7 +49,7 @@ inline fun <reified T : ViewModel> koinActivityViewModel(
4149
noinline parameters: ParametersDefinition? = null,
4250
): T = koinViewModel<T>(
4351
qualifier = qualifier,
44-
viewModelStoreOwner = LocalActivity.current as ComponentActivity,
52+
viewModelStoreOwner = LocalActivity.current as? ComponentActivity ?: error("LocalActivity is not a ComponentActivity"),
4553
key = key,
4654
scope = scope,
4755
parameters = parameters,

projects/compose/koin-compose/src/androidMain/kotlin/org/koin/compose/activity/KoinActivityInject.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ import org.koin.core.scope.Scope
4343
@Composable
4444
inline fun <reified T> koinActivityInject(
4545
qualifier: Qualifier? = null,
46-
scope: Scope = ((LocalActivity.current as ComponentActivity) as? AndroidScopeComponent)?.scope ?: error("Activity is not an AndroidScopeComponent"),
46+
scope: Scope = (LocalActivity.current as? AndroidScopeComponent)?.scope ?: error("Activity is not an AndroidScopeComponent. Make your activity implement AndroidScopeComponent to use koinActivityInject"),
4747
noinline parameters: ParametersDefinition? = null,
4848
): T {
4949
val p = parameters?.invoke()

projects/compose/koin-compose/src/commonMain/kotlin/org/koin/compose/KoinApplication.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,15 @@ import org.koin.mp.KoinPlatformTools
4545
*/
4646
//For Compat with 4.1.0
4747
@Deprecated("LocalKoinScope has been replaced with LocalKoinScopeContext, using ComposeContextWrapper.getValue() to retrieve the value. See also KoinScope() or UnboundKoinScope() Compose functions" , level = DeprecationLevel.ERROR ,replaceWith = ReplaceWith("LocalKoinScopeContext"))
48-
val LocalKoinScope: ProvidableCompositionLocal<Scope> = compositionLocalOf { error(" should not be used in favor of LocalKoinScopeContext") }
48+
val LocalKoinScope: ProvidableCompositionLocal<Scope> = compositionLocalOf { error("should not be used in favor of LocalKoinScopeContext") }
4949

5050
/**
5151
* Deprecated - Internal API
5252
* Current Koin Application context, as default with Default Koin context
5353
*/
5454
//For Compat with 4.1.0
5555
@Deprecated("LocalKoinApplication is deprecated. Use getKoin() to access the Koin instance directly.", level = DeprecationLevel.ERROR, replaceWith = ReplaceWith("getKoin()"))
56-
val LocalKoinApplication : ProvidableCompositionLocal<Koin> = compositionLocalOf { error(" should not be used in favor of getKoin()") }
56+
val LocalKoinApplication : ProvidableCompositionLocal<Koin> = compositionLocalOf { error("should not be used in favor of getKoin()") }
5757

5858
/**
5959
* Internal API
@@ -82,7 +82,7 @@ private fun getDefaultRootScope() = KoinPlatform.getKoin().scopeRegistry.rootSco
8282
/**
8383
* Retrieve the current Koin application from the composition.
8484
*
85-
* @author @author jjkester
85+
* @author jjkester
8686
*/
8787
@OptIn(InternalComposeApi::class, KoinInternalApi::class)
8888
@Composable
@@ -98,7 +98,7 @@ fun getKoin(): Koin = currentComposer.run {
9898
/**
9999
* Retrieve the current Koin scope from the composition
100100
*
101-
* @author @author jjkester
101+
* @author jjkester
102102
*
103103
*/
104104
@OptIn(InternalComposeApi::class, KoinInternalApi::class)

projects/compose/koin-compose/src/commonMain/kotlin/org/koin/compose/application/CompositionKoinApplicationLoader.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class CompositionKoinApplicationLoader(
4646
koin = KoinPlatform.getKoin()
4747
koin!!.logger.debug("$this -> re-attach Koin instance $koin")
4848
} else {
49-
error("can't start Koin context, no koinApplication argument found nor existing context")
49+
error("Can't start Koin context, no koinApplication argument found nor existing context")
5050
}
5151
}
5252

projects/compose/koin-compose/src/nativeMain/kotlin/org/koin/compose/KoinApplication.native.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@ package org.koin.compose
22

33
import androidx.compose.runtime.Composable
44
import org.koin.core.Koin
5-
import org.koin.core.KoinApplication
5+
import org.koin.core.annotation.KoinExperimentalAPI
66
import org.koin.dsl.KoinConfiguration
77
import org.koin.core.logger.Level
88
import org.koin.dsl.koinConfiguration
99
import org.koin.dsl.includes
1010
import org.koin.mp.KoinPlatform
1111

1212
@Composable
13+
@KoinExperimentalAPI
1314
internal actual fun composeMultiplatformConfiguration(loggerLevel : Level, config : KoinConfiguration) : KoinConfiguration {
1415
return koinConfiguration {
1516
printLogger(loggerLevel)

0 commit comments

Comments
 (0)