Migrate ForYou feature to Nav3

dt/nav3-c
Don Turner 2 months ago
parent 572aaed590
commit ec2b09caca

@ -20,12 +20,14 @@ import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.navigation
import androidx.navigation3.runtime.NavEntry
import androidx.navigation3.runtime.entryProvider
import androidx.navigation3.ui.NavDisplay
import com.google.samples.apps.nowinandroid.feature.bookmarks.api.navigation.BookmarksRoute
import com.google.samples.apps.nowinandroid.feature.bookmarks.impl.navigation.bookmarksScreen
import com.google.samples.apps.nowinandroid.feature.foryou.navigation.ForYouBaseRoute
import com.google.samples.apps.nowinandroid.feature.foryou.navigation.ForYouRoute
import com.google.samples.apps.nowinandroid.feature.foryou.navigation.forYouSection
import com.google.samples.apps.nowinandroid.feature.interests.navigation.InterestsRoute
import com.google.samples.apps.nowinandroid.feature.search.navigation.SearchRoute
@ -63,13 +65,11 @@ fun NiaNavHost(
startDestination = ForYouBaseRoute,
modifier = modifier,
) {
forYouSection(
onTopicClick = {
nav3Navigator.goTo(route = TopicRoute(it))
},
) {
composable<TopicRoute>{}
navigation<ForYouBaseRoute>(startDestination = ForYouRoute) {
composable<ForYouRoute> {}
composable<TopicRoute> {}
}
composable<ForYouRoute> {}
composable<BookmarksRoute> {}
composable<SearchRoute> {}
composable<InterestsRoute>{}
@ -77,6 +77,11 @@ fun NiaNavHost(
}
},
) {
forYouSection(
onTopicClick = {
nav3Navigator.goTo(route = TopicRoute(it))
},
)
bookmarksScreen(
onTopicClick = { it: String ->
nav3Navigator.goTo(route = InterestsRoute(it))

@ -39,7 +39,7 @@ import com.google.samples.apps.nowinandroid.core.data.util.NetworkMonitor
import com.google.samples.apps.nowinandroid.core.data.util.TimeZoneMonitor
import com.google.samples.apps.nowinandroid.core.ui.TrackDisposableJank
import com.google.samples.apps.nowinandroid.feature.bookmarks.api.navigation.BookmarksRoute
import com.google.samples.apps.nowinandroid.feature.foryou.navigation.navigateToForYou
import com.google.samples.apps.nowinandroid.feature.foryou.navigation.ForYouRoute
import com.google.samples.apps.nowinandroid.feature.interests.navigation.InterestsRoute
import com.google.samples.apps.nowinandroid.feature.search.navigation.SearchRoute
import com.google.samples.apps.nowinandroid.feature.search.navigation.navigateToSearch
@ -185,7 +185,7 @@ class NiaAppState(
}
when (topLevelDestination) {
FOR_YOU -> navController.navigateToForYou(topLevelNavOptions)
FOR_YOU -> nav3Navigator.goTo(route = ForYouRoute, topLevelNavOptions)
BOOKMARKS -> nav3Navigator.goTo(route = BookmarksRoute, topLevelNavOptions)
INTERESTS -> {
nav3Navigator.goTo(route = InterestsRoute(null), topLevelNavOptions)
@ -264,6 +264,8 @@ class Nav3NavigatorSimple(val navController: NavHostController){
entry.toRoute<SearchRoute>()
} else if (destination.hasRoute<InterestsRoute>()) {
entry.toRoute<InterestsRoute>()
} else if (destination.hasRoute<ForYouRoute>()) {
entry.toRoute<ForYouRoute>()
} else {
// Non migrated top level route
println("Non migrated destination: $destination")

@ -29,6 +29,7 @@ dependencies {
implementation(libs.accompanist.permissions)
implementation(projects.core.data)
implementation(projects.core.domain)
implementation(projects.core.navigation)
implementation(projects.core.notifications)
testImplementation(libs.hilt.android.testing)

@ -16,12 +16,8 @@
package com.google.samples.apps.nowinandroid.feature.foryou.navigation
import androidx.navigation.NavController
import androidx.navigation.NavGraphBuilder
import androidx.navigation.NavOptions
import androidx.navigation.compose.composable
import androidx.navigation.compose.navigation
import androidx.navigation.navDeepLink
import androidx.navigation3.runtime.EntryProviderBuilder
import androidx.navigation3.runtime.entry
import com.google.samples.apps.nowinandroid.core.notifications.DEEP_LINK_URI_PATTERN
import com.google.samples.apps.nowinandroid.feature.foryou.ForYouScreen
import kotlinx.serialization.Serializable
@ -30,36 +26,30 @@ import kotlinx.serialization.Serializable
@Serializable data object ForYouBaseRoute // route to base navigation graph
fun NavController.navigateToForYou(navOptions: NavOptions) = navigate(route = ForYouRoute, navOptions)
/**
* The ForYou section of the app. It can also display information about topics.
* This should be supplied from a separate module.
*
* @param onTopicClick - Called when a topic is clicked, contains the ID of the topic
* @param topicDestination - Destination for topic content
*/
fun NavGraphBuilder.forYouSection(
onTopicClick: (String) -> Unit,
topicDestination: NavGraphBuilder.() -> Unit,
fun EntryProviderBuilder<Any>.forYouSection(
onTopicClick: (String) -> Unit
) {
navigation<ForYouBaseRoute>(startDestination = ForYouRoute) {
composable<ForYouRoute>(
deepLinks = listOf(
navDeepLink {
/**
* This destination has a deep link that enables a specific news resource to be
* opened from a notification (@see SystemTrayNotifier for more). The news resource
* ID is sent in the URI rather than being modelled in the route type because it's
* transient data (stored in SavedStateHandle) that is cleared after the user has
* opened the news resource.
*/
uriPattern = DEEP_LINK_URI_PATTERN
},
),
) {
ForYouScreen(onTopicClick)
}
topicDestination()
entry<ForYouRoute>(
/*deepLinks = listOf(
navDeepLink {
*//**
* This destination has a deep link that enables a specific news resource to be
* opened from a notification (@see SystemTrayNotifier for more). The news resource
* ID is sent in the URI rather than being modelled in the route type because it's
* transient data (stored in SavedStateHandle) that is cleared after the user has
* opened the news resource.
*//*
uriPattern = DEEP_LINK_URI_PATTERN
},
),*/
) {
ForYouScreen(onTopicClick)
}
}

@ -14,6 +14,8 @@
* limitations under the License.
*/
@file:OptIn(ExperimentalUuidApi::class)
package com.google.samples.apps.nowinandroid.feature.topic.navigation
import androidx.hilt.navigation.compose.hiltViewModel
@ -22,8 +24,10 @@ import androidx.navigation3.runtime.entry
import com.google.samples.apps.nowinandroid.feature.topic.TopicScreen
import com.google.samples.apps.nowinandroid.feature.topic.TopicViewModel
import kotlinx.serialization.Serializable
import kotlin.uuid.ExperimentalUuidApi
import kotlin.uuid.Uuid
@Serializable data class TopicRoute(val id: String)
@Serializable data class TopicRoute(val id: String, val uuid: String = Uuid.random().toString())
fun EntryProviderBuilder<Any>.topicScreen(
showBackButton: Boolean,

Loading…
Cancel
Save