-
Notifications
You must be signed in to change notification settings - Fork 5.4k
JetNews: Migrate from Navigation 2 to Navigation 3 #1644
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -23,30 +23,36 @@ import androidx.compose.material3.ModalNavigationDrawer | |||||||||||||
| import androidx.compose.material3.rememberDrawerState | ||||||||||||||
| import androidx.compose.material3.windowsizeclass.WindowWidthSizeClass | ||||||||||||||
| import androidx.compose.runtime.Composable | ||||||||||||||
| import androidx.compose.runtime.getValue | ||||||||||||||
| import androidx.compose.runtime.mutableStateListOf | ||||||||||||||
| import androidx.compose.runtime.remember | ||||||||||||||
| import androidx.compose.runtime.rememberCoroutineScope | ||||||||||||||
| import androidx.navigation.compose.currentBackStackEntryAsState | ||||||||||||||
| import androidx.navigation.compose.rememberNavController | ||||||||||||||
| import com.example.jetnews.data.AppContainer | ||||||||||||||
| import com.example.jetnews.ui.components.AppNavRail | ||||||||||||||
| import com.example.jetnews.ui.theme.JetnewsTheme | ||||||||||||||
| import kotlinx.coroutines.launch | ||||||||||||||
|
|
||||||||||||||
| @Composable | ||||||||||||||
| fun JetnewsApp(appContainer: AppContainer, widthSizeClass: WindowWidthSizeClass) { | ||||||||||||||
| fun JetnewsApp( | ||||||||||||||
| appContainer: AppContainer, | ||||||||||||||
| widthSizeClass: WindowWidthSizeClass, | ||||||||||||||
| startRoute: Home = Home(), | ||||||||||||||
| ) { | ||||||||||||||
| JetnewsTheme { | ||||||||||||||
| val navController = rememberNavController() | ||||||||||||||
| val navigationActions = remember(navController) { | ||||||||||||||
| JetnewsNavigationActions(navController) | ||||||||||||||
| val backStack = remember { mutableStateListOf<JetnewsRoute>(startRoute) } | ||||||||||||||
| val currentRoute: JetnewsRoute = backStack.lastOrNull() ?: Home() | ||||||||||||||
|
|
||||||||||||||
| val navigateToHome: () -> Unit = { | ||||||||||||||
| // Pop everything back to Home (the start destination) | ||||||||||||||
| while (backStack.size > 1) backStack.removeLast() | ||||||||||||||
| } | ||||||||||||||
| val navigateToInterests: () -> Unit = { | ||||||||||||||
| // Pop to Home, then add Interests on top (single top behavior) | ||||||||||||||
| while (backStack.size > 1) backStack.removeLast() | ||||||||||||||
| backStack.add(Interests) | ||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The current implementation for To optimize this and align closer with
Suggested change
|
||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| val coroutineScope = rememberCoroutineScope() | ||||||||||||||
|
|
||||||||||||||
| val navBackStackEntry by navController.currentBackStackEntryAsState() | ||||||||||||||
| val currentRoute = | ||||||||||||||
| navBackStackEntry?.destination?.route ?: JetnewsDestinations.HOME_ROUTE | ||||||||||||||
|
|
||||||||||||||
| val isExpandedScreen = widthSizeClass == WindowWidthSizeClass.Expanded | ||||||||||||||
| val sizeAwareDrawerState = rememberSizeAwareDrawerState(isExpandedScreen) | ||||||||||||||
|
|
||||||||||||||
|
|
@@ -55,8 +61,8 @@ fun JetnewsApp(appContainer: AppContainer, widthSizeClass: WindowWidthSizeClass) | |||||||||||||
| AppDrawer( | ||||||||||||||
| drawerState = sizeAwareDrawerState, | ||||||||||||||
| currentRoute = currentRoute, | ||||||||||||||
| navigateToHome = navigationActions.navigateToHome, | ||||||||||||||
| navigateToInterests = navigationActions.navigateToInterests, | ||||||||||||||
| navigateToHome = navigateToHome, | ||||||||||||||
| navigateToInterests = navigateToInterests, | ||||||||||||||
| closeDrawer = { coroutineScope.launch { sizeAwareDrawerState.close() } }, | ||||||||||||||
| ) | ||||||||||||||
| }, | ||||||||||||||
|
|
@@ -68,14 +74,14 @@ fun JetnewsApp(appContainer: AppContainer, widthSizeClass: WindowWidthSizeClass) | |||||||||||||
| if (isExpandedScreen) { | ||||||||||||||
| AppNavRail( | ||||||||||||||
| currentRoute = currentRoute, | ||||||||||||||
| navigateToHome = navigationActions.navigateToHome, | ||||||||||||||
| navigateToInterests = navigationActions.navigateToInterests, | ||||||||||||||
| navigateToHome = navigateToHome, | ||||||||||||||
| navigateToInterests = navigateToInterests, | ||||||||||||||
| ) | ||||||||||||||
| } | ||||||||||||||
| JetnewsNavGraph( | ||||||||||||||
| appContainer = appContainer, | ||||||||||||||
| isExpandedScreen = isExpandedScreen, | ||||||||||||||
| navController = navController, | ||||||||||||||
| backStack = backStack, | ||||||||||||||
| openDrawer = { coroutineScope.launch { sizeAwareDrawerState.open() } }, | ||||||||||||||
| ) | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When the
backStackbecomes empty (e.g., when the user presses back on the last screen),currentRoutedefaults toHome(). This causesAppDrawerandAppNavRailto show "Home" as selected, whileNavDisplayshows a blank screen because its backstack is empty. This creates a UI inconsistency.To fix this,
currentRouteshould be nullable. When it'snull, no navigation item will be selected, which correctly reflects the empty state.You'll also need to update the
currentRouteparameter inAppDrawerandAppNavRailto be nullable (JetnewsRoute?). Theischecks for selection will continue to work correctly.