@@ -28,16 +28,16 @@ extension Reducer {
28
28
}
29
29
30
30
struct NestedState : Equatable , Identifiable {
31
- var children : IdentifiedArrayOf < NestedState > = [ ]
32
31
let id : UUID
33
- var description : String = " "
32
+ var name : String = " "
33
+ var rows : IdentifiedArrayOf < NestedState > = [ ]
34
34
}
35
35
36
- indirect enum NestedAction : Equatable {
37
- case append
38
- case node ( id : NestedState . ID , action : NestedAction )
39
- case remove ( IndexSet )
40
- case rename ( String )
36
+ enum NestedAction : Equatable {
37
+ case addRowButtonTapped
38
+ case nameTextFieldChanged ( String )
39
+ case onDelete ( IndexSet )
40
+ indirect case row ( id : NestedState . ID , action : NestedAction )
41
41
}
42
42
43
43
struct NestedEnvironment {
@@ -48,49 +48,49 @@ let nestedReducer = Reducer<
48
48
NestedState , NestedAction , NestedEnvironment
49
49
> . recurse { `self`, state, action, environment in
50
50
switch action {
51
- case . append :
52
- state. children . append ( NestedState ( id: environment. uuid ( ) ) )
51
+ case . addRowButtonTapped :
52
+ state. rows . append ( NestedState ( id: environment. uuid ( ) ) )
53
53
return . none
54
54
55
- case . node:
55
+ case let . nameTextFieldChanged( name) :
56
+ state. name = name
57
+ return . none
58
+
59
+ case let . onDelete( indexSet) :
60
+ state. rows. remove ( atOffsets: indexSet)
61
+ return . none
62
+
63
+ case . row:
56
64
return self . forEach (
57
- state: \. children ,
58
- action: / NestedAction. node ( id: action: ) ,
65
+ state: \. rows ,
66
+ action: / NestedAction. row ( id: action: ) ,
59
67
environment: { $0 }
60
68
)
61
69
. run ( & state, action, environment)
62
-
63
- case let . remove( indexSet) :
64
- state. children. remove ( atOffsets: indexSet)
65
- return . none
66
-
67
- case let . rename( name) :
68
- state. description = name
69
- return . none
70
70
}
71
71
}
72
72
73
73
struct NestedView : View {
74
74
let store : Store < NestedState , NestedAction >
75
75
76
76
var body : some View {
77
- WithViewStore ( self . store, observe: \. description ) { viewStore in
77
+ WithViewStore ( self . store, observe: \. name ) { viewStore in
78
78
Form {
79
79
Section {
80
80
AboutView ( readMe: readMe)
81
81
}
82
82
83
83
ForEachStore (
84
- self . store. scope ( state: \. children , action: NestedAction . node ( id: action: ) )
84
+ self . store. scope ( state: \. rows , action: NestedAction . row ( id: action: ) )
85
85
) { childStore in
86
- WithViewStore ( childStore, observe: \. description ) { childViewStore in
86
+ WithViewStore ( childStore, observe: \. name ) { childViewStore in
87
87
NavigationLink (
88
88
destination: NestedView ( store: childStore)
89
89
) {
90
90
HStack {
91
91
TextField (
92
92
" Untitled " ,
93
- text: childViewStore. binding ( send: NestedAction . rename )
93
+ text: childViewStore. binding ( send: NestedAction . nameTextFieldChanged )
94
94
)
95
95
Text ( " Next " )
96
96
. font ( . callout)
@@ -99,12 +99,12 @@ struct NestedView: View {
99
99
}
100
100
}
101
101
}
102
- . onDelete { viewStore. send ( . remove ( $0) ) }
102
+ . onDelete { viewStore. send ( . onDelete ( $0) ) }
103
103
}
104
104
. navigationTitle ( viewStore. state. isEmpty ? " Untitled " : viewStore. state)
105
105
. toolbar {
106
106
ToolbarItem ( placement: . navigationBarTrailing) {
107
- Button ( " Add row " ) { viewStore. send ( . append ) }
107
+ Button ( " Add row " ) { viewStore. send ( . addRowButtonTapped ) }
108
108
}
109
109
}
110
110
}
@@ -113,42 +113,26 @@ struct NestedView: View {
113
113
114
114
extension NestedState {
115
115
static let mock = NestedState (
116
- children: [
117
- NestedState (
118
- children: [
119
- NestedState (
120
- children: [ ] ,
121
- id: UUID ( ) ,
122
- description: " "
123
- )
124
- ] ,
125
- id: UUID ( ) ,
126
- description: " Bar "
127
- ) ,
116
+ id: UUID ( ) ,
117
+ name: " Foo " ,
118
+ rows: [
128
119
NestedState (
129
- children: [
130
- NestedState (
131
- children: [ ] ,
132
- id: UUID ( ) ,
133
- description: " Fizz "
134
- ) ,
135
- NestedState (
136
- children: [ ] ,
137
- id: UUID ( ) ,
138
- description: " Buzz "
139
- ) ,
140
- ] ,
141
120
id: UUID ( ) ,
142
- description: " Baz "
121
+ name: " Bar " ,
122
+ rows: [
123
+ NestedState ( id: UUID ( ) , name: " " , rows: [ ] ) ,
124
+ ]
143
125
) ,
144
126
NestedState (
145
- children: [ ] ,
146
127
id: UUID ( ) ,
147
- description: " "
128
+ name: " Baz " ,
129
+ rows: [
130
+ NestedState ( id: UUID ( ) , name: " Fizz " , rows: [ ] ) ,
131
+ NestedState ( id: UUID ( ) , name: " Buzz " , rows: [ ] ) ,
132
+ ]
148
133
) ,
149
- ] ,
150
- id: UUID ( ) ,
151
- description: " Foo "
134
+ NestedState ( id: UUID ( ) , name: " " , rows: [ ] ) ,
135
+ ]
152
136
)
153
137
}
154
138
0 commit comments