-
Notifications
You must be signed in to change notification settings - Fork 81
Refactor conditional navigation recipe to make it saveable. #163
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
base: main
Are you sure you want to change the base?
Conversation
Summary of ChangesHello @dturner, 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 significantly refactors the conditional navigation recipe to ensure its state is fully saveable and restorable, addressing issue #147. By transitioning from a custom Highlights
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request refactors the conditional navigation logic to be saveable, which is a great improvement. The separation of concerns into a Navigator class is well done. However, there is a critical issue where a piece of navigation state (onLoginSuccessRoute) is not saved, which defeats the purpose of the refactor on process death. I've also identified a high-severity bug in the login logic and a medium-severity performance issue. My review provides detailed suggestions to address these points and make the implementation robust.
| private var isLoggedIn by isLoggedInState | ||
|
|
||
| // The route that we will navigate to after successful login. | ||
| private var onLoginSuccessRoute: Route? = null |
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.
The onLoginSuccessRoute property is not persisted across process death. This breaks the conditional navigation flow if the app is killed while on the login screen, defeating the purpose of this refactor.
To fix this, this state should be hoisted to ConditionalActivity and stored using rememberSaveable. The Navigator would then accept this state in its constructor.
In ConditionalActivity.kt:
val onLoginSuccessRoute = rememberSaveable { mutableStateOf<Route?>(null) }
val navigator = remember { // ...
Navigator(
// ...
onLoginSuccessRouteState = onLoginSuccessRoute
)
}In Navigator.kt:
class Navigator(
// ...
onLoginSuccessRouteState: MutableState<Route?>,
) {
// ...
private var onLoginSuccessRoute by onLoginSuccessRouteState
// ...
}Since this change spans multiple files, I'm providing the explanation and examples here. Please apply these changes to make the navigation state fully saveable.
| fun login() { | ||
| isLoggedIn = true | ||
| onLoginSuccessRoute?.let { | ||
| backStack.add(it) | ||
| backStack.remove(loginRoute) | ||
| } | ||
| } |
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.
The login() function has two issues:
- If a user navigates to the login screen manually (so
onLoginSuccessRouteisnull), after a successful login, they remain on the login screen. The login screen should be popped from the back stack. onLoginSuccessRouteis not cleared after being used. This could lead to unexpected redirection later if the user logs out and logs in again.
fun login() {
isLoggedIn = true
onLoginSuccessRoute?.let { destination ->
backStack.add(destination)
backStack.remove(loginRoute)
onLoginSuccessRoute = null
} ?: run {
// When login is successful, we should at least pop the login screen.
if (backStack.lastOrNull() == loginRoute) {
backStack.removeLast()
}
}
}| val navigator = Navigator( | ||
| backStack = backStack, | ||
| loginRoute = Login, | ||
| isLoggedInState = isLoggedInState | ||
| ) |
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.
The Navigator instance is created on every recomposition. This is inefficient and can lead to subtle bugs. It should be wrapped in remember to ensure the same instance is used across recompositions.
val navigator = remember(backStack, isLoggedInState) {
Navigator(
backStack = backStack,
loginRoute = Login,
isLoggedInState = isLoggedInState
)
}| // We use a sealed class for the route supertype because KotlinX Serialization handles polymorphic | ||
| // serialization of sealed classes automatically. | ||
| @Serializable | ||
| sealed class Route(val requiresLogin: Boolean = false) : NavKey |
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.
Personally, I'm not really a fan of using "Route" in the context of nav3 because we don't use that term at all in nav3 library (kdocs or naming). I think sticking to "Key" helps clarify its role and where its used in the APIs.
Fixes #147