Skip to content

Commit c4a5dcb

Browse files
4.2.0-beta4-rc2 - adjust KoinApplication.withConfiguration<T>() support
1 parent 9ece681 commit c4a5dcb

3 files changed

Lines changed: 137 additions & 3 deletions

File tree

docs/reference/koin-compose/compose.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,53 @@ You can use parameters with lambda injection like `koinInject<MyService>{ parame
124124
From version 4.0.2 of Koin, koinInject(Qualifier,Scope,ParametersHolder) is introduced to let you use parameters in the most efficient way
125125
:::
126126

127+
### Injecting from Activity Scope - koinActivityInject (Android only)
128+
129+
:::info
130+
This is an **Android-only** feature available in `koin-compose` starting from version 4.2.0.
131+
:::
132+
133+
The `koinActivityInject()` function allows you to resolve dependencies scoped to the current Activity from within a Composable. This is useful when you need to share state or dependencies across multiple Composables within the same Activity.
134+
135+
**Requirements:**
136+
- Your Activity must implement `AndroidScopeComponent` to provide its Koin scope
137+
- The dependency must be declared in an Activity scope
138+
139+
```kotlin
140+
// Define scoped dependencies
141+
val appModule = module {
142+
scope<MainActivity> {
143+
scoped { StateHolder() }
144+
}
145+
}
146+
147+
// Activity implementing AndroidScopeComponent
148+
class MainActivity : ComponentActivity(), AndroidScopeComponent {
149+
override val scope: Scope by activityScope()
150+
151+
// ...
152+
}
153+
154+
// Use in any Composable within the Activity
155+
@Composable
156+
fun MyScreen() {
157+
val stateHolder: StateHolder = koinActivityInject()
158+
// Use stateHolder...
159+
}
160+
```
161+
162+
The resolved instance is remembered across recompositions. You can also pass optional parameters:
163+
164+
```kotlin
165+
@Composable
166+
fun MyScreen() {
167+
val stateHolder: StateHolder = koinActivityInject(
168+
qualifier = named("myQualifier"),
169+
parameters = { parametersOf("param1") }
170+
)
171+
}
172+
```
173+
127174
## ViewModel for @Composable
128175

129176
The same way you have access to classical single/factory instances, you gain access to the following Koin ViewModel API:

projects/core/koin-core/src/commonMain/kotlin/org/koin/plugin/module/dsl/ApplicationDSLExt.kt

Lines changed: 89 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
package org.koin.plugin.module.dsl
22

3+
import org.koin.core.KoinApplication
34
import org.koin.core.module.Module
5+
import org.koin.dsl.KoinConfiguration
6+
import org.koin.dsl.includes
47
import org.koin.dsl.koinApplication
8+
import org.koin.dsl.koinConfiguration
59
import org.koin.mp.KoinPlatformTools
610

711
/**
@@ -14,7 +18,7 @@ import org.koin.mp.KoinPlatformTools
1418
* @KoinApplication
1519
* object MyApp
1620
*
17-
* startKoin(MyApp::class) {
21+
* startKoin<MyApp>() {
1822
* printLogger()
1923
* }
2024
* // Plugin transforms to:
@@ -29,6 +33,14 @@ public fun <T : Any> startKoin(appDeclaration: org.koin.dsl.KoinAppDeclaration?
2933
USE_KOIN_COMPILER_PLUGIN("startKoin<T>()")
3034
}
3135

36+
/**
37+
* Compiler Plugin Support Function - Start Koin with the provided list of modules.
38+
* This function is called by the compiler plugin after transforming startKoin<T>().
39+
*
40+
* @param modules List of modules discovered from @KoinApplication annotated class
41+
* @param appDeclaration Optional Koin application configuration block
42+
* @return The configured KoinApplication instance
43+
*/
3244
public fun startKoinWith(modules : List<Module>, appDeclaration: org.koin.dsl.KoinAppDeclaration? = null): org.koin.core.KoinApplication {
3345
return KoinPlatformTools.defaultContext().startKoin {
3446
modules(modules)
@@ -42,7 +54,7 @@ public fun startKoinWith(modules : List<Module>, appDeclaration: org.koin.dsl.Ko
4254
*
4355
* Usage:
4456
* ```kotlin
45-
* koinApplication(MyApp::class) {
57+
* koinApplication<MyApp>() {
4658
* printLogger()
4759
* }
4860
* ```
@@ -52,9 +64,84 @@ public fun <T : Any> koinApplication(appDeclaration: org.koin.dsl.KoinAppDeclara
5264
USE_KOIN_COMPILER_PLUGIN("koinApplication<T>()")
5365
}
5466

67+
/**
68+
* Compiler Plugin Support Function - Create KoinApplication with the provided list of modules.
69+
* This function is called by the compiler plugin after transforming koinApplication<T>().
70+
*
71+
* @param modules List of modules discovered from @KoinApplication annotated class
72+
* @param appDeclaration Optional Koin application configuration block
73+
* @return The configured KoinApplication instance (not registered globally)
74+
*/
5575
public fun koinApplicationWith(modules : List<Module>, appDeclaration: org.koin.dsl.KoinAppDeclaration? = null): org.koin.core.KoinApplication {
5676
return koinApplication {
5777
modules(modules)
5878
if (appDeclaration != null) appDeclaration()
5979
}
80+
}
81+
82+
83+
/**
84+
* Compiler Plugin Stub - Create KoinConfiguration with modules discovered from @KoinApplication annotated class.
85+
* The compiler plugin transforms this call to inject modules based on the @Configuration tags associated with type parameter T.
86+
*
87+
* Usage:
88+
* ```kotlin
89+
* koinConfiguration<MyApp>() {
90+
* printLogger()
91+
* }
92+
* ```
93+
*
94+
* @param T Type annotated with @KoinApplication
95+
* @param appDeclaration Optional Koin application configuration block
96+
* @return KoinConfiguration with discovered modules
97+
*/
98+
public fun <T : Any> koinConfiguration(appDeclaration: org.koin.dsl.KoinAppDeclaration? = null) : KoinConfiguration {
99+
// Generate Call: koinConfigurationWith(T::class().modules)
100+
USE_KOIN_COMPILER_PLUGIN("koinConfiguration<T>()")
101+
}
102+
103+
104+
/**
105+
* Compiler Plugin Support Function - Create KoinConfiguration with the provided list of modules.
106+
* This function is called by the compiler plugin after transforming koinConfiguration<T>().
107+
*
108+
* @param modules List of modules discovered from @KoinApplication annotated class
109+
* @param appDeclaration Optional Koin application configuration block
110+
* @return KoinConfiguration with provided modules
111+
*/
112+
public fun koinConfigurationWith(modules : List<Module>, appDeclaration: org.koin.dsl.KoinAppDeclaration? = null) : KoinConfiguration = koinConfiguration {
113+
includes(appDeclaration)
114+
modules(modules)
115+
}
116+
117+
118+
/**
119+
* Compiler Plugin Stub - Apply configuration with modules discovered from @KoinApplication annotated class to an existing KoinApplication.
120+
* The compiler plugin transforms this call to inject modules based on the @Configuration tags associated with type parameter T.
121+
*
122+
* Usage:
123+
* ```kotlin
124+
* startKoin {
125+
* printLogger()
126+
* }.withConfiguration<MyApp>()
127+
* ```
128+
*
129+
* @param T Type annotated with @KoinApplication
130+
* @param appDeclaration Optional Koin application configuration block
131+
*/
132+
public fun <T : Any> KoinApplication.withConfiguration(appDeclaration: org.koin.dsl.KoinAppDeclaration? = null) {
133+
// Generate Call: koinConfigurationWith(T::class().modules)
134+
USE_KOIN_COMPILER_PLUGIN("KoinApplication.useKoinConfiguration<T>()")
135+
}
136+
137+
/**
138+
* Compiler Plugin Support Function - Apply configuration with the provided list of modules to an existing KoinApplication.
139+
* This function is called by the compiler plugin after transforming withConfiguration<T>().
140+
*
141+
* @param modules List of modules discovered from @KoinApplication annotated class
142+
* @param appDeclaration Optional Koin application configuration block
143+
*/
144+
public fun KoinApplication.withConfigurationWith(modules : List<Module>, appDeclaration: org.koin.dsl.KoinAppDeclaration? = null) {
145+
includes(appDeclaration)
146+
modules(modules)
60147
}

projects/gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ kotlin.incremental.multiplatform=true
1111
kotlin.code.style=official
1212

1313
#Koin
14-
koinVersion=4.2.0-beta3
14+
koinVersion=4.2.0-beta4-rc2
1515

1616
#Compose
1717
org.jetbrains.compose.experimental.jscanvas.enabled=true

0 commit comments

Comments
 (0)