Add more KDocs and nav args encoding

pull/186/head
Manuel Vivo 2 years ago
parent 872a7a6523
commit ea07e97a07

@ -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 ->

@ -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)
}
}

@ -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)
}
}

Loading…
Cancel
Save