-
Notifications
You must be signed in to change notification settings - Fork 103
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
base: main
Are you sure you want to change the base?
Conversation
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
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 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 genericviewModel
method, allowing for the retrieval of any ViewModel type from the store, replacing the previous specificget*ViewModel
methods. - Centralized ViewModel Factory Helper (KMP Shared): A new
ViewModelFactory.kt
file has been added to the shared Kotlin module. It introduces a reusablevmFactory
helper and acreationExtras
function, which streamlines the creation of ViewModel factories and simplifies dependency injection (specifically forAppContainer
) across both Android and iOS platforms. - SwiftUI Integration Enhancements: An
ObservableValueWrapper
has been introduced to make theAppContainer
observable by SwiftUI, and theViewModelStoreOwnerProvider
SwiftUI View is now used to inject theIOSViewModelStoreOwner
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
andListScreen.kt
has been updated to utilize the new sharedcreationExtras
helper, removing redundantremember
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
-
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 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.
Fruitties/shared/src/commonMain/kotlin/com/example/fruitties/viewmodel/CartViewModel.kt
Outdated
Show resolved
Hide resolved
Fruitties/shared/src/commonMain/kotlin/com/example/fruitties/viewmodel/CartViewModel.kt
Outdated
Show resolved
Hide resolved
Fruitties/shared/src/commonMain/kotlin/com/example/fruitties/viewmodel/MainViewModel.kt
Outdated
Show resolved
Hide resolved
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`.
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.
This is looking good. We need to fix the duplicate file, but most of the other changes are small.
Fruitties/shared/src/commonMain/kotlin/com/example/fruitties/viewmodel/CartViewModel.kt
Outdated
Show resolved
Hide resolved
Fruitties/shared/src/commonMain/kotlin/com/example/fruitties/viewmodel/ViewModelFactory.kt
Outdated
Show resolved
Hide resolved
Integrate Kermit for logging
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.
Looking good! Please run ./gradlew spotlessApply
for formatting.
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.
I suggest renaming this file, maybe ViewModelStoreUtil.kt
Move IOSViewModelStoreOwner to Swift