Refactor GenAI Writing Assistance sample to use UI state - #75
Conversation
There was a problem hiding this comment.
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 GenAIWritingAssistanceUiStatewas 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
ModalBottomSheetwas replaced with a persistentCardcomponent 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
DownloadCallbackto 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
-
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.
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
left a comment
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
Do we already support Speech to text?
There was a problem hiding this comment.
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), |
There was a problem hiding this comment.
nit: can we use 4 or 8?
| showBottomSheet = true | ||
| viewModel.proofread(textInput, context) | ||
| }, | ||
| Modifier.padding(10.dp), |
There was a problem hiding this comment.
nit: can we use 8 to align with the 8-point grid system in UI design
|
|
||
| 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 |
There was a problem hiding this comment.
can this be configured in the request?
There was a problem hiding this comment.
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() |
There was a problem hiding this comment.
should this be wrapped in try catch block as well?
There was a problem hiding this comment.
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() |
There was a problem hiding this comment.
same here: do we need a try catch?
There was a problem hiding this comment.
same thought about not adding a try/catch until we understand when an exception might be thrown
…m/android/ai-samples into caren/writing_assistance_refactor
No description provided.