From abe15077093f7f9baedfc0226659abc9f04f4e1a Mon Sep 17 00:00:00 2001 From: Manuel Vivo Date: Thu, 21 Jul 2022 06:09:49 +0200 Subject: [PATCH] Add more KDocs and nav args encoding --- .../apps/nowinandroid/ui/NiaAppState.kt | 23 +++++++++++++++---- .../author/navigation/AuthorNavigation.kt | 13 +++++++++-- .../topic/navigation/TopicNavigation.kt | 13 +++++++++-- 3 files changed, 41 insertions(+), 8 deletions(-) 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 = 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) } }