File tree Expand file tree Collapse file tree 3 files changed +40
-5
lines changed
shared/src/iosMain/kotlin/com/example/kmp/snippets Expand file tree Collapse file tree 3 files changed +40
-5
lines changed Original file line number Diff line number Diff line change 1
1
import SwiftUI
2
- import Shared
2
+ import KmpKit
3
3
4
4
struct ContentView : View {
5
5
@State private var showContent = false
@@ -16,7 +16,7 @@ struct ContentView: View {
16
16
Image ( systemName: " swift " )
17
17
. font ( . system( size: 200 ) )
18
18
. foregroundColor ( . accentColor)
19
- Text ( " SwiftUI: \( Greeting ( ) . greet ( ) ) " )
19
+ Text ( " SwiftUI " )
20
20
}
21
21
. transition ( . move( edge: . top) . combined ( with: . opacity) )
22
22
}
Original file line number Diff line number Diff line change
1
+ import Foundation
2
+ import KmpKit
3
+
4
+ // [START android_kmp_viewmodel_ios_viewmodel_storeowner]
5
+ class IosViewModelStoreOwner : ObservableObject , ViewModelStoreOwner {
6
+
7
+ let viewModelStore = ViewModelStore ( )
8
+
9
+ /// This function allows retrieving the androidx ViewModel from the store.
10
+ /// It uses the utilify function to pass the generic type T to shared code
11
+ func viewModel< T: ViewModel > (
12
+ key: String ? = nil ,
13
+ factory: ViewModelProviderFactory ,
14
+ extras: CreationExtras ? = nil
15
+ ) -> T {
16
+ do {
17
+ return try viewModelStore. resolveViewModel (
18
+ modelClass: T . self,
19
+ factory: factory,
20
+ key: key,
21
+ extras: extras
22
+ ) as! T
23
+ } catch {
24
+ fatalError ( " Failed to create ViewModel of type \( T . self) " )
25
+ }
26
+ }
27
+
28
+ /// This is called when this class is used as a `@StateObject`
29
+ deinit {
30
+ viewModelStore. clear ( )
31
+ }
32
+ }
33
+ // [END android_kmp_viewmodel_ios_viewmodel_storeowner]
Original file line number Diff line number Diff line change 1
- package com.example.kmp.snipets
1
+ package com.example.kmp.snippets
2
2
3
3
import androidx.lifecycle.ViewModel
4
4
import androidx.lifecycle.ViewModelProvider
@@ -9,9 +9,10 @@ import kotlinx.cinterop.ObjCClass
9
9
import kotlinx.cinterop.getOriginalKotlinClass
10
10
import kotlin.reflect.KClass
11
11
12
+ // [START android_kmp_viewmodel_resolve_viewmodel]
12
13
/* *
13
14
* This function allows retrieving any ViewModel from Swift Code with generics. We only get
14
- * [kotlinx.cinterop. ObjCClass] type for the [modelClass], because the interop between Kotlin and Swift code
15
+ * [ObjCClass] type for the [modelClass], because the interop between Kotlin and Swift code
15
16
* doesn't preserve the generic class, but we can retrieve the original KClass in Kotlin.
16
17
*/
17
18
@BetaInteropApi
@@ -28,4 +29,5 @@ fun ViewModelStore.resolveViewModel(
28
29
29
30
val provider = ViewModelProvider .Companion .create(this , factory, extras ? : CreationExtras .Empty )
30
31
return key?.let { provider[key, vmClass] } ? : provider[vmClass]
31
- }
32
+ }
33
+ // [END android_kmp_viewmodel_resolve_viewmodel]
You can’t perform that action at this time.
0 commit comments