Skip to content

Commit 8094a29

Browse files
committed
fix: KoinPreview needs to use KoinApplicationPreview
1 parent c274250 commit 8094a29

File tree

1 file changed

+20
-8
lines changed
  • core/ui/compose/common/src/main/kotlin/app/k9mail/core/ui/compose/common/koin

1 file changed

+20
-8
lines changed

core/ui/compose/common/src/main/kotlin/app/k9mail/core/ui/compose/common/koin/KoinPreview.kt

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,38 @@
11
package app.k9mail.core.ui.compose.common.koin
22

33
import androidx.compose.runtime.Composable
4-
import org.koin.compose.KoinContext
5-
import org.koin.core.Koin
4+
import org.koin.compose.KoinApplicationPreview
5+
import org.koin.core.module.Module
66
import org.koin.dsl.ModuleDeclaration
77
import org.koin.dsl.module
88

99
/**
1010
* Helper to make Compose previews work when some dependencies are injected via Koin.
11+
*
12+
* Usage:
13+
* ```kotlin
14+
* @Composable
15+
* @Preview()
16+
* fun SomeComposablePreview() {
17+
* koinPreview {
18+
* // Declare Koin modules here
19+
* factory { SomeDependency() }
20+
* } WithContent {
21+
* // Your preview content here
22+
* }
23+
* }
24+
* ```
1125
*/
1226
fun koinPreview(moduleDeclaration: ModuleDeclaration): KoinPreview {
13-
val koin = Koin().apply {
14-
loadModules(listOf(module(moduleDeclaration = moduleDeclaration)))
15-
}
27+
val modules = listOf(module(moduleDeclaration = moduleDeclaration))
1628

17-
return KoinPreview(koin)
29+
return KoinPreview(modules)
1830
}
1931

20-
class KoinPreview internal constructor(private val koin: Koin) {
32+
class KoinPreview internal constructor(private val modulesList: List<Module>) {
2133
@Composable
2234
infix fun WithContent(content: @Composable () -> Unit) {
23-
KoinContext(context = koin) {
35+
KoinApplicationPreview(application = { modules(modulesList) }) {
2436
content()
2537
}
2638
}

0 commit comments

Comments
 (0)