diff --git a/compose/build.gradle.kts b/compose/build.gradle.kts index 6ca0070..4eb996e 100644 --- a/compose/build.gradle.kts +++ b/compose/build.gradle.kts @@ -18,5 +18,7 @@ android { dependencies { api(projects.android) implementation(libs.composeRuntime) + implementation(libs.navigationCompose) + implementation(libs.navigationUi) implementation(libs.lifecycleComposeViewmodel) } diff --git a/compose/src/main/kotlin/app/meetacy/di/android/compose/navigation/NavController.kt b/compose/src/main/kotlin/app/meetacy/di/android/compose/navigation/NavController.kt new file mode 100644 index 0000000..b52e72a --- /dev/null +++ b/compose/src/main/kotlin/app/meetacy/di/android/compose/navigation/NavController.kt @@ -0,0 +1,30 @@ +package app.meetacy.di.android.compose.navigation + +import androidx.compose.runtime.* +import androidx.navigation.NavController +import app.meetacy.di.DI +import app.meetacy.di.builder.di +import app.meetacy.di.dependency.Dependency + +public val DI.navController: NavController by Dependency + +@PublishedApi +internal val LocalNavController: ProvidableCompositionLocal = + compositionLocalOf { + error("Please call `buildNavigationDI(...)` only inside `NavigationScreen { ... }`") + } + +@Composable +public fun NavigationScreen( + controller: NavController, + block: @Composable () -> Unit +) { + CompositionLocalProvider( + LocalNavController provides controller, + content = block + ) +} + +public fun buildNavigationDI(di: DI): DI = di + di { + val navController by provider { LocalNavController.current } +} diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 87b0c79..2e12483 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -4,13 +4,17 @@ kotlin = "1.9.21" androidGradle = "7.3.0" composeRuntime = "1.3.3" composeCompiler = "1.5.6" +androidxNavigation = "2.7.7" lifecycle = "2.6.1" -mdi = "0.0.31" +mdi = "0.0.32" [libraries] composeRuntime = { module = "androidx.compose.runtime:runtime", version.ref = "composeRuntime" } +navigationUi = { module = "androidx.navigation:navigation-ui-ktx", version.ref = "androidxNavigation" } +navigationCompose = { module = "androidx.navigation:navigation-compose", version.ref = "androidxNavigation" } + lifecycleComposeViewmodel = { module = "androidx.lifecycle:lifecycle-viewmodel-compose", version.ref = "lifecycle" } # gradle plugins