Add specific type for deeplinks

Change-Id: I63c0abb16cbf540ef72dfb945518a48113bdbf75
dt/nav-safe-args-android-dependency
Don Turner 8 months ago
parent 3cff2fbda2
commit 4a84cf2bf4

@ -44,7 +44,7 @@ fun NiaNavHost(
val navController = appState.navController val navController = appState.navController
NavHost( NavHost(
navController = navController, navController = navController,
startDestination = ForYouDestination(linkedNewsResourceId = null), startDestination = ForYouDestination(),
modifier = modifier, modifier = modifier,
) { ) {
forYouScreen(onTopicClick = navController::navigateToInterests) forYouScreen(onTopicClick = navController::navigateToInterests)

@ -30,14 +30,17 @@ const val LINKED_NEWS_RESOURCE_ID = "linkedNewsResourceId"
private const val DEEP_LINK_BASE_PATH = "$DEEP_LINK_SCHEME_AND_HOST/$FOR_YOU_PATH" private const val DEEP_LINK_BASE_PATH = "$DEEP_LINK_SCHEME_AND_HOST/$FOR_YOU_PATH"
@Serializable data class ForYouDestination(val linkedNewsResourceId: String?) @Serializable data class ForYouDestination(val linkedNewsResourceId: String? = null)
// For deeplinks the news resource ID cannot be null so we use a different type with the same
// argument name to enforce this requirement.
@Serializable data class ForYouDeeplink(val linkedNewsResourceId: String)
fun NavController.navigateToForYou(navOptions: NavOptions) = navigate(route = ForYouDestination(linkedNewsResourceId = null), navOptions) fun NavController.navigateToForYou(navOptions: NavOptions) = navigate(route = ForYouDestination(), navOptions)
fun NavGraphBuilder.forYouScreen(onTopicClick: (String) -> Unit) { fun NavGraphBuilder.forYouScreen(onTopicClick: (String) -> Unit) {
composable<ForYouDestination>( composable<ForYouDestination>(
deepLinks = listOf( deepLinks = listOf(
navDeepLink<ForYouDestination>(basePath = DEEP_LINK_BASE_PATH), navDeepLink<ForYouDeeplink>(basePath = DEEP_LINK_BASE_PATH),
), ),
) { ) {
ForYouRoute(onTopicClick) ForYouRoute(onTopicClick)

Loading…
Cancel
Save