Skip to content

Commit 6ad9957

Browse files
committed
Merge branch 'main' into mlykotom/fruittie-screen-and-ios-designs
# Conflicts: # Fruitties/shared/src/iosMain/kotlin/com/example/fruitties/di/viewmodel/ViewModelStoreUtil.kt
2 parents d107f53 + c33d0ac commit 6ad9957

File tree

3 files changed

+20
-6
lines changed

3 files changed

+20
-6
lines changed

Fruitties/iosApp/iosApp/IOSViewModelStoreOwner.swift

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,16 @@ import shared
33

44
/// A ViewModelStoreOwner specifically for iOS.
55
/// This is used with from iOS with Kotlin Multiplatform (KMP).
6-
class IOSViewModelStoreOwner: ObservableObject {
6+
class IOSViewModelStoreOwner: ObservableObject, SwiftViewModelStoreOwner {
77

8-
private let viewModelStore = Lifecycle_viewmodelViewModelStore()
8+
var viewModelStore: Lifecycle_viewmodelViewModelStore =
9+
Lifecycle_viewmodelViewModelStore()
910

1011
/// This function allows retrieving the androidx ViewModel from the store.
11-
func viewModel<T: AnyObject>(
12+
func viewModel<T: Lifecycle_viewmodelViewModel>(
1213
key: String? = nil,
1314
factory: Lifecycle_viewmodelViewModelProviderFactory,
14-
extras: Lifecycle_viewmodelCreationExtras
15+
extras: Lifecycle_viewmodelCreationExtras? = nil
1516
) -> T {
1617
do {
1718
return try viewModelStore.getViewModel(

Fruitties/shared/src/commonMain/kotlin/com/example/fruitties/viewmodel/CartViewModel.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class CartViewModel(
3232
private val repository: DataRepository,
3333
) : ViewModel() {
3434
init {
35+
// Leaving the comments here to show when the ViewModel is created.
3536
Logger.v { "CartViewModel created" }
3637
}
3738

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,36 @@ package com.example.fruitties.di.viewmodel
33
import androidx.lifecycle.ViewModel
44
import androidx.lifecycle.ViewModelProvider
55
import androidx.lifecycle.ViewModelStore
6+
import androidx.lifecycle.ViewModelStoreOwner
67
import androidx.lifecycle.viewmodel.CreationExtras
78
import kotlinx.cinterop.BetaInteropApi
89
import kotlinx.cinterop.ObjCClass
910
import kotlinx.cinterop.getOriginalKotlinClass
1011
import kotlin.reflect.KClass
1112

13+
/**
14+
* This function allows retrieving any ViewModel from Swift Code with generics.
15+
* We only get [ObjCClass] type for the [modelClass], because the interop between Kotlin and Swift
16+
* code doesn't preserve the generic class, but we can retrieve the original KClass in Kotlin.
17+
*/
1218
@Suppress("unused") // Android Studio is not aware of iOS usage.
1319
@OptIn(BetaInteropApi::class)
1420
@Throws(IllegalStateException::class)
1521
fun ViewModelStore.getViewModel(
1622
modelClass: ObjCClass,
1723
factory: ViewModelProvider.Factory,
1824
key: String?,
19-
extras: CreationExtras,
25+
extras: CreationExtras? = null,
2026
): ViewModel {
2127
@Suppress("UNCHECKED_CAST")
2228
val vmClass = getOriginalKotlinClass(modelClass) as? KClass<ViewModel>
2329
?: error("modelClass isn't a ViewModel type")
24-
val provider = ViewModelProvider.create(this, factory, extras)
30+
val provider = ViewModelProvider.create(this, factory, extras ?: CreationExtras.Empty)
2531
return key?.let { provider[key, vmClass] } ?: provider[vmClass]
2632
}
33+
34+
/**
35+
* The ViewModelStoreOwner isn't used anywhere in the project, therefore it's not visible for Swift by default.
36+
*/
37+
@Suppress("unused")
38+
interface SwiftViewModelStoreOwner : ViewModelStoreOwner

0 commit comments

Comments
 (0)