-
Notifications
You must be signed in to change notification settings - Fork 103
Improve retrieving ViewModels #80
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 @mlykotom, 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 introduces a significant architectural improvement by refactoring how ViewModels are retrieved and instantiated across both Android and iOS platforms in this Kotlin Multiplatform project. The core change involves centralizing ViewModel factory definitions within the AppContainer
and providing a more idiomatic way to access these factories, particularly through the introduction of LocalAppContainer
for Android Compose. This approach streamlines dependency injection, moves away from direct Android Context access for DI, and reduces boilerplate in individual ViewModel classes, leading to a cleaner and more maintainable codebase.
Highlights
- Centralized ViewModel Factories: I've refactored the
AppContainer
to directly exposeViewModelProvider.Factory
instances forMainViewModel
,CartViewModel
, andFruittieViewModel
. This centralizes the ViewModel creation logic within the dependency injection graph. - Simplified ViewModel Instantiation: ViewModel instantiation in Android Compose and iOS SwiftUI has been simplified. Instead of manually retrieving the
AppContainer
and passing it to a genericcreationExtras
helper, ViewModels are now created directly using the factories provided byLocalAppContainer.current
(on Android) orappContainer.value
(on iOS). - LocalAppContainer for Android Compose: A new
LocalAppContainer
CompositionLocal has been introduced in the Android application. This allows Composables to easily access theAppContainer
instance without relying on the AndroidContext
, improving testability and separation of concerns. - Reduced ViewModel Boilerplate: The individual ViewModel classes (
CartViewModel
,FruittieViewModel
,MainViewModel
) no longer define their ownFactory
companion objects, as their creation is now managed by theAppContainer
. - Refactored ViewModelFactory Helper: The
ViewModelFactory.kt
file has been significantly simplified, removing the customfruittiesViewModelFactory
andAPP_CONTAINER_KEY
as they are no longer needed with the new centralized factory approach. ThecreationExtras
function is now a more generic helper for passing additional data. - Renaming and Package Restructuring: Several classes and packages have been renamed or moved for better organization, including the main Android
Application
class (App
toFruittiesAndroidApp
) and the Android theme file (MyApplicationTheme
toFruittiesTheme
).
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 is currently in preview and 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 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 centralizes ViewModel creation and improves dependency injection using LocalAppContainer
on Android, and moves ViewModel factories into the AppContainer
, simplifying the UI layer on both Android and iOS. Suggestions have been made to improve robustness and correctness.
Fruitties/androidApp/src/main/java/com/example/fruitties/android/ui/FruittieScreen.kt
Outdated
Show resolved
Hide resolved
Fruitties/androidApp/src/main/java/com/example/fruitties/android/MainActivity.kt
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.
Code Assist found a place with a high risk of introducing a bug, so it makes sense to add a key parameter. I also think we should add a comment to the initializer block that uses get(KEY).
fruittieId
Although I think we could improve it by providing the initializer directly in the
AppContainer
and get rid of using theCreationExtras
at all, I think it's a good way to show how to use it from Swift code.