@@ -5,180 +5,7 @@ import SwiftUI
55struct CaseStudiesApp : App {
66 var body : some Scene {
77 WindowGroup {
8- //RootView()
9- ContentView ( )
8+ RootView ( )
109 }
1110 }
1211}
13-
14-
15- import SwiftUI
16- import ComposableArchitecture
17-
18-
19- //@inlinable
20- //public func _$isIdentityEqual<ID, T: ObservableState>(
21- // _ lhs: IdentifiedArray<ID, T>,
22- // _ rhs: IdentifiedArray<ID, T>
23- //) -> Bool {
24- // false
25- //}
26-
27- @Reducer
28- struct MainFeature {
29- @ObservableState
30- struct State {
31- var childArray : IdentifiedArrayOf < ChildFeature . State >
32- var childSolo : ChildFeature . State ?
33-
34- init ( ) {
35- childSolo = ChildFeature . State ( id: " 1 " , count: 0 )
36- // It's important to start with non empty array.
37- childArray = [ ChildFeature . State ( id: " 1 " , count: 0 ) ]
38- }
39- }
40-
41- enum Action {
42- case setAnotherIDs
43- case getBackToInitialIDsWithAnotherCount
44- case resetToInitialIDs
45- case childArray( IdentifiedActionOf < ChildFeature > )
46- case childSolo( ChildFeature . Action )
47- }
48-
49- var body : some ReducerOf < Self > {
50- Reduce < State , Action > { state, action in
51- switch action {
52- case . setAnotherIDs:
53- // Update ID
54- state. childSolo = ChildFeature . State ( id: " 2 " , count: 0 )
55- state. childArray = [ ChildFeature . State ( id: " 2 " , count: 0 ) ]
56- return . none
57-
58- case . getBackToInitialIDsWithAnotherCount:
59- // Go back to the initial ID with a different count
60- state. childSolo = ChildFeature . State ( id: " 1 " , count: 10 )
61- state. childArray = [ ChildFeature . State ( id: " 1 " , count: 10 ) ]
62- return . none
63-
64- case . resetToInitialIDs:
65- // Set the same ID as in the previous action, but with a count of 0
66- // After this step, the child stores in childArray stopped working.
67- state. childSolo = ChildFeature . State ( id: " 1 " , count: 0 )
68- state. childArray = [ ChildFeature . State ( id: " 1 " , count: 0 ) ]
69- return . none
70-
71- case . childArray:
72- return . none
73-
74- case . childSolo:
75- return . none
76- }
77- }
78- . ifLet ( \. childSolo, action: \. childSolo) {
79- ChildFeature ( )
80- }
81- . forEach ( \. childArray, action: \. childArray) {
82- ChildFeature ( )
83- }
84- }
85- }
86-
87- @Reducer
88- struct ChildFeature {
89- @ObservableState
90- struct State : Identifiable {
91- let id : String
92- var count : Int
93- }
94-
95- enum Action {
96- case plus
97- case minus
98- }
99-
100- var body : some ReducerOf < Self > {
101- Reduce { state, action in
102- switch action {
103- case . plus:
104- state. count += 1
105- return . none
106-
107- case . minus:
108- state. count -= 1
109- return . none
110- }
111- }
112- }
113- }
114-
115- struct ChildView : View {
116- let store : StoreOf < ChildFeature >
117-
118- var body : some View {
119- VStack {
120- Text ( " id: " + store. id)
121-
122- HStack {
123- Button ( " Minus " ) {
124- store. send ( . minus)
125- }
126-
127- Text ( store. count. description)
128-
129- Button ( " Plus " ) {
130- store. send ( . plus)
131- }
132- }
133- . frame ( height: 50 )
134- . buttonStyle ( . borderedProminent)
135- }
136- }
137- }
138-
139- struct MainView : View {
140- let store : StoreOf < MainFeature >
141-
142- var body : some View {
143- ScrollView {
144- VStack {
145- Button ( " setAnotherIDs " ) {
146- store. send ( . setAnotherIDs)
147- }
148-
149- Button ( " getBackToInitialIDsWithAnotherCount " ) {
150- store. send ( . getBackToInitialIDsWithAnotherCount)
151- }
152-
153- Button ( " resetToInitialIDs (not working) " ) {
154- store. send ( . resetToInitialIDs)
155- }
156-
157- Color . red. frame ( height: 10 )
158-
159- // Text("Child solo:")
160- // if let childStore = store.scope(state: \.childSolo, action: \.childSolo) {
161- // ChildView(store: childStore)
162- // }
163-
164- Color . red. frame ( height: 10 )
165-
166- Text ( " Child array: " )
167- ForEach (
168- store. scope ( state: \. childArray, action: \. childArray)
169- ) { childStore in
170- ChildView ( store: childStore)
171- }
172- }
173- }
174- }
175- }
176-
177- struct ContentView : View {
178- @State var mainStore : StoreOf < MainFeature > = Store ( initialState: MainFeature . State ( ) ) {
179- MainFeature ( ) . _printChanges ( )
180- }
181- var body : some View {
182- MainView ( store: mainStore)
183- }
184- }
0 commit comments