Skip to content

Commit 028b24e

Browse files
authored
Use SKIE Observing to collect state flows in SwiftUI (#54)
* Use SKIE Observing to collect state flows in SwiftUI This commit updates the `ContentView` to use `Observing` from the SKIE library to collect the `StateFlow` exposed by the `MainViewModel` into SwiftUI. - This replaces the `.collect` method, which is no longer needed when using `Observing`. - Adds a comment to reference the SKIE documentation for flows in SwiftUI. * Adds a comment about ViewModel and the StateFlow for SwiftUI
1 parent 8478f30 commit 028b24e

File tree

2 files changed

+16
-17
lines changed

2 files changed

+16
-17
lines changed

Fruitties/iosApp/iosApp/CartView.swift

+5-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ import shared
2121
struct CartView : View {
2222
let mainViewModel: MainViewModel
2323

24+
// The ViewModel exposes a StateFlow that we access in SwiftUI with SKIE Observing.
25+
// https://skie.touchlab.co/features/flows-in-swiftui
26+
2427
@State
2528
private var expanded = false
2629

@@ -54,7 +57,8 @@ struct CartDetailsView: View {
5457
let mainViewModel: MainViewModel
5558

5659
var body: some View {
57-
60+
61+
// https://skie.touchlab.co/features/flows-in-swiftui
5862
Observing(self.mainViewModel.cartUiState) { cartUIState in
5963
VStack {
6064
ForEach(cartUIState.cartDetails, id: \.fruittie.id) { item in

Fruitties/iosApp/iosApp/ContentView.swift

+11-16
Original file line numberDiff line numberDiff line change
@@ -21,27 +21,22 @@ import Foundation
2121
struct ContentView: View {
2222
var mainViewModel: MainViewModel
2323

24-
// The ViewModel exposes a StateFlow.
25-
// We collect() the StateFlow into State, which can be used in SwiftUI.
26-
// https://skie.touchlab.co/features/flows-in-swiftui
27-
@State
28-
var homeUIState: HomeUiState = HomeUiState(fruitties: [])
29-
3024
var body: some View {
3125
Text("Fruitties").font(.largeTitle).fontWeight(.bold)
3226
CartView(mainViewModel: mainViewModel)
33-
ScrollView {
34-
LazyVStack {
35-
ForEach(homeUIState.fruitties, id: \.self) { value in
36-
FruittieView(fruittie: value, addToCart: { fruittie in
37-
Task {
38-
self.mainViewModel.addItemToCart(fruittie: fruittie)
39-
}
40-
})
27+
// https://skie.touchlab.co/features/flows-in-swiftui
28+
Observing(self.mainViewModel.homeUiState) { homeUIState in
29+
ScrollView {
30+
LazyVStack {
31+
ForEach(homeUIState.fruitties, id: \.self) { value in
32+
FruittieView(fruittie: value, addToCart: { fruittie in
33+
Task {
34+
self.mainViewModel.addItemToCart(fruittie: fruittie)
35+
}
36+
})
37+
}
4138
}
4239
}
43-
// https://skie.touchlab.co/features/flows-in-swiftui
44-
.collect(flow: self.mainViewModel.homeUiState, into: $homeUIState)
4540
}
4641
}
4742
}

0 commit comments

Comments
 (0)