Skip to content

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

srikrishnasakunia
Copy link

@srikrishnasakunia srikrishnasakunia commented Aug 11, 2025

Addresses #88

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a 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

  1. 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.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a 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.

originalImageUrl: Uri?,
) {
_state.update {
CustomizeExportState(
originalImageUrl,
exportImageCanvas = it.exportImageCanvas.copy(imageBitmap = resultImageUrl),
exportImageCanvas = it.exportImageCanvas.copy(imageUri = resultImageUrl),
Copy link
Collaborator

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.

Copy link
Collaborator

@riggaroo riggaroo left a 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.

Copy link
Contributor

@dturner dturner left a 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

@riggaroo riggaroo changed the title Krishna/nav3 result screen imp Migrate ResultsScreen to use Nav3 Aug 13, 2025
…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`.
@srikrishnasakunia srikrishnasakunia force-pushed the krishna/nav3_resultScreenImp branch from 660d8e5 to 500f5dc Compare August 18, 2025 08:46
@riggaroo
Copy link
Collaborator

/gemini review

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a 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.

Comment on lines +134 to +136
} catch (e: Exception) {
e.printStackTrace()
null
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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.

Suggested change
} catch (e: Exception) {
e.printStackTrace()
null
} catch (e: Exception) {
android.util.Log.e("LocalFileProviderImpl", "Error loading bitmap from URI: $uri", e)
null
}

Comment on lines 80 to 90
fun setArguments(
resultImageUrl: Bitmap,
resultImageUrl: Uri,
originalImageUrl: Uri?,
) {
_state.update {
CustomizeExportState(
originalImageUrl,
exportImageCanvas = it.exportImageCanvas.copy(imageBitmap = resultImageUrl),
exportImageCanvas = it.exportImageCanvas.copy(imageUri = resultImageUrl),
)
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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.")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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).

Suggested change
_snackbarHostState.value.showSnackbar("Could not load image.")
_snackbarHostState.value.showSnackbar(getApplication<Application>().getString(R.string.error_could_not_load_image))

Comment on lines +78 to +80
/*resultImageUri: Uri,
originalImageUri: Uri?,
promptText: String?,
promptText: String?,*/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

These commented-out parameters in the function signature should be removed to improve code clarity and maintainability.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

Comment on lines +89 to +91
/*LaunchedEffect(resultImageUri, originalImageUri, promptText) {
viewModel.setArguments(resultImageUri, originalImageUri, promptText)
}*/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This commented-out LaunchedEffect should be removed to keep the codebase clean and avoid confusion.

Copy link
Collaborator

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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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.

Suggested change
fun setArgumentsWithOriginalImage() = runTest {
fun initialState_withOriginalImage_isCorrect() = runTest {

Copy link
Collaborator

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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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.

Suggested change
fun setArgumentsWithPrompt() = runTest {
fun initialState_withPrompt_isCorrect() = runTest {

@riggaroo
Copy link
Collaborator

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.

Copy link
Collaborator

@riggaroo riggaroo left a 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,
Copy link
Collaborator

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()
Copy link
Collaborator

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.

Comment on lines +78 to +80
/*resultImageUri: Uri,
originalImageUri: Uri?,
promptText: String?,
promptText: String?,*/
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

Comment on lines +89 to +91
/*LaunchedEffect(resultImageUri, originalImageUri, promptText) {
viewModel.setArguments(resultImageUri, originalImageUri, promptText)
}*/
Copy link
Collaborator

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{
Copy link
Collaborator

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 {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants