Skip to content

Commit 503db88

Browse files
committed
Add swift content
1 parent 0e15647 commit 503db88

File tree

2 files changed

+23
-25
lines changed

2 files changed

+23
-25
lines changed
Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,31 @@
1-
import SwiftUI
1+
import Foundation
22
import KmpKit
3+
import SwiftUI
34

5+
// [START android_kmp_viewmodel_ios_contentview]
46
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-
}
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]

kmp/shared/src/commonMain/kotlin/com/example/kmp/snippets/MainViewModel.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff 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

0 commit comments

Comments
 (0)