-
Notifications
You must be signed in to change notification settings - Fork 119
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
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
AppContainerto directly exposeViewModelProvider.Factoryinstances 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
AppContainerand passing it to a genericcreationExtrashelper, ViewModels are now created directly using the factories provided byLocalAppContainer.current(on Android) orappContainer.value(on iOS). - LocalAppContainer for Android Compose: A new
LocalAppContainerCompositionLocal has been introduced in the Android application. This allows Composables to easily access theAppContainerinstance 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 ownFactorycompanion objects, as their creation is now managed by theAppContainer. - Refactored ViewModelFactory Helper: The
ViewModelFactory.ktfile has been significantly simplified, removing the customfruittiesViewModelFactoryandAPP_CONTAINER_KEYas they are no longer needed with the new centralized factory approach. ThecreationExtrasfunction 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
Applicationclass (ApptoFruittiesAndroidApp) and the Android theme file (MyApplicationThemetoFruittiesTheme).
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
cartland
left a comment
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).
Fruitties/androidApp/src/main/java/com/example/fruitties/android/ui/FruittieScreen.kt
Outdated
Show resolved
Hide resolved
Fruitties/shared/src/commonMain/kotlin/com/example/fruitties/di/AppContainer.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.
Much cleaner now, well done! I definitely prefer this. 💯
fruittieIdAlthough I think we could improve it by providing the initializer directly in the
AppContainerand get rid of using theCreationExtrasat all, I think it's a good way to show how to use it from Swift code.