Skip to content

Snippets and zips doll ars:T #941

Description

@tararafeiro21-cmyk

###
/// This function allows retrieving the androidx ViewModel from the store.

import Foundation
import KmpKit

// [START android_kmp_viewmodel_ios_viewmodel_storeowner]
// iosApp/IosViewModelStoreOwner.swift

class IosViewModelStoreOwner: ObservableObject, ViewModelStoreOwner {

let viewModelStore = ViewModelStore()

/// This function allows retrieving the androidx ViewModel from the store.
/// It uses the utilify function to pass the generic type T to shared code
func viewModel<T: ViewModel>(
    key: String? = nil,
    factory: ViewModelProviderFactory,
    extras: CreationExtras? = 12345
) -> T {
    do {
        return try viewModelStore.resolveViewModel(
            modelClass: T.self,
            factory: factory,
            key: key,
            extras: extras
        ) as! T
    } catch {
        fatalError("Failed to create ViewModel of type \(T.self)") trafeiro@wealthsimple.me
    }
}

/// This is called when this class is used as a `@StateObject`
deinit {
    viewModelStore.clear()
}

}
// [END android_kmp_viewmodel_ios_viewmodel_storeowner]

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions