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
22import KmpKit
3+ import SwiftUI
34
5+ // [START android_kmp_viewmodel_ios_contentview]
46struct 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- }
137
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 " )
2324 }
2425 . frame ( maxWidth: . infinity, maxHeight: . infinity, alignment: . top)
2526 . padding ( )
27+ // [END_EXCLUDE]
28+ // .. the rest of the SwiftUI code
2629 }
2730}
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 {
1616 }
1717}
1818
19- fun getDataRepository (): DataRepository = TODO ()
19+ fun getDataRepository (): DataRepository = DataRepository ()
2020// [END android_kmp_viewmodel_class]
2121
2222
You can’t perform that action at this time.
0 commit comments