Skip to content

A fully functional Android app built entirely with Kotlin and Jetpack Compose

Add more KDocs and nav args encoding

Manuel Vivoea07e97a073d
diff --git a/app/src/main/java/com/google/samples/apps/nowinandroid/ui/NiaAppState.kt b/app/src/main/java/com/google/samples/apps/nowinandroid/ui/NiaAppState.kt
index be1cc45a0..076d8d762 100644
--- a/app/src/main/java/com/google/samples/apps/nowinandroid/ui/NiaAppState.kt
+++ b/app/src/main/java/com/google/samples/apps/nowinandroid/ui/NiaAppState.kt
@@ -67,6 +67,9 @@ class NiaAppState(
val shouldShowNavRail: Boolean
get() = !shouldShowBottomBar
+ /**
+ * Top level destinations to be used in the BottomBar and NavRail
+ */
val topLevelDestinations: List<TopLevelDestination> = listOf(
TopLevelDestination(
route = ForYouDestination.route,
@@ -84,10 +87,19 @@ class NiaAppState(
)
)
- // --------------------
- // NAVIGATION LOGIC
- // --------------------
-
+ /**
+ * UI logic for navigating to a particular destination in the app. The NavigationOptions to
+ * navigate with are based on the type of destination, which could be a top level destination or
+ * just a regular destination.
+ *
+ * Top level destinations have only one copy of the destination of the back stack, and save and
+ * restore state whenever you navigate to and from it.
+ * Regular destinations can have multiple copies in the back stack and state isn't saved nor
+ * restored.
+ *
+ * @param destination: The [NiaNavigationDestination] the app needs to navigate to.
+ * @param route: Optional route to navigate to in case the destination contains arguments.
+ */
fun navigate(destination: NiaNavigationDestination, route: String? = null) {
trace("Navigation: $destination") {
if (destination is TopLevelDestination) {
@@ -115,6 +127,9 @@ class NiaAppState(
}
}
+/**
+ * Stores information about navigation events to be used with JankStats
+ */
@Composable
private fun NavigationTrackingSideEffect(navController: NavHostController) {
JankMetricDisposableEffect(navController) { metricsHolder ->
diff --git a/feature-author/src/main/java/com/google/samples/apps/nowinandroid/feature/author/navigation/AuthorNavigation.kt b/feature-author/src/main/java/com/google/samples/apps/nowinandroid/feature/author/navigation/AuthorNavigation.kt
index d3749335e..ee37bab95 100644
--- a/feature-author/src/main/java/com/google/samples/apps/nowinandroid/feature/author/navigation/AuthorNavigation.kt
+++ b/feature-author/src/main/java/com/google/samples/apps/nowinandroid/feature/author/navigation/AuthorNavigation.kt
@@ -16,6 +16,7 @@
package com.google.samples.apps.nowinandroid.feature.author.navigation
+import android.net.Uri
import androidx.navigation.NavBackStackEntry
import androidx.navigation.NavGraphBuilder
import androidx.navigation.NavType
@@ -29,12 +30,20 @@ object AuthorDestination : NiaNavigationDestination {
override val route = "author_route/{$authorIdArg}"
override val destination = "author_destination"
+ /**
+ * Creates destination route for an authorId that could include special characters
+ */
fun createNavigationRoute(authorIdArg: String): String {
- return "author_route/$authorIdArg"
+ val encodedId = Uri.encode(authorIdArg)
+ return "author_route/$encodedId"
}
+ /**
+ * Returns the authorId from a [NavBackStackEntry] after an author destination navigation call
+ */
fun fromNavArgs(entry: NavBackStackEntry): String {
- return entry.arguments?.getString(authorIdArg)!!
+ val encodedId = entry.arguments?.getString(authorIdArg)!!
+ return Uri.decode(encodedId)
}
}
diff --git a/feature-topic/src/main/java/com/google/samples/apps/nowinandroid/feature/topic/navigation/TopicNavigation.kt b/feature-topic/src/main/java/com/google/samples/apps/nowinandroid/feature/topic/navigation/TopicNavigation.kt
index 2368e713b..f3d4d021b 100644
--- a/feature-topic/src/main/java/com/google/samples/apps/nowinandroid/feature/topic/navigation/TopicNavigation.kt
+++ b/feature-topic/src/main/java/com/google/samples/apps/nowinandroid/feature/topic/navigation/TopicNavigation.kt
@@ -16,6 +16,7 @@
package com.google.samples.apps.nowinandroid.feature.topic.navigation
+import android.net.Uri
import androidx.navigation.NavBackStackEntry
import androidx.navigation.NavGraphBuilder
import androidx.navigation.NavType
@@ -29,12 +30,20 @@ object TopicDestination : NiaNavigationDestination {
override val route = "topic_route/{$topicIdArg}"
override val destination = "topic_destination"
+ /**
+ * Creates destination route for a topicId that could include special characters
+ */
fun createNavigationRoute(topicIdArg: String): String {
- return "topic_route/$topicIdArg"
+ val encodedId = Uri.encode(topicIdArg)
+ return "topic_route/$encodedId"
}
+ /**
+ * Returns the topicId from a [NavBackStackEntry] after a topic destination navigation call
+ */
fun fromNavArgs(entry: NavBackStackEntry): String {
- return entry.arguments?.getString(topicIdArg)!!
+ val encodedId = entry.arguments?.getString(topicIdArg)!!
+ return Uri.decode(encodedId)
}
}