Skip to content

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

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

Conversation

mlykotom
Copy link
Collaborator

@mlykotom mlykotom commented Jul 7, 2025

  • using LocalAppContainer to retrieve the DI graph instead of getting it with Android Context
  • AppContainer provides the viewModel factories
  • extras is only used for the FruittieViewModel to pass the fruittieId
  • did some small renaming alongside

Although I think we could improve it by providing the initializer directly in the AppContainer and get rid of using the CreationExtras at all, I think it's a good way to show how to use it from Swift code.

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 @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 expose ViewModelProvider.Factory instances for MainViewModel, CartViewModel, and FruittieViewModel. 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 generic creationExtras helper, ViewModels are now created directly using the factories provided by LocalAppContainer.current (on Android) or appContainer.value (on iOS).
  • LocalAppContainer for Android Compose: A new LocalAppContainer CompositionLocal has been introduced in the Android application. This allows Composables to easily access the AppContainer instance without relying on the Android Context, improving testability and separation of concerns.
  • Reduced ViewModel Boilerplate: The individual ViewModel classes (CartViewModel, FruittieViewModel, MainViewModel) no longer define their own Factory companion objects, as their creation is now managed by the AppContainer.
  • Refactored ViewModelFactory Helper: The ViewModelFactory.kt file has been significantly simplified, removing the custom fruittiesViewModelFactory and APP_CONTAINER_KEY as they are no longer needed with the new centralized factory approach. The creationExtras 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 to FruittiesAndroidApp) and the Android theme file (MyApplicationTheme to FruittiesTheme).
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

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

Copy link
Contributor

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

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.

2 participants