-
Notifications
You must be signed in to change notification settings - Fork 161
Migrate ResultsScreen to use Nav3 #95
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
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.
Summary of Changes
Hello @srikrishnasakunia, 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!
I've undertaken a significant refactoring effort to enhance the application's image handling and navigation. The primary goal was to move away from directly passing large Bitmap objects between screens, which can be inefficient and lead to memory issues. Instead, I've implemented a system that utilizes Android's Uri (Uniform Resource Identifier) for image results. This change not only improves memory management but also streamlines the overall navigation flow from image creation to the display of results and subsequent customization, making the application more robust and performant.
Highlights
- Image Handling Refactor: I've refactored the application's image handling to switch from passing Bitmap objects directly to using Uri objects for image results across the application. This change impacts the CreationViewModel, ResultsScreen, and CustomizeAndExportScreen, improving memory management and enabling better integration with Android's content URI system.
- Navigation Flow Update: I've introduced new ImageResult and ShareResult navigation routes in NavigationRoutes.kt. These routes are now used to manage the flow from image creation to result display and customization, ensuring that image data is passed efficiently using Uris.
- Decoupling UI from Bitmap: I've removed direct Bitmap references from UI composables and ViewModels in the 'feature/results' and 'feature/creation' modules, replacing them with Uris. Bitmaps are now loaded from Uris only when explicitly needed for display or processing, which helps in decoupling the UI from heavy image data.
- Streamlined Creation to Results Transition: The CreationScreen now utilizes an 'onImageCreated' callback to trigger navigation to the ResultsScreen with the generated image's Uri. This streamlines the transition, eliminating the need for CreationScreen to directly manage the states of ResultsScreen and CustomizeAndExportScreen.
- Test Updates: Corresponding unit and instrumentation tests across the application have been updated to reflect the change from Bitmap to Uri for image handling, ensuring continued correctness and stability of the features.
Using Gemini Code Assist
The 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 in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command>
or @gemini-code-assist <command>
. Below is a summary of the supported commands.
Feature | Command | Description |
---|---|---|
Code Review | /gemini review |
Performs a code review for the current pull request in its current state. |
Pull Request Summary | /gemini summary |
Provides a summary of the current pull request in its current state. |
Comment | @gemini-code-assist | Responds in comments when explicitly tagged, both in issue comments and review comments. |
Help | /gemini help |
Displays a list of available commands. |
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 .gemini/
folder in the base of the repository. Detailed instructions can be found here.
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 or fill out our survey to provide feedback.
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
-
Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩
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 implements the results screen and integrates it into the navigation flow using navigation3. The changes involve passing image data as URIs instead of Bitmaps, which is a great improvement for performance and stability. The navigation logic has been updated to include the new screens, and feature modules have been decoupled from navigation concerns. I've found a critical issue in the navigation logic that could lead to an incorrect back stack, and a high-severity issue regarding a potential resource leak. Other suggestions are minor improvements to code clarity and best practices.
app/src/main/java/com/android/developers/androidify/navigation/MainNavigation.kt
Outdated
Show resolved
Hide resolved
...esults/src/main/java/com/android/developers/androidify/customize/CustomizeExportViewModel.kt
Outdated
Show resolved
Hide resolved
.../results/src/androidTest/java/com/android/developers/androidify/results/ResultsScreenTest.kt
Outdated
Show resolved
Hide resolved
...esults/src/main/java/com/android/developers/androidify/customize/CustomizeExportViewModel.kt
Outdated
Show resolved
Hide resolved
originalImageUrl: Uri?, | ||
) { | ||
_state.update { | ||
CustomizeExportState( | ||
originalImageUrl, | ||
exportImageCanvas = it.exportImageCanvas.copy(imageBitmap = resultImageUrl), | ||
exportImageCanvas = it.exportImageCanvas.copy(imageUri = resultImageUrl), |
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.
I think this is where its losing state on restoring from background. we can keep the imageUri, but we should probably add the loaded image to the State to be able to draw it.
So instead of changing the imageBitmap to imageUri, lets add an imageUri parameter, and then on load of the bitmap, we set the state to that Bitmap.
When it comes back from being in the background, we will need to rehydrate the state to load up the bitmap again.
...e/results/src/main/java/com/android/developers/androidify/customize/CustomizeExportScreen.kt
Outdated
Show resolved
Hide resolved
feature/creation/src/main/java/com/android/developers/androidify/creation/CreationScreen.kt
Outdated
Show resolved
Hide resolved
...e/results/src/main/java/com/android/developers/androidify/customize/CustomizeExportScreen.kt
Outdated
Show resolved
Hide resolved
app/src/main/java/com/android/developers/androidify/navigation/NavigationRoutes.kt
Outdated
Show resolved
Hide resolved
app/src/main/java/com/android/developers/androidify/navigation/MainNavigation.kt
Outdated
Show resolved
Hide resolved
feature/results/src/main/java/com/android/developers/androidify/results/BotResultCard.kt
Outdated
Show resolved
Hide resolved
feature/results/src/main/java/com/android/developers/androidify/results/ResultsScreen.kt
Outdated
Show resolved
Hide resolved
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.
Getting there! left some comments.
app/src/main/java/com/android/developers/androidify/navigation/MainNavigation.kt
Outdated
Show resolved
Hide resolved
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.
Took an initial pass
app/src/main/java/com/android/developers/androidify/navigation/NavigationRoutes.kt
Outdated
Show resolved
Hide resolved
app/src/main/java/com/android/developers/androidify/navigation/NavigationRoutes.kt
Outdated
Show resolved
Hide resolved
app/src/main/java/com/android/developers/androidify/navigation/NavigationRoutes.kt
Outdated
Show resolved
Hide resolved
app/src/main/java/com/android/developers/androidify/navigation/NavigationRoutes.kt
Outdated
Show resolved
Hide resolved
app/src/main/java/com/android/developers/androidify/navigation/NavigationRoutes.kt
Outdated
Show resolved
Hide resolved
app/src/main/java/com/android/developers/androidify/navigation/MainNavigation.kt
Outdated
Show resolved
Hide resolved
feature/creation/src/main/java/com/android/developers/androidify/creation/CreationScreen.kt
Outdated
Show resolved
Hide resolved
.../results/src/androidTest/java/com/android/developers/androidify/results/ResultsScreenTest.kt
Outdated
Show resolved
Hide resolved
...esults/src/main/java/com/android/developers/androidify/customize/CustomizeExportViewModel.kt
Outdated
Show resolved
Hide resolved
feature/results/src/main/java/com/android/developers/androidify/results/BotResultCard.kt
Outdated
Show resolved
Hide resolved
…vigation.kt using Nav3 instead of driving them from CreationScreen.kt # Conflicts: # feature/results/src/main/java/com/android/developers/androidify/customize/ImageRenderer.kt # Conflicts: # feature/results/src/main/java/com/android/developers/androidify/customize/CustomizeState.kt
This commit refactors image handling in the `CreationViewModel` and `CustomizeExportViewModel`. **CreationViewModel:** - The `saveBitmapToCache` function has been removed. - Image saving is now delegated to `imageGenerationRepository.saveImage(bitmap)`. **CustomizeExportViewModel:** - Images are now loaded from a URI using a new `convertUriToBitmap(uri)` suspend function. This function uses the `application.contentResolver` to open an input stream from the URI and decodes it into a Bitmap. - The `export()` function was updated to use `convertUriToBitmap` to get the image for export instead of directly accessing `state.value.exportImageCanvas.imageBitmap`.
660d8e5
to
500f5dc
Compare
/gemini review |
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 successfully migrates the ResultsScreen
and its related components to use Nav3, which is a great improvement for centralizing navigation logic. The use of assisted injection for ViewModels and passing Uri
objects as navigation arguments cleans up the state management considerably. The changes are well-implemented across various modules. I've included a few suggestions to enhance code quality, such as improving error logging, removing hardcoded strings, and cleaning up commented-out code.
} catch (e: Exception) { | ||
e.printStackTrace() | ||
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.
e.printStackTrace()
should be avoided in production code. It prints to the standard error stream and can be lost, especially in release builds where logs might be stripped. Using a structured logging framework like android.util.Log
is a better practice for error handling as it allows for tagging and filtering of log messages.
} catch (e: Exception) { | |
e.printStackTrace() | |
null | |
} catch (e: Exception) { | |
android.util.Log.e("LocalFileProviderImpl", "Error loading bitmap from URI: $uri", e) | |
null | |
} |
fun setArguments( | ||
resultImageUrl: Bitmap, | ||
resultImageUrl: Uri, | ||
originalImageUrl: Uri?, | ||
) { | ||
_state.update { | ||
CustomizeExportState( | ||
originalImageUrl, | ||
exportImageCanvas = it.exportImageCanvas.copy(imageBitmap = resultImageUrl), | ||
exportImageCanvas = it.exportImageCanvas.copy(imageUri = resultImageUrl), | ||
) | ||
} | ||
} |
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 setArguments
function appears to be redundant now that the ViewModel's initial state is configured through assisted injection in the init
block. This function is only used in tests and its logic is a subset of the init
block's. To avoid confusion and potential misuse, it's best to remove it and update the tests to rely solely on ViewModel instantiation for setup.
) | ||
} | ||
} catch (e: Exception) { | ||
_snackbarHostState.value.showSnackbar("Could not load image.") |
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 snackbar message is hardcoded. For better maintainability and to support localization, it's recommended to extract this string into a string resource file (strings.xml
).
_snackbarHostState.value.showSnackbar("Could not load image.") | |
_snackbarHostState.value.showSnackbar(getApplication<Application>().getString(R.string.error_could_not_load_image)) |
/*resultImageUri: Uri, | ||
originalImageUri: Uri?, | ||
promptText: String?, | ||
promptText: String?,*/ |
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.
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.
+1
/*LaunchedEffect(resultImageUri, originalImageUri, promptText) { | ||
viewModel.setArguments(resultImageUri, originalImageUri, promptText) | ||
}*/ |
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.
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.
+1
@@ -55,14 +61,9 @@ class ResultsViewModelTest { | |||
|
|||
@Test | |||
fun setArgumentsWithOriginalImage() = runTest { |
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 test name setArgumentsWithOriginalImage
is no longer accurate because the setArguments
method has been removed in favor of constructor injection. This test now verifies the initial state of the ViewModel. Renaming it would make its purpose clearer.
fun setArgumentsWithOriginalImage() = runTest { | |
fun initialState_withOriginalImage_isCorrect() = runTest { |
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.
+1
@@ -71,14 +72,9 @@ | |||
|
|||
@Test | |||
fun setArgumentsWithPrompt() = runTest { |
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.
Similar to the previous comment, the test name setArgumentsWithPrompt
is now misleading. It should be updated to reflect that it tests the ViewModel's initial state when created with a prompt.
fun setArgumentsWithPrompt() = runTest { | |
fun initialState_withPrompt_isCorrect() = runTest { |
Bug: On back press from CustomizeExport from the Back arrow, it goes back to Create screen, it should probably go back to the Result screen first, then back to Create after that. |
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.
Looking great :) Small nits but overall getting there!
*/ | ||
@Serializable | ||
data class Result( | ||
@Serializable(with = UriSerializer::class) val resultImageUri: Uri, |
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.
nit: I wonder if there is a way to register the UriSerializer for Nav3? @dturner
@@ -231,11 +234,11 @@ private fun ImageRendererPreviewSquare() { | |||
@Preview | |||
@Composable | |||
private fun ImageRendererPreviewBanner() { | |||
val bitmap = ImageBitmap.imageResource(R.drawable.placeholderbot) | |||
val imageUri = ("${ContentResolver.SCHEME_ANDROID_RESOURCE}://${LocalContext.current.packageName}/${R.drawable.placeholderbot}").toUri() |
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.
Let's extract this placeholder bot Uri to a PreviewTestData.kt file and call it placeholderBot - since you are duplicating the logic in many places.
/*resultImageUri: Uri, | ||
originalImageUri: Uri?, | ||
promptText: String?, | ||
promptText: String?,*/ |
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.
+1
/*LaunchedEffect(resultImageUri, originalImageUri, promptText) { | ||
viewModel.setArguments(resultImageUri, originalImageUri, promptText) | ||
}*/ |
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.
+1
originalImageUrl: Uri?, | ||
promptText: String?, | ||
) { | ||
init{ |
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.
please run spotless - ./gradlew spotlessApply
@@ -55,14 +61,9 @@ class ResultsViewModelTest { | |||
|
|||
@Test | |||
fun setArgumentsWithOriginalImage() = runTest { |
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.
+1
Addresses #88