Address API Feedback on hilt-navigation-compose.
* Use a shorter name, from hiltNavGraphViewModel() to hiltViewModel()
* Removed the extension on `NavController` since a user can directly use the `navigation-compose` API for getting a `NavBackStackEntry` from a route. To reduce the inline code a similar internal function with @PublishApi was added.
* Replace the KDoc sample code with actual compilable code referenced via @sample and annotated with @Sampled, this is the pattern used in the rest of the Compose codebase.
Bug: 183404714
Test: HiltViewModelComposeTest
Relnote: Rename the functions hiltNavGraphViewModel() to hiltViewModel() and removed the extension on `NavController` since a user can directly use the `navigation-compose` API for getting a `NavBackStackEntry` from a route.
Change-Id: I6ef57a195f43a7cc7d9d7d56837778534b02121a
diff --git a/hilt/hilt-navigation-compose/api/current.txt b/hilt/hilt-navigation-compose/api/current.txt
index 31a768e..447d5c6 100644
--- a/hilt/hilt-navigation-compose/api/current.txt
+++ b/hilt/hilt-navigation-compose/api/current.txt
@@ -1,10 +1,12 @@
// Signature format: 4.0
package androidx.hilt.navigation.compose {
- public final class NavHostControllerKt {
+ public final class HiltViewModelKt {
method @androidx.compose.runtime.Composable public static inline <reified VM extends androidx.lifecycle.ViewModel> VM! hiltNavGraphViewModel();
method @androidx.compose.runtime.Composable public static inline <reified VM extends androidx.lifecycle.ViewModel> VM! hiltNavGraphViewModel(androidx.navigation.NavBackStackEntry backStackEntry);
method @androidx.compose.runtime.Composable public static inline <reified VM extends androidx.lifecycle.ViewModel> VM! hiltNavGraphViewModel(androidx.navigation.NavController, String route);
+ method @androidx.compose.runtime.Composable public static inline <reified VM extends androidx.lifecycle.ViewModel> VM! hiltViewModel();
+ method @androidx.compose.runtime.Composable public static inline <reified VM extends androidx.lifecycle.ViewModel> VM! hiltViewModel(androidx.navigation.NavBackStackEntry backStackEntry);
}
}
diff --git a/hilt/hilt-navigation-compose/api/public_plus_experimental_current.txt b/hilt/hilt-navigation-compose/api/public_plus_experimental_current.txt
index 31a768e..447d5c6 100644
--- a/hilt/hilt-navigation-compose/api/public_plus_experimental_current.txt
+++ b/hilt/hilt-navigation-compose/api/public_plus_experimental_current.txt
@@ -1,10 +1,12 @@
// Signature format: 4.0
package androidx.hilt.navigation.compose {
- public final class NavHostControllerKt {
+ public final class HiltViewModelKt {
method @androidx.compose.runtime.Composable public static inline <reified VM extends androidx.lifecycle.ViewModel> VM! hiltNavGraphViewModel();
method @androidx.compose.runtime.Composable public static inline <reified VM extends androidx.lifecycle.ViewModel> VM! hiltNavGraphViewModel(androidx.navigation.NavBackStackEntry backStackEntry);
method @androidx.compose.runtime.Composable public static inline <reified VM extends androidx.lifecycle.ViewModel> VM! hiltNavGraphViewModel(androidx.navigation.NavController, String route);
+ method @androidx.compose.runtime.Composable public static inline <reified VM extends androidx.lifecycle.ViewModel> VM! hiltViewModel();
+ method @androidx.compose.runtime.Composable public static inline <reified VM extends androidx.lifecycle.ViewModel> VM! hiltViewModel(androidx.navigation.NavBackStackEntry backStackEntry);
}
}
diff --git a/hilt/hilt-navigation-compose/api/restricted_current.txt b/hilt/hilt-navigation-compose/api/restricted_current.txt
index 31a768e..0b008ea 100644
--- a/hilt/hilt-navigation-compose/api/restricted_current.txt
+++ b/hilt/hilt-navigation-compose/api/restricted_current.txt
@@ -1,10 +1,13 @@
// Signature format: 4.0
package androidx.hilt.navigation.compose {
- public final class NavHostControllerKt {
+ public final class HiltViewModelKt {
method @androidx.compose.runtime.Composable public static inline <reified VM extends androidx.lifecycle.ViewModel> VM! hiltNavGraphViewModel();
method @androidx.compose.runtime.Composable public static inline <reified VM extends androidx.lifecycle.ViewModel> VM! hiltNavGraphViewModel(androidx.navigation.NavBackStackEntry backStackEntry);
method @androidx.compose.runtime.Composable public static inline <reified VM extends androidx.lifecycle.ViewModel> VM! hiltNavGraphViewModel(androidx.navigation.NavController, String route);
+ method @androidx.compose.runtime.Composable @kotlin.PublishedApi internal static <VM extends androidx.lifecycle.ViewModel> VM hiltViewModel(kotlin.reflect.KClass<VM> kClass, androidx.navigation.NavBackStackEntry backStackEntry);
+ method @androidx.compose.runtime.Composable public static inline <reified VM extends androidx.lifecycle.ViewModel> VM! hiltViewModel();
+ method @androidx.compose.runtime.Composable public static inline <reified VM extends androidx.lifecycle.ViewModel> VM! hiltViewModel(androidx.navigation.NavBackStackEntry backStackEntry);
}
}
diff --git a/hilt/hilt-navigation-compose/build.gradle b/hilt/hilt-navigation-compose/build.gradle
index afc8ac0..8ef423c 100644
--- a/hilt/hilt-navigation-compose/build.gradle
+++ b/hilt/hilt-navigation-compose/build.gradle
@@ -60,12 +60,12 @@
}
androidx {
- name = "Navigation Compose Hilt Extension"
+ name = "Navigation Compose Hilt Integration"
publish = Publish.SNAPSHOT_AND_RELEASE
mavenVersion = LibraryVersions.HILT_NAVIGATION_COMPOSE
mavenGroup = LibraryGroups.HILT
inceptionYear = "2021"
- description = "Navigation Compose Hilt Extension"
+ description = "Navigation Compose Hilt Integration"
runApiTasks = new RunApiTasks.Yes()
legacyDisableKotlinStrictApiMode = true
}
diff --git a/hilt/hilt-navigation-compose/samples/build.gradle b/hilt/hilt-navigation-compose/samples/build.gradle
new file mode 100644
index 0000000..cda9b05
--- /dev/null
+++ b/hilt/hilt-navigation-compose/samples/build.gradle
@@ -0,0 +1,46 @@
+/*
+ * Copyright 2021 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+
+import androidx.build.LibraryGroups
+import androidx.build.LibraryVersions
+import androidx.build.LibraryType
+
+import static androidx.build.dependencies.DependenciesKt.*
+
+plugins {
+ id("AndroidXPlugin")
+ id("com.android.library")
+ id("AndroidXUiPlugin")
+ id("org.jetbrains.kotlin.android")
+}
+
+dependencies {
+ kotlinPlugin(projectOrArtifact(":compose:compiler:compiler"))
+ implementation(KOTLIN_STDLIB)
+
+ compileOnly(projectOrArtifact(":annotation:annotation-sampled"))
+ implementation(projectOrArtifact(":hilt:hilt-navigation-compose"))
+}
+
+androidx {
+ name = "Navigation Compose Hilt Extension Samples"
+ type = LibraryType.SAMPLES
+ mavenGroup = LibraryGroups.HILT
+ mavenVersion = LibraryVersions.HILT_NAVIGATION_COMPOSE
+ inceptionYear = "2021"
+ description = "Samples for the Navigation Compose Hilt Integration"
+}
diff --git a/hilt/hilt-navigation-compose/samples/src/main/AndroidManifest.xml b/hilt/hilt-navigation-compose/samples/src/main/AndroidManifest.xml
new file mode 100644
index 0000000..0a1e1b5
--- /dev/null
+++ b/hilt/hilt-navigation-compose/samples/src/main/AndroidManifest.xml
@@ -0,0 +1,17 @@
+<!--
+ Copyright 2021 The Android Open Source Project
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+ -->
+
+<manifest package="androidx.hilt.navigation.compose.samples" />
diff --git a/hilt/hilt-navigation-compose/samples/src/main/java/androidx/hilt/navigation/compose/samples/HiltViewModelSamples.kt b/hilt/hilt-navigation-compose/samples/src/main/java/androidx/hilt/navigation/compose/samples/HiltViewModelSamples.kt
new file mode 100644
index 0000000..2a13592
--- /dev/null
+++ b/hilt/hilt-navigation-compose/samples/src/main/java/androidx/hilt/navigation/compose/samples/HiltViewModelSamples.kt
@@ -0,0 +1,68 @@
+/*
+ * Copyright 2021 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+@file:Suppress("UNUSED_VARIABLE") // These are sample files.
+
+package androidx.hilt.navigation.compose.samples
+
+import androidx.annotation.Sampled
+import androidx.compose.runtime.Composable
+import androidx.hilt.navigation.compose.hiltViewModel
+import androidx.lifecycle.ViewModel
+import androidx.navigation.compose.NavHost
+import androidx.navigation.compose.composable
+import androidx.navigation.compose.getBackStackEntry
+import androidx.navigation.compose.navigation
+import androidx.navigation.compose.rememberNavController
+import dagger.hilt.android.lifecycle.HiltViewModel
+import javax.inject.Inject
+
+@Sampled
+@Composable
+fun NavComposable() {
+ val navController = rememberNavController()
+ NavHost(navController, startDestination = "ExampleRoute") {
+ composable("ExampleRoute") {
+ val viewModel = hiltViewModel<ExampleViewModel>()
+ }
+ }
+}
+
+@Sampled
+@Composable
+fun NestedNavComposable() {
+ val navController = rememberNavController()
+ NavHost(navController, startDestination = "Parent") {
+ navigation(startDestination = "InnerRouteA", route = "Parent") {
+ composable("InnerRouteA") {
+ val viewModel = hiltViewModel<ParentViewModel>(
+ navController.getBackStackEntry("Parent")
+ )
+ }
+ composable("InnerRouteB") {
+ val viewModel = hiltViewModel<ParentViewModel>(
+ navController.getBackStackEntry("Parent")
+ )
+ }
+ }
+ }
+}
+
+@HiltViewModel
+class ExampleViewModel @Inject constructor() : ViewModel()
+
+@HiltViewModel
+class ParentViewModel @Inject constructor() : ViewModel()
diff --git a/hilt/hilt-navigation-compose/src/androidTest/AndroidManifest.xml b/hilt/hilt-navigation-compose/src/androidTest/AndroidManifest.xml
index e104685..764ae11 100644
--- a/hilt/hilt-navigation-compose/src/androidTest/AndroidManifest.xml
+++ b/hilt/hilt-navigation-compose/src/androidTest/AndroidManifest.xml
@@ -19,6 +19,6 @@
package="androidx.hilt.navigation.compose">
<application>
<activity
- android:name="androidx.hilt.navigation.compose.HiltNavGraphViewModelComposeTest$TestActivity" />
+ android:name="androidx.hilt.navigation.compose.HiltViewModelComposeTest$TestActivity" />
</application>
</manifest>
diff --git a/hilt/hilt-navigation-compose/src/androidTest/java/androidx/hilt/navigation/compose/HiltNavGraphViewModelComposeTest.kt b/hilt/hilt-navigation-compose/src/androidTest/java/androidx/hilt/navigation/compose/HiltViewModelComposeTest.kt
similarity index 88%
rename from hilt/hilt-navigation-compose/src/androidTest/java/androidx/hilt/navigation/compose/HiltNavGraphViewModelComposeTest.kt
rename to hilt/hilt-navigation-compose/src/androidTest/java/androidx/hilt/navigation/compose/HiltViewModelComposeTest.kt
index 68ad986..e45832f 100644
--- a/hilt/hilt-navigation-compose/src/androidTest/java/androidx/hilt/navigation/compose/HiltNavGraphViewModelComposeTest.kt
+++ b/hilt/hilt-navigation-compose/src/androidTest/java/androidx/hilt/navigation/compose/HiltViewModelComposeTest.kt
@@ -29,6 +29,7 @@
import androidx.lifecycle.ViewModel
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
+import androidx.navigation.compose.getBackStackEntry
import androidx.navigation.compose.navigate
import androidx.navigation.compose.navigation
import androidx.navigation.compose.rememberNavController
@@ -48,7 +49,7 @@
@HiltAndroidTest
@ExperimentalTestApi
@RunWith(AndroidJUnit4::class)
-class HiltNavGraphViewModelComposeTest {
+class HiltViewModelComposeTest {
@get:Rule
val testRule = HiltAndroidRule(this)
@@ -64,10 +65,10 @@
val navController = rememberNavController()
NavHost(navController, startDestination = "One") {
composable("One") {
- firstViewModel = hiltNavGraphViewModel()
+ firstViewModel = hiltViewModel()
}
}
- secondViewModel = hiltNavGraphViewModel()
+ secondViewModel = hiltViewModel()
}
composeTestRule.awaitIdle()
assertThat(firstViewModel).isNotNull()
@@ -85,12 +86,12 @@
composeTestRule.setContent {
val navController = rememberNavController()
NavHost(navController, startDestination = "One") {
- composable("One") { backStackEntry ->
- firstViewModel = hiltNavGraphViewModel(backStackEntry)
+ composable("One") {
+ firstViewModel = hiltViewModel()
NavigateButton("Two") { navController.navigate("Two") }
}
- composable("Two") { backStackEntry ->
- secondViewModel = hiltNavGraphViewModel(backStackEntry)
+ composable("Two") {
+ secondViewModel = hiltViewModel()
NavigateButton("One") { navController.navigate("One") }
}
}
@@ -114,11 +115,15 @@
NavHost(navController, startDestination = "Main") {
navigation(startDestination = "One", route = "Main") {
composable("One") {
- firstViewModel = navController.hiltNavGraphViewModel("Main")
+ firstViewModel = hiltViewModel(
+ navController.getBackStackEntry("Main")
+ )
NavigateButton("Two") { navController.navigate("Two") }
}
composable("Two") {
- secondViewModel = navController.hiltNavGraphViewModel("Main")
+ secondViewModel = hiltViewModel(
+ navController.getBackStackEntry("Main")
+ )
NavigateButton("One") { navController.navigate("One") }
}
}
diff --git a/hilt/hilt-navigation-compose/src/main/java/androidx/hilt/navigation/compose/HiltViewModel.kt b/hilt/hilt-navigation-compose/src/main/java/androidx/hilt/navigation/compose/HiltViewModel.kt
new file mode 100644
index 0000000..d6ce28c
--- /dev/null
+++ b/hilt/hilt-navigation-compose/src/main/java/androidx/hilt/navigation/compose/HiltViewModel.kt
@@ -0,0 +1,127 @@
+/*
+ * Copyright 2021 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package androidx.hilt.navigation.compose
+
+import androidx.compose.runtime.Composable
+import androidx.compose.ui.platform.LocalContext
+import androidx.hilt.navigation.HiltViewModelFactory
+import androidx.lifecycle.ViewModel
+import androidx.lifecycle.ViewModelProvider
+import androidx.lifecycle.viewmodel.compose.LocalViewModelStoreOwner
+import androidx.lifecycle.viewmodel.compose.viewModel
+import androidx.navigation.NavBackStackEntry
+import androidx.navigation.NavController
+import androidx.navigation.compose.getBackStackEntry
+import kotlin.reflect.KClass
+
+/**
+ * Returns an existing
+ * [HiltViewModel](https://dagger.dev/api/latest/dagger/hilt/android/lifecycle/HiltViewModel)
+ * -annotated [ViewModel] or creates a new one scoped to the current navigation graph present on
+ * the {@link NavController} back stack.
+ *
+ * If no navigation graph is currently present then the current scope will be used, usually, a
+ * fragment or an activity.
+ *
+ * @sample androidx.hilt.navigation.compose.samples.NavComposable
+ */
+@Composable
+inline fun <reified VM : ViewModel> hiltViewModel(): VM {
+ val owner = LocalViewModelStoreOwner.current
+ return if (owner is NavBackStackEntry) {
+ hiltViewModel(VM::class, owner)
+ } else {
+ viewModel()
+ }
+}
+
+/**
+ * Returns an existing
+ * [HiltViewModel](https://dagger.dev/api/latest/dagger/hilt/android/lifecycle/HiltViewModel)
+ * -annotated [ViewModel] or creates a new one scoped to the current navigation graph present on
+ * the {@link NavController} back stack.
+ *
+ * If no navigation graph is currently present then the current scope will be used, usually, a
+ * fragment or an activity.
+ */
+@Deprecated(
+ message = "Use hiltViewModel() instead.",
+ replaceWith = ReplaceWith("hiltViewModel()"),
+)
+@Composable
+inline fun <reified VM : ViewModel> hiltNavGraphViewModel() = hiltViewModel<VM>()
+
+/**
+ * Returns an existing
+ * [HiltViewModel](https://dagger.dev/api/latest/dagger/hilt/android/lifecycle/HiltViewModel)
+ * -annotated [ViewModel] or creates a new one scoped to the current navigation graph present on
+ * the [NavController] back stack.
+ *
+ * @param backStackEntry The entry of a [NavController] back stack.
+ *
+ * @sample androidx.hilt.navigation.compose.samples.NestedNavComposable
+ */
+@Composable
+inline fun <reified VM : ViewModel> hiltViewModel(backStackEntry: NavBackStackEntry) =
+ hiltViewModel(VM::class, backStackEntry)
+
+/**
+ * Returns an existing
+ * [HiltViewModel](https://dagger.dev/api/latest/dagger/hilt/android/lifecycle/HiltViewModel)
+ * -annotated [ViewModel] or creates a new one scoped to the current navigation graph present on
+ * the [NavController] back stack.
+ *
+ * @param backStackEntry The entry of a [NavController] back stack.
+ */
+@Deprecated(
+ message = "Use hiltViewModel(NavBackStackEntry) instead.",
+ replaceWith = ReplaceWith("hiltViewModel(backStackEntry)"),
+)
+@Composable
+inline fun <reified VM : ViewModel> hiltNavGraphViewModel(backStackEntry: NavBackStackEntry) =
+ hiltViewModel<VM>(backStackEntry)
+
+/**
+ * Returns an existing
+ * [HiltViewModel](https://dagger.dev/api/latest/dagger/hilt/android/lifecycle/HiltViewModel)
+ * -annotated [ViewModel] or creates a new one scoped to the current navigation graph present on
+ * the [NavController] back stack.
+ *
+ * @param route route of a destination that exists on the [NavController] back stack.
+ */
+@Deprecated(
+ message = "Use hiltViewModel(NavBackStackEntry) in combination with " +
+ "NavController#getBackStackEntry(String). This API will be removed in a future version.",
+ replaceWith = ReplaceWith("hiltViewModel(this.getBackStackEntry(route))"),
+ level = DeprecationLevel.ERROR
+)
+@Composable
+inline fun <reified VM : ViewModel> NavController.hiltNavGraphViewModel(route: String) =
+ hiltViewModel<VM>(this.getBackStackEntry(route))
+
+@Composable
+@PublishedApi
+internal fun <VM : ViewModel> hiltViewModel(
+ kClass: KClass<VM>,
+ backStackEntry: NavBackStackEntry
+): VM {
+ val viewModelFactory = HiltViewModelFactory(
+ context = LocalContext.current,
+ navBackStackEntry = backStackEntry
+ )
+ return ViewModelProvider(backStackEntry, viewModelFactory).get(kClass.java)
+}
\ No newline at end of file
diff --git a/hilt/hilt-navigation-compose/src/main/java/androidx/hilt/navigation/compose/NavHostController.kt b/hilt/hilt-navigation-compose/src/main/java/androidx/hilt/navigation/compose/NavHostController.kt
deleted file mode 100644
index b7854dc..0000000
--- a/hilt/hilt-navigation-compose/src/main/java/androidx/hilt/navigation/compose/NavHostController.kt
+++ /dev/null
@@ -1,106 +0,0 @@
-/*
- * Copyright 2021 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package androidx.hilt.navigation.compose
-
-import androidx.compose.runtime.Composable
-import androidx.compose.ui.platform.LocalContext
-import androidx.hilt.navigation.HiltViewModelFactory
-import androidx.lifecycle.ViewModel
-import androidx.lifecycle.ViewModelProvider
-import androidx.lifecycle.viewmodel.compose.LocalViewModelStoreOwner
-import androidx.lifecycle.viewmodel.compose.viewModel
-import androidx.navigation.NavBackStackEntry
-import androidx.navigation.NavController
-import androidx.navigation.compose.getBackStackEntry
-
-/**
- * Returns an existing
- * [HiltViewModel](https://dagger.dev/api/latest/dagger/hilt/android/lifecycle/HiltViewModel)
- * -annotated [ViewModel] or creates a new one scoped to the current navigation graph present on
- * the {@link NavController} back stack:
- * ```
- * @Composable
- * fun ExampleScreen() {
- * val viewmodel = hiltNavGraphViewModel<ExampleViewModel>()
- * }
- * ```
- *
- * If no navigation graph is currently present then the current scope will be used, usually, a
- * fragment or an activity.
- */
-@Composable
-inline fun <reified VM : ViewModel> hiltNavGraphViewModel(): VM {
- val owner = LocalViewModelStoreOwner.current
- return if (owner is NavBackStackEntry) {
- hiltNavGraphViewModel(owner)
- } else {
- viewModel()
- }
-}
-
-/**
- * Returns an existing
- * [HiltViewModel](https://dagger.dev/api/latest/dagger/hilt/android/lifecycle/HiltViewModel)
- * -annotated [ViewModel] or creates a new one scoped to the current navigation graph present on
- * the [NavController] back stack:
- * ```
- * @Composable
- * fun MyApp() {
- * // ...
- * NavHost(navController, startDestination = startRoute) {
- * composable("example") { backStackEntry ->
- * val viewmodel = hiltNavGraphViewModel<ExampleViewModel>(backStackEntry)
- * }
- * }
- * }
- * ```
- *
- * @param backStackEntry The entry of a [NavController] back stack.
- */
-@Composable
-inline fun <reified VM : ViewModel> hiltNavGraphViewModel(backStackEntry: NavBackStackEntry): VM {
- val viewModelFactory = HiltViewModelFactory(
- context = LocalContext.current,
- navBackStackEntry = backStackEntry
- )
- return ViewModelProvider(backStackEntry, viewModelFactory).get(VM::class.java)
-}
-
-/**
- * Returns an existing
- * [HiltViewModel](https://dagger.dev/api/latest/dagger/hilt/android/lifecycle/HiltViewModel)
- * -annotated [ViewModel] or creates a new one scoped to the current navigation graph present on
- * the [NavController] back stack:
- * ```
- * @Composable
- * fun MyApp() {
- * // ...
- * NavHost(navController, startDestination = startRoute) {
- * navigation(startDestination = innerStartRoute, route = "Parent") {
- * // ...
- * composable("example") { backStackEntry ->
- * val parentViewModel = hiltNavGraphViewModel<ParentViewModel>("Parent")
- * }
- * }
- * }
- * }
- * ```
- * @param route route of a destination that exists on the [NavController] back stack.
- */
-@Composable
-inline fun <reified VM : ViewModel> NavController.hiltNavGraphViewModel(route: String) =
- hiltNavGraphViewModel<VM>(this.getBackStackEntry(route))
diff --git a/settings.gradle b/settings.gradle
index f7aa99b..07fef9b 100644
--- a/settings.gradle
+++ b/settings.gradle
@@ -393,6 +393,7 @@
includeProject(":hilt:hilt-compiler", "hilt/hilt-compiler", [BuildType.MAIN])
includeProject(":hilt:hilt-navigation", "hilt/hilt-navigation", [BuildType.MAIN, BuildType.COMPOSE])
includeProject(":hilt:hilt-navigation-compose", "hilt/hilt-navigation-compose", [BuildType.COMPOSE])
+includeProject(":hilt:hilt-navigation-compose-samples", "hilt/hilt-navigation-compose/samples", [BuildType.COMPOSE])
includeProject(":hilt:hilt-navigation-fragment", "hilt/hilt-navigation-fragment", [BuildType.MAIN])
includeProject(":hilt:hilt-work", "hilt/hilt-work", [BuildType.MAIN])
includeProject(":hilt:integration-tests:hilt-testapp-viewmodel", "hilt/integration-tests/viewmodelapp", [BuildType.MAIN])