Skip to content

Refactor GenAI Writing Assistance sample to use UI state - #75

Merged
calren merged 5 commits into
mainfrom
caren/writing_assistance_refactor
Aug 19, 2025
Merged

Refactor GenAI Writing Assistance sample to use UI state#75
calren merged 5 commits into
mainfrom
caren/writing_assistance_refactor

Conversation

@calren

@calren calren commented Aug 15, 2025

Copy link
Copy Markdown
Collaborator

No description provided.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Summary of Changes

Hello @calren, 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 refactors the GenAI Writing Assistance sample application to enhance its user interface by implementing a comprehensive UI state management system. This change shifts the display of generated text and operational feedback from a modal bottom sheet to an integrated, inline card, providing users with real-time updates on the status of AI operations, including model downloading, processing, and error handling.

Highlights

  • Centralized UI State Management: A sealed class GenAIWritingAssistanceUiState was introduced within the ViewModel to encapsulate all possible states of the writing assistance process, from initial loading and feature checks to active generation and error conditions.
  • Dynamic UI Feedback: The user interface now dynamically updates based on the ViewModel's uiState, displaying messages for feature downloading progress, generation in progress, and specific error conditions, significantly improving user transparency.
  • Streamlined Output Display: The ModalBottomSheet was replaced with a persistent Card component on the main screen to show the generated output and status messages, offering a more seamless and less intrusive user experience.
  • Robust Feature Download Handling: The ViewModel now integrates DownloadCallback to provide granular progress updates during the download of GenAI models, which are then accurately reflected in the UI state.
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. 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

  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.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

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 is a great step forward, refactoring the GenAI Writing Assistance sample to use a UI state object, which aligns with modern Android architecture patterns and improves maintainability. The implementation of a sealed class for UI state and the use of collectAsStateWithLifecycle are well executed.

My review focuses on further aligning the code with the provided style guide and enhancing code quality. The primary recommendation is to remove the dependency on AndroidViewModel and avoid passing Context to ViewModel methods, which will significantly improve testability. I've also included suggestions for improving code clarity and correctness, such as using val for immutable properties, removing redundant code blocks, and ensuring when statements are exhaustive.

@lethargicpanda lethargicpanda left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Made a comment about a comment. Looks good otherwise. Make sure that you fully test it for regressions before merging.

private val proofreader = Proofreading.getClient(
ProofreaderOptions.builder(context)
.setLanguage(ProofreaderOptions.Language.ENGLISH)
// If input was transcript of speech-to-text, this should be InputType.SPEECH

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Do we already support Speech to text?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

For example through SpeechRecognizer API

If the text input was from SpeechRecognizer's output, the model would look for different kinds of errors (ie: their instead of there)

Modifier.weight(1f).padding(5.dp),
Modifier
.weight(1f)
.padding(5.dp),

@ksemenova ksemenova Aug 18, 2025

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

nit: can we use 4 or 8?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

updated to 4

showBottomSheet = true
viewModel.proofread(textInput, context)
},
Modifier.padding(10.dp),

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

nit: can we use 8 to align with the 8-point grid system in UI design

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

updated to 8


private suspend fun runProofreadingInference(textToProofread: String) {
val proofreadRequest = ProofreadingRequest.builder(textToProofread).build()
// More than 1 result may be generated. Results are returned in descending order of

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

can this be configured in the request?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

no, you currently can't specify how many results to be returned, but that's a good feature request. i will something

// quality of confidence. Here we use the first result which has the highest quality
// of confidence.
_uiState.value = GenAIWritingAssistanceUiState.Generating
val results = proofreader.runInference(proofreadRequest).await()

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

should this be wrapped in try catch block as well?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

i haven't seen this throw an exception yet, so i'll leave out the try/catch for now. It'll help me understand situations where try/catch is necessary if it does end up needing it

// quality of confidence. Here we use the first result which has the highest quality of
// confidence.
_uiState.value = GenAIWritingAssistanceUiState.Generating
val results = rewriter.runInference(rewriteRequest).await()

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

same here: do we need a try catch?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

same thought about not adding a try/catch until we understand when an exception might be thrown

@calren
calren merged commit 3b59124 into main Aug 19, 2025
5 checks passed
@calren
calren deleted the caren/writing_assistance_refactor branch August 19, 2025 18:22
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