File tree Expand file tree Collapse file tree 2 files changed +23
-25
lines changed
shared/src/commonMain/kotlin/com/example/kmp/snippets Expand file tree Collapse file tree 2 files changed +23
-25
lines changed Original file line number Diff line number Diff line change 1
- import SwiftUI
1
+ import Foundation
2
2
import KmpKit
3
+ import SwiftUI
3
4
5
+ // [START android_kmp_viewmodel_ios_contentview]
4
6
struct ContentView : View {
5
- @State private var showContent = false
6
- var body : some View {
7
- VStack {
8
- Button ( " Click me! " ) {
9
- withAnimation {
10
- showContent = !showContent
11
- }
12
- }
13
7
14
- if showContent {
15
- VStack ( spacing: 16 ) {
16
- Image ( systemName: " swift " )
17
- . font ( . system( size: 200 ) )
18
- . foregroundColor ( . accentColor)
19
- Text ( " SwiftUI " )
20
- }
21
- . transition ( . move( edge: . top) . combined ( with: . opacity) )
22
- }
8
+ /// Use the store owner as a StateObject to allow retrieving ViewModels and scoping it to this screen.
9
+ @StateObject private var viewModelStoreOwner = IosViewModelStoreOwner ( )
10
+
11
+ var body : some View {
12
+ /// Retrieves the `MainViewModel` instance using the `viewModelStoreOwner`.
13
+ /// The `MainViewModel.Factory` and `creationExtras` are provided to enable dependency injection
14
+ /// and proper initialization of the ViewModel with its required `AppContainer`.
15
+ let mainViewModel : MainViewModel = viewModelStoreOwner. viewModel (
16
+ factory: MainViewModelKt . mainViewModelFactory
17
+ )
18
+ // [START_EXCLUDE]
19
+ VStack ( spacing: 16 ) {
20
+ Image ( systemName: " swift " )
21
+ . font ( . system( size: 200 ) )
22
+ . foregroundColor ( . accentColor)
23
+ Text ( " SwiftUI " )
23
24
}
24
25
. frame ( maxWidth: . infinity, maxHeight: . infinity, alignment: . top)
25
26
. padding ( )
27
+ // [END_EXCLUDE]
28
+ // .. the rest of the SwiftUI code
26
29
}
27
30
}
28
-
29
- struct ContentView_Previews : PreviewProvider {
30
- static var previews : some View {
31
- ContentView ( )
32
- }
33
- }
31
+ // [END android_kmp_viewmodel_ios_contentview]
Original file line number Diff line number Diff line change @@ -16,7 +16,7 @@ val mainViewModelFactory = viewModelFactory {
16
16
}
17
17
}
18
18
19
- fun getDataRepository (): DataRepository = TODO ()
19
+ fun getDataRepository (): DataRepository = DataRepository ()
20
20
// [END android_kmp_viewmodel_class]
21
21
22
22
You can’t perform that action at this time.
0 commit comments