Skip to content

ViewModel Scoping with store in Swift #75

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 16 commits into
base: main
Choose a base branch
from

Conversation

mlykotom
Copy link
Collaborator

Move IOSViewModelStoreOwner to Swift

Move IOSViewModelStoreOwner to Swift

# Conflicts:
#	Fruitties/iosApp/iosApp/CartView.swift
#	Fruitties/iosApp/iosApp/ContentView.swift
#	Fruitties/iosApp/iosApp/ViewModelStoreOwnerProvider.swift
#	Fruitties/iosApp/iosApp/iOSApp.swift
#	Fruitties/shared/src/iosMain/kotlin/com/example/fruitties/di/viewmodel/IOSViewModelStoreOwner.kt
Copy link

@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 significantly refactors the ViewModel management strategy within the Kotlin Multiplatform project, particularly for the iOS application. The core IOSViewModelStoreOwner functionality is now natively implemented in Swift, enhancing integration with SwiftUI's lifecycle and environment. Concurrently, the shared Kotlin module introduces a centralized and generic ViewModel factory helper, simplifying ViewModel instantiation across both Android and iOS platforms and improving overall code consistency and maintainability.

Highlights

  • iOS ViewModel Management Refactor: The IOSViewModelStoreOwner logic has been moved from the shared Kotlin module to native Swift (ViewModelStoreOwnerProvider.swift), providing a more idiomatic and robust way to manage ViewModel lifecycles within SwiftUI, including automatic clearing of the ViewModel store when a view disappears.
  • Generic ViewModel Retrieval: The new Swift IOSViewModelStoreOwner class introduces a generic viewModel method, allowing for the retrieval of any ViewModel type from the store, replacing the previous specific get*ViewModel methods.
  • Centralized ViewModel Factory Helper (KMP Shared): A new ViewModelFactory.kt file has been added to the shared Kotlin module. It introduces a reusable vmFactory helper and a creationExtras function, which streamlines the creation of ViewModel factories and simplifies dependency injection (specifically for AppContainer) across both Android and iOS platforms.
  • SwiftUI Integration Enhancements: An ObservableValueWrapper has been introduced to make the AppContainer observable by SwiftUI, and the ViewModelStoreOwnerProvider SwiftUI View is now used to inject the IOSViewModelStoreOwner into the SwiftUI environment, enabling proper ViewModel scoping and access throughout the view hierarchy.
  • Android ViewModel Instantiation Simplification: The Android UI code in CartScreen.kt and ListScreen.kt has been updated to utilize the new shared creationExtras helper, removing redundant remember blocks and making ViewModel instantiation cleaner and more consistent with the new shared logic.
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

@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 refactors the ViewModel management for the iOS app by moving the IOSViewModelStoreOwner from Kotlin to Swift. This is a great architectural improvement that makes the iOS side more idiomatic and leverages SwiftUI's lifecycle management features effectively. The introduction of ViewModelStoreOwnerProvider and the generic vmFactory in Kotlin significantly cleans up the code and reduces boilerplate.

I've left a few comments on potential improvements:

  • Using safer casting in Swift.
  • Removing redundant calculations in a SwiftUI view.
  • Removing debug println statements from ViewModels.

One high-severity issue to address is the presence of two identical files: ViewModelStoreOwnerProvider.swift and ViewModelStoreProvider.swift. The Xcode project file (.pbxproj) seems to reference ViewModelStoreProvider.swift, but the Swift code uses the type ViewModelStoreOwnerProvider. This inconsistency should be resolved by deleting one file and renaming the other to match the type name, and updating the project file accordingly. This will prevent confusion and future maintenance issues.

Overall, this is a solid PR that improves the iOS architecture.

cartland and others added 2 commits June 25, 2025 12:53
This commit introduces Kermit for logging:

- Added the Kermit dependency (`co.touchlab:kermit`) to `libs.versions.toml` and `shared/build.gradle.kts`.
- Replaced `println` statements with `Logger.i` in `CartViewModel` and `MainViewModel` for improved logging.
- Added logging for ViewModel creation and clearing in both `CartViewModel` and `MainViewModel`.
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.

This is looking good. We need to fix the duplicate file, but most of the other changes are small.

Base automatically changed from nav3 to main June 25, 2025 21:27
@mlykotom mlykotom requested a review from cartland June 25, 2025 21:48
@marcellogalhardo marcellogalhardo self-requested a review July 1, 2025 08:19
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.

Looking good! Please run ./gradlew spotlessApply for formatting.

Copy link
Contributor

Choose a reason for hiding this comment

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

I suggest renaming this file, maybe ViewModelStoreUtil.kt

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