Conversation
Summary of ChangesHello @claraf3, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request undertakes a significant architectural update by migrating the Jetnews application to Navigation 3. This change streamlines how routes are defined and managed, introduces a more robust navigation state, and sets up a clear pattern for handling navigation actions. The update also includes foundational support for deeplinking, enhancing the app's ability to respond to external navigation requests. Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request successfully migrates the app to Navigation 3, introducing type-safe navigation with NavKeys and a custom state management solution for handling bottom navigation and deeplinks. The refactoring is extensive and well-executed. However, I've identified a critical issue in the rememberNavigationState implementation where remember is called inside a loop, which violates Compose's rules and can lead to unpredictable behavior. I've also included a couple of medium-severity suggestions to improve code robustness and structure. Overall, great work on this significant migration.
| fun rememberNavigationState(startKey: NavKey): NavigationState { | ||
| val currentKeys = rememberNavBackStack(startKey) | ||
| val backStacks = buildMap { | ||
| getNavigationKeys(startKey) | ||
| .forEach { | ||
| put(it::class.toString(), rememberNavBackStack(it)) | ||
| } | ||
| }.toMutableMap() | ||
| return remember(startKey) { NavigationState(currentKeys, backStacks) } | ||
| } |
There was a problem hiding this comment.
Calling a @Composable function like rememberNavBackStack inside a loop (forEach) is a violation of Compose rules. Composable functions must be called from another composable function's scope, not from a regular lambda. This can lead to incorrect state management, performance issues, and unpredictable behavior.
The back stack for each root destination (tab) should be remembered at the top level of the composable. To handle deeplinks correctly, you can use a LaunchedEffect to initialize the specific back stack when startKey changes.
@Composable
fun rememberNavigationState(startKey: NavKey): NavigationState {
val currentKeys = rememberNavBackStack(startKey)
// Remember a NavBackStack for each root destination.
// It's a rule of Compose to call remember functions at the top level, not in loops.
val homeBackStack = rememberNavBackStack(Home())
val interestsBackStack = rememberNavBackStack(Interests)
val backStacks = remember(homeBackStack, interestsBackStack) {
mutableMapOf(
Home::class.toString() to homeBackStack,
Interests::class.toString() to interestsBackStack
)
}
// Use a LaunchedEffect to handle deeplink initialization safely.
// This will run when the composable first enters the composition and if startKey changes.
LaunchedEffect(startKey) {
val targetBackStack = backStacks[startKey::class.toString()]
if (targetBackStack != null && targetBackStack.lastOrNull() != startKey) {
targetBackStack.clear()
targetBackStack.add(startKey)
}
}
return remember(currentKeys, backStacks) { NavigationState(currentKeys, backStacks) }
}
JetNews/app/src/main/java/com/example/jetnews/ui/navigation/JetnewsNavDisplay.kt
Outdated
Show resolved
Hide resolved
JetNews/app/src/main/java/com/example/jetnews/ui/navigation/JetnewsNavigation.kt
Show resolved
Hide resolved
35348de to
c30410b
Compare
c30410b to
403b89e
Compare
|
@dturner Hi Don I wasn't sure how complicated you wanted the deepLink implementation to be. In a small app with only one deep link, I imagine the implementation would probably be similar to what I have right now - a small helper function to parse the postId. |
Migration to Navigation 3 with simple deeplink implementation