Skip to content

Commit 676622e

Browse files
committed
Adopt Koin for dependency injection
1 parent b9552d3 commit 676622e

File tree

5 files changed

+65
-16
lines changed

5 files changed

+65
-16
lines changed

demos/supabase-todolist/gradle/libs.versions.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ kotlinx-io = "0.5.4"
1313
ktor = "3.0.1"
1414
uuid = "0.8.2"
1515
buildKonfig = "0.15.1"
16+
koin-bom = "4.0.2"
1617

1718
junit = "4.13.2"
1819

@@ -59,6 +60,8 @@ androidx-espresso-core = { group = "androidx.test.espresso", name = "espresso-co
5960
androidx-material = { group = "com.google.android.material", name = "material", version.ref = "androidx-material" }
6061
compose-ui-tooling-preview = { module = "androidx.compose.ui:ui-tooling-preview", version.ref = "compose-preview" }
6162
compose-lifecycle = { module = "org.jetbrains.androidx.lifecycle:lifecycle-viewmodel-compose", version.ref = "lifecycle" }
63+
koin-core = { module = "io.insert-koin:koin-core", version.ref = "koin-bom" }
64+
koin-compose-viewmodel = { module = "io.insert-koin:koin-compose-viewmodel", version.ref = "koin-bom" }
6265

6366
[plugins]
6467
androidApplication = { id = "com.android.application", version.ref = "android-gradle-plugin" }

demos/supabase-todolist/iosApp/iosApp.xcodeproj/project.pbxproj

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@
118118
7555FF79242A565900829871 /* Resources */,
119119
F85CB1118929364A9C6EFABC /* Frameworks */,
120120
3C5ACF3A4AAFF294B2A5839B /* [CP] Embed Pods Frameworks */,
121+
1015E800EC39A6B62654C306 /* [CP] Copy Pods Resources */,
121122
);
122123
buildRules = (
123124
);
@@ -191,6 +192,23 @@
191192
runOnlyForDeploymentPostprocessing = 0;
192193
shellPath = /bin/sh;
193194
};
195+
1015E800EC39A6B62654C306 /* [CP] Copy Pods Resources */ = {
196+
isa = PBXShellScriptBuildPhase;
197+
buildActionMask = 2147483647;
198+
files = (
199+
);
200+
inputFileListPaths = (
201+
"${PODS_ROOT}/Target Support Files/Pods-iosApp/Pods-iosApp-resources-${CONFIGURATION}-input-files.xcfilelist",
202+
);
203+
name = "[CP] Copy Pods Resources";
204+
outputFileListPaths = (
205+
"${PODS_ROOT}/Target Support Files/Pods-iosApp/Pods-iosApp-resources-${CONFIGURATION}-output-files.xcfilelist",
206+
);
207+
runOnlyForDeploymentPostprocessing = 0;
208+
shellPath = /bin/sh;
209+
shellScript = "\"${PODS_ROOT}/Target Support Files/Pods-iosApp/Pods-iosApp-resources.sh\"\n";
210+
showEnvVarsInLog = 0;
211+
};
194212
3C5ACF3A4AAFF294B2A5839B /* [CP] Embed Pods Frameworks */ = {
195213
isa = PBXShellScriptBuildPhase;
196214
buildActionMask = 2147483647;

demos/supabase-todolist/shared/build.gradle.kts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ kotlin {
5151
implementation(compose.components.resources)
5252
implementation(compose.materialIconsExtended)
5353
implementation(libs.compose.lifecycle)
54+
api(libs.koin.core)
55+
implementation(libs.koin.compose.viewmodel)
5456
}
5557
androidMain.dependencies {
5658
api(libs.androidx.activity.compose)

demos/supabase-todolist/shared/src/commonMain/kotlin/com/powersync/demos/App.kt

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import com.powersync.DatabaseDriverFactory
1515
import com.powersync.PowerSyncDatabase
1616
import com.powersync.bucket.BucketPriority
1717
import com.powersync.connector.supabase.SupabaseConnector
18+
import com.powersync.connectors.PowerSyncBackendConnector
1819
import com.powersync.demos.components.EditDialog
1920
import com.powersync.demos.powersync.ListContent
2021
import com.powersync.demos.powersync.ListItem
@@ -26,21 +27,49 @@ import com.powersync.demos.screens.SignUpScreen
2627
import com.powersync.demos.screens.TodosScreen
2728
import kotlinx.coroutines.flow.debounce
2829
import kotlinx.coroutines.runBlocking
30+
import org.koin.compose.KoinApplication
31+
import org.koin.compose.koinInject
32+
import org.koin.compose.viewmodel.koinViewModel
33+
import org.koin.core.KoinApplication
34+
import org.koin.core.module.dsl.bind
35+
import org.koin.core.module.dsl.viewModel
36+
import org.koin.core.module.dsl.viewModelOf
37+
import org.koin.core.module.dsl.withOptions
38+
import org.koin.dsl.module
39+
40+
val sharedAppModule = module {
41+
single { PowerSyncDatabase(get(), schema) }
42+
single {
43+
SupabaseConnector(
44+
powerSyncEndpoint = Config.POWERSYNC_URL,
45+
supabaseUrl = Config.SUPABASE_URL,
46+
supabaseKey = Config.SUPABASE_ANON_KEY,
47+
)
48+
} withOptions { bind<PowerSyncBackendConnector>() }
49+
50+
viewModel { NavController(Screen.Home) }
51+
viewModelOf(::AuthViewModel)
52+
}
2953

3054
@Composable
3155
fun App(
3256
factory: DatabaseDriverFactory,
3357
modifier: Modifier = Modifier,
3458
) {
35-
val supabase =
36-
remember {
37-
SupabaseConnector(
38-
powerSyncEndpoint = Config.POWERSYNC_URL,
39-
supabaseUrl = Config.SUPABASE_URL,
40-
supabaseKey = Config.SUPABASE_ANON_KEY,
41-
)
42-
}
43-
val db = remember { PowerSyncDatabase(factory, schema) }
59+
fun KoinApplication.withDatabase() {
60+
modules(module { single { factory } }, sharedAppModule)
61+
}
62+
63+
KoinApplication(application = KoinApplication::withDatabase) {
64+
AppContent(modifier=modifier)
65+
}
66+
}
67+
68+
@Composable
69+
internal fun AppContent(
70+
db: PowerSyncDatabase = koinInject(),
71+
modifier: Modifier = Modifier,
72+
) {
4473
// Debouncing the status flow prevents flicker
4574
val status by db.currentStatus
4675
.asFlow()
@@ -55,12 +84,8 @@ fun App(
5584
derivedStateOf { status.statusForPriority(BucketPriority(1)).hasSynced }
5685
}
5786

58-
val navController = remember { NavController(Screen.Home) }
59-
val authViewModel =
60-
remember {
61-
AuthViewModel(supabase, db, navController)
62-
}
63-
87+
val authViewModel = koinViewModel<AuthViewModel>()
88+
val navController = koinViewModel<NavController>()
6489
val authState by authViewModel.authState.collectAsState()
6590
val currentScreen by navController.currentScreen.collectAsState()
6691

demos/supabase-todolist/shared/src/commonMain/kotlin/com/powersync/demos/NavController.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.powersync.demos
22

3+
import androidx.lifecycle.ViewModel
34
import kotlinx.coroutines.flow.MutableStateFlow
45
import kotlinx.coroutines.flow.StateFlow
56
import kotlinx.coroutines.flow.asStateFlow
@@ -11,7 +12,7 @@ sealed class Screen {
1112
data object Todos : Screen()
1213
}
1314

14-
internal class NavController(initialScreen: Screen) {
15+
internal class NavController(initialScreen: Screen): ViewModel() {
1516
private val _currentScreen = MutableStateFlow<Screen>(initialScreen)
1617
val currentScreen: StateFlow<Screen> = _currentScreen.asStateFlow()
1718

0 commit comments

Comments
 (0)