@@ -21,56 +21,46 @@ import shared
21
21
struct CartView : View {
22
22
let mainViewModel : MainViewModel
23
23
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
-
30
24
@State
31
25
private var expanded = false
32
26
33
27
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
+ }
51
48
}
52
49
}
53
- // https://skie.touchlab.co/features/flows-in-swiftui
54
- . collect ( flow: self . mainViewModel. cartUiState, into: $cartUIState)
55
50
}
56
51
}
57
52
58
53
struct CartDetailsView : View {
59
54
let mainViewModel : MainViewModel
60
55
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
-
67
56
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
+ }
71
63
}
72
64
}
73
- // https://skie.touchlab.co/features/flows-in-swiftui
74
- . collect ( flow: mainViewModel. cartUiState, into: $cartUIState)
75
65
}
76
66
}
0 commit comments