refactor: integrate Koin for dependency injection across platforms
This commit is contained in:
@@ -43,4 +43,9 @@ Learn more about [Kotlin Multiplatform](https://www.jetbrains.com/help/kotlin-mu
|
||||
[Kotlin/Wasm](https://kotl.in/wasm/)…
|
||||
|
||||
We would appreciate your feedback on Compose/Web and Kotlin/Wasm in the public Slack channel [#compose-web](https://slack-chats.kotlinlang.org/c/compose-web).
|
||||
If you face any issues, please report them on [YouTrack](https://youtrack.jetbrains.com/newIssue?project=CMP).
|
||||
If you face any issues, please report them on [YouTrack](https://youtrack.jetbrains.com/newIssue?project=CMP).
|
||||
|
||||
## Dependency injection
|
||||
|
||||
For dependency injection, we use [Koin](https://insert-koin.io/). To know more about the integration with Compose,
|
||||
please refer to the [Koin Compose documentation](https://insert-koin.io/docs/reference/koin-compose/compose).
|
||||
@@ -14,8 +14,8 @@ kotlin {
|
||||
dependencies {
|
||||
implementation(projects.shared)
|
||||
|
||||
// Compose
|
||||
implementation(libs.androidx.activity.compose)
|
||||
|
||||
implementation(libs.compose.uiToolingPreview)
|
||||
debugImplementation(libs.compose.uiTooling)
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
|
||||
<application
|
||||
android:name=".GwenedegApp"
|
||||
android:allowBackup="true"
|
||||
android:icon="@mipmap/ic_launcher"
|
||||
android:label="@string/app_name"
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
package fr.ajaury.gwenedeg
|
||||
|
||||
import android.app.Application
|
||||
|
||||
class GwenedegApp : Application()
|
||||
@@ -25,4 +25,4 @@ compose.desktop {
|
||||
packageVersion = "1.0.0"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@ package fr.ajaury.gwenedeg
|
||||
import androidx.compose.ui.window.Window
|
||||
import androidx.compose.ui.window.application
|
||||
|
||||
fun main() =
|
||||
fun main() {
|
||||
application {
|
||||
Window(
|
||||
onCloseRequest = ::exitApplication,
|
||||
@@ -12,3 +12,4 @@ fun main() =
|
||||
App()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,6 +15,7 @@ kotlin = "2.3.21"
|
||||
kotlin-wrappers = "2026.5.5"
|
||||
kotlinx-coroutines = "1.11.0"
|
||||
material3 = "1.11.0-alpha07"
|
||||
koin = "4.2.1"
|
||||
|
||||
[libraries]
|
||||
kotlin-test = { module = "org.jetbrains.kotlin:kotlin-test", version.ref = "kotlin" }
|
||||
@@ -34,6 +35,12 @@ compose-material3 = { module = "org.jetbrains.compose.material3:material3", vers
|
||||
compose-ui = { module = "org.jetbrains.compose.ui:ui", version.ref = "composeMultiplatform" }
|
||||
compose-components-resources = { module = "org.jetbrains.compose.components:components-resources", version.ref = "composeMultiplatform" }
|
||||
compose-uiToolingPreview = { module = "org.jetbrains.compose.ui:ui-tooling-preview", version.ref = "composeMultiplatform" }
|
||||
koin-bom = { module = "io.insert-koin:koin-bom", version.ref = "koin" }
|
||||
koin-core = { module = "io.insert-koin:koin-core" }
|
||||
koin-core-viewmodel = { module = "io.insert-koin:koin-core-viewmodel" }
|
||||
koin-compose = { module = "io.insert-koin:koin-compose" }
|
||||
koin-compose-viewmodel = { module = "io.insert-koin:koin-compose-viewmodel" }
|
||||
koin-android = { module = "io.insert-koin:koin-android" }
|
||||
kotlinx-coroutinesSwing = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-swing", version.ref = "kotlinx-coroutines" }
|
||||
wrappers-browser = { module = "org.jetbrains.kotlin-wrappers:kotlin-browser", version.ref = "kotlin-wrappers" }
|
||||
|
||||
|
||||
@@ -65,6 +65,13 @@ kotlin {
|
||||
implementation(libs.compose.uiToolingPreview)
|
||||
implementation(libs.androidx.lifecycle.viewmodelCompose)
|
||||
implementation(libs.androidx.lifecycle.runtimeCompose)
|
||||
|
||||
// DI
|
||||
implementation(project.dependencies.platform(libs.koin.bom))
|
||||
implementation(libs.koin.core)
|
||||
implementation(libs.koin.core.viewmodel)
|
||||
implementation(libs.koin.compose)
|
||||
implementation(libs.koin.compose.viewmodel)
|
||||
}
|
||||
commonTest.dependencies {
|
||||
implementation(libs.kotlin.test)
|
||||
|
||||
@@ -2,20 +2,22 @@ package fr.ajaury.gwenedeg
|
||||
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.ui.tooling.preview.Preview
|
||||
import fr.ajaury.gwenedeg.records.data.InMemoryRecordRepository
|
||||
import fr.ajaury.gwenedeg.di.appModule
|
||||
import fr.ajaury.gwenedeg.records.ui.RecordsScreen
|
||||
import fr.ajaury.gwenedeg.records.ui.viewmodel.RecordsViewModel
|
||||
import org.koin.compose.KoinApplication
|
||||
import org.koin.dsl.koinConfiguration
|
||||
|
||||
@Composable
|
||||
@Preview
|
||||
fun App() {
|
||||
val viewModel = remember {
|
||||
RecordsViewModel(InMemoryRecordRepository())
|
||||
}
|
||||
|
||||
MaterialTheme {
|
||||
RecordsScreen(viewModel)
|
||||
KoinApplication(
|
||||
configuration = koinConfiguration {
|
||||
modules(appModule)
|
||||
},
|
||||
) {
|
||||
MaterialTheme {
|
||||
RecordsScreen()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
package fr.ajaury.gwenedeg.di
|
||||
|
||||
import fr.ajaury.gwenedeg.records.data.InMemoryRecordRepository
|
||||
import fr.ajaury.gwenedeg.records.domain.RecordRepository
|
||||
import fr.ajaury.gwenedeg.records.ui.viewmodel.RecordsViewModel
|
||||
import org.koin.core.module.dsl.bind
|
||||
import org.koin.core.module.dsl.factoryOf
|
||||
import org.koin.core.module.dsl.viewModelOf
|
||||
import org.koin.dsl.module
|
||||
|
||||
val appModule = module {
|
||||
factoryOf(::InMemoryRecordRepository) { bind<RecordRepository>() }
|
||||
viewModelOf(::RecordsViewModel)
|
||||
}
|
||||
@@ -14,11 +14,21 @@ import androidx.compose.runtime.getValue
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.lifecycle.compose.collectAsStateWithLifecycle
|
||||
import fr.ajaury.gwenedeg.records.domain.Record
|
||||
import fr.ajaury.gwenedeg.records.ui.viewmodel.RecordsViewModel
|
||||
import org.koin.compose.viewmodel.koinViewModel
|
||||
|
||||
@Composable
|
||||
fun RecordsScreen(viewModel: RecordsViewModel) {
|
||||
fun RecordsScreen(viewModel: RecordsViewModel = koinViewModel()) {
|
||||
val records by viewModel.uiState.collectAsStateWithLifecycle()
|
||||
|
||||
RecordsScreen(
|
||||
records = records,
|
||||
)
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun RecordsScreen(records: List<Record>) {
|
||||
Scaffold { innerPadding ->
|
||||
LazyColumn(
|
||||
modifier =
|
||||
|
||||
@@ -2,4 +2,5 @@ package fr.ajaury.gwenedeg
|
||||
|
||||
import androidx.compose.ui.window.ComposeUIViewController
|
||||
|
||||
@Suppress("ktlint:standard:function-naming")
|
||||
fun MainViewController() = ComposeUIViewController { App() }
|
||||
|
||||
@@ -22,7 +22,8 @@ kotlin {
|
||||
commonMain.dependencies {
|
||||
implementation(projects.shared)
|
||||
|
||||
// Compose
|
||||
implementation(libs.compose.ui)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
@file:Suppress("ktlint:standard:filename")
|
||||
|
||||
package fr.ajaury.gwenedeg
|
||||
|
||||
import androidx.compose.ui.ExperimentalComposeUiApi
|
||||
|
||||
Reference in New Issue
Block a user