Skip to content

Commit b1e0e10

Browse files
authored
Switch to Observing API in swift (android#37)
1 parent ac12d4e commit b1e0e10

File tree

1 file changed

+26
-36
lines changed

1 file changed

+26
-36
lines changed

Fruitties/iosApp/iosApp/CartView.swift

Lines changed: 26 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -21,56 +21,46 @@ import shared
2121
struct CartView : View {
2222
let 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 cartUIState: CartUiState = CartUiState(cartDetails: [])
29-
3024
@State
3125
private var expanded = false
3226

3327
var body: some View {
34-
VStack {
35-
HStack {
36-
let total = cartUIState.cartDetails.reduce(0) { $0 + ($1.count) }
37-
Text("Cart has \(total) items").padding()
38-
Spacer()
39-
Button {
40-
expanded.toggle()
41-
} label: {
42-
if (expanded) {
43-
Text("collapse")
44-
} else {
45-
Text("expand")
46-
}
47-
}.padding()
48-
}
49-
if (expanded) {
50-
CartDetailsView(mainViewModel: mainViewModel)
28+
// https://skie.touchlab.co/features/flows-in-swiftui
29+
Observing(self.mainViewModel.cartUiState) { cartUIState in
30+
VStack {
31+
HStack {
32+
let total = cartUIState.cartDetails.reduce(0) { $0 + $1.count }
33+
Text("Cart has \(total) items").padding()
34+
Spacer()
35+
Button {
36+
expanded.toggle()
37+
} label: {
38+
if (expanded) {
39+
Text("collapse")
40+
} else {
41+
Text("expand")
42+
}
43+
}.padding()
44+
}
45+
if (expanded) {
46+
CartDetailsView(mainViewModel: mainViewModel)
47+
}
5148
}
5249
}
53-
// https://skie.touchlab.co/features/flows-in-swiftui
54-
.collect(flow: self.mainViewModel.cartUiState, into: $cartUIState)
5550
}
5651
}
5752

5853
struct CartDetailsView: View {
5954
let mainViewModel: MainViewModel
6055

61-
// The ViewModel exposes a StateFlow.
62-
// We collect() the StateFlow into State, which can be used in SwiftUI.
63-
// https://skie.touchlab.co/features/flows-in-swiftui
64-
@State
65-
var cartUIState: CartUiState = CartUiState(cartDetails: [])
66-
6756
var body: some View {
68-
VStack {
69-
ForEach(cartUIState.cartDetails, id: \.fruittie.id) { item in
70-
Text("\(item.fruittie.name): \(item.count)")
57+
58+
Observing(self.mainViewModel.cartUiState) { cartUIState in
59+
VStack {
60+
ForEach(cartUIState.cartDetails, id: \.fruittie.id) { item in
61+
Text("\(item.fruittie.name): \(item.count)")
62+
}
7163
}
7264
}
73-
// https://skie.touchlab.co/features/flows-in-swiftui
74-
.collect(flow: mainViewModel.cartUiState, into: $cartUIState)
7565
}
7666
}

0 commit comments

Comments
 (0)