Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: android/kotlin-multiplatform-samples
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: main
Choose a base ref
...
head repository: android/kotlin-multiplatform-samples
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: SwiftViewModelStoreOwner
Choose a head ref
  • 2 commits
  • 11 files changed
  • 1 contributor

Commits on Jun 25, 2025

  1. Prototype ViewModelStoreOwner on iOS.

    This commit refactors the ViewModel management in the iOS app to use a more robust and flexible approach with `ViewModelStoreOwner` and dependency injection.
    
    Key changes:
    
    *   **Introduced `IOSViewModelStoreOwner`:**
        *   Manages the lifecycle of ViewModels.
        *   Provides methods to retrieve ViewModels using either an enum (`ViewModelType`), specific function calls, or direct `ViewModelProvider` usage.
        *   Allows passing `CreationExtras` for dependency injection into ViewModels.
    *   **Created `ViewModelStoreOwnerProvider`:**
        *   A SwiftUI `View` that provides an `IOSViewModelStoreOwner` to its content via the environment.
        *   Manages the lifecycle of the `ViewModelStore`, clearing it when the provider disappears.
        *   Allows for nested providers to create scoped `ViewModelStoreOwner` instances.
    *   **Implemented `ObservableValueWrapper`:**
        *   A generic wrapper to make non-`ObservableObject` types (like `IOSViewModelStoreOwner` and `AppContainer`) observable by SwiftUI.
    *   **Updated ViewModels in `CartView` and `ContentView`:**
        *   ViewModels are now retrieved from the `IOSViewModelStoreOwner` injected via `@EnvironmentObject`.
        *   `AppContainer` is also injected via `@EnvironmentObject` to provide dependencies to ViewModels.
        *   `CartView` is now wrapped in its own `ViewModelStoreOwnerProvider` to scope its ViewModels.
    *   **Modified `iOSApp.swift`:**
        *   Initializes and provides the root `AppContainer` and `ViewModelStoreOwnerProvider` to the main `ContentView`.
    
    This approach improves ViewModel scoping, lifecycle management, and simplifies dependency injection for ViewModels in the iOS application.
    cartland committed Jun 25, 2025
    Configuration menu
    Copy the full SHA
    b854449 View commit details
    Browse the repository at this point in the history
  2. Move ViewModelStoreOwner to Swift

    This commit refactors the ViewModel management on iOS to improve clarity and provide a more Swift-idiomatic approach.
    
    Key changes:
    
    *   **Replaced `IOSViewModelStoreOwner` with `SwiftViewModelStoreOwner`:**
        *   The Kotlin-based `IOSViewModelStoreOwner` has been removed.
        *   A new Swift class `SwiftViewModelStoreOwner` is introduced, directly implementing the `IOSViewModelStoreOwner` interface (defined in Kotlin). This class is responsible for creating and managing `ViewModelStore`.
        *   This change moves the ViewModel lifecycle management logic into Swift, making it more natural for iOS development.
    *   **Introduced `IOSBridgeUtilities.kt`:**
        *   This new Kotlin file provides helper functions (`createIOSViewModelStore`, `createViewModelProvider`) and data classes (`IOSViewModelStoreOwner`, `IOSViewModelProviderFactory`, `IOSViewModelStore`, `IOSCreationExtras`) to bridge between Kotlin Multiplatform ViewModels and Swift.
        *   These utilities simplify the interaction between Swift and the underlying Kotlin ViewModel system.
    *   **Updated ViewModel retrieval in Swift views:**
        *   `CartView.swift` and `ContentView.swift` now use the new `SwiftViewModelStoreOwner` to get `CartViewModel` and `MainViewModel` respectively.
        *   The retrieval logic is streamlined, using specific getter methods on `SwiftViewModelStoreOwner` (e.g., `getCartViewModel`, `getMainViewModel`).
        *   The `ViewModelType` enum and the generic `getViewModelWithEnum` method have been removed, as specific getters are now preferred.
    *   **Simplified `ViewModelStoreOwnerProvider.swift`:**
        *   Now directly uses `SwiftViewModelStoreOwner` as a `@StateObject`.
        *   The `ObservableValueWrapper` for `IOSViewModelStoreOwner` is no longer needed in this context.
    *   **`ObservableValueWrapper.swift` generic type constraint:**
        *   Changed from `<Value>` to `<T>` for conventional naming.
    *   **Added `KClass` properties to ViewModels:**
        *   `MainViewModel` and `CartViewModel` now expose their `KClass` (e.g., `MainViewModel.companion.KClass`). This is used by `SwiftViewModelStoreOwner` to correctly retrieve ViewModel instances from the `ViewModelProvider`.
    *   **Updated iOS project files:**
        *   `project.pbxproj` has been updated to include the new `SwiftViewModelStoreOwner.swift` and reflect the removal of `IOSViewModelStoreOwner.kt`.
    
    This refactoring aims to make the ViewModel integration on iOS cleaner, more type-safe, and easier to understand for Swift developers working with a Kotlin Multiplatform shared module.
    cartland committed Jun 25, 2025
    Configuration menu
    Copy the full SHA
    e163d2e View commit details
    Browse the repository at this point in the history
Loading