@@ -7,6 +7,13 @@ private let readMe = """
7
7
8
8
@Reducer
9
9
struct NavigationDemo {
10
+ @Reducer ( state: . equatable)
11
+ enum Path {
12
+ case screenA( ScreenA )
13
+ case screenB( ScreenB )
14
+ case screenC( ScreenC )
15
+ }
16
+
10
17
@ObservableState
11
18
struct State : Equatable {
12
19
var path = StackState < Path . State > ( )
@@ -27,23 +34,23 @@ struct NavigationDemo {
27
34
return . none
28
35
29
36
case . goToABCButtonTapped:
30
- state. path. append ( . screenA( ) )
31
- state. path. append ( . screenB( ) )
32
- state. path. append ( . screenC( ) )
37
+ state. path. append ( . screenA( ScreenA . State ( ) ) )
38
+ state. path. append ( . screenB( ScreenB . State ( ) ) )
39
+ state. path. append ( . screenC( ScreenC . State ( ) ) )
33
40
return . none
34
41
35
42
case let . path( action) :
36
43
switch action {
37
44
case . element( id: _, action: . screenB( . screenAButtonTapped) ) :
38
- state. path. append ( . screenA( ) )
45
+ state. path. append ( . screenA( ScreenA . State ( ) ) )
39
46
return . none
40
47
41
48
case . element( id: _, action: . screenB( . screenBButtonTapped) ) :
42
- state. path. append ( . screenB( ) )
49
+ state. path. append ( . screenB( ScreenB . State ( ) ) )
43
50
return . none
44
51
45
52
case . element( id: _, action: . screenB( . screenCButtonTapped) ) :
46
- state. path. append ( . screenC( ) )
53
+ state. path. append ( . screenC( ScreenC . State ( ) ) )
47
54
return . none
48
55
49
56
default :
@@ -55,37 +62,7 @@ struct NavigationDemo {
55
62
return . none
56
63
}
57
64
}
58
- . forEach ( \. path, action: \. path) {
59
- Path ( )
60
- }
61
- }
62
-
63
- @Reducer
64
- struct Path {
65
- @ObservableState
66
- enum State : Equatable {
67
- case screenA( ScreenA . State = . init( ) )
68
- case screenB( ScreenB . State = . init( ) )
69
- case screenC( ScreenC . State = . init( ) )
70
- }
71
-
72
- enum Action {
73
- case screenA( ScreenA . Action )
74
- case screenB( ScreenB . Action )
75
- case screenC( ScreenC . Action )
76
- }
77
-
78
- var body : some Reducer < State , Action > {
79
- Scope ( state: \. screenA, action: \. screenA) {
80
- ScreenA ( )
81
- }
82
- Scope ( state: \. screenB, action: \. screenB) {
83
- ScreenB ( )
84
- }
85
- Scope ( state: \. screenC, action: \. screenC) {
86
- ScreenC ( )
87
- }
88
- }
65
+ . forEach ( \. path, action: \. path)
89
66
}
90
67
}
91
68
@@ -100,15 +77,15 @@ struct NavigationDemoView: View {
100
77
Section {
101
78
NavigationLink (
102
79
" Go to screen A " ,
103
- state: NavigationDemo . Path. State. screenA ( )
80
+ state: NavigationDemo . Path. State. screenA ( ScreenA . State ( ) )
104
81
)
105
82
NavigationLink (
106
83
" Go to screen B " ,
107
- state: NavigationDemo . Path. State. screenB ( )
84
+ state: NavigationDemo . Path. State. screenB ( ScreenB . State ( ) )
108
85
)
109
86
NavigationLink (
110
87
" Go to screen C " ,
111
- state: NavigationDemo . Path. State. screenC ( )
88
+ state: NavigationDemo . Path. State. screenC ( ScreenC . State ( ) )
112
89
)
113
90
}
114
91
@@ -120,19 +97,13 @@ struct NavigationDemoView: View {
120
97
}
121
98
. navigationTitle ( " Root " )
122
99
} destination: { store in
123
- switch store. state {
124
- case . screenA:
125
- if let store = store. scope ( state: \. screenA, action: \. screenA) {
126
- ScreenAView ( store: store)
127
- }
128
- case . screenB:
129
- if let store = store. scope ( state: \. screenB, action: \. screenB) {
130
- ScreenBView ( store: store)
131
- }
132
- case . screenC:
133
- if let store = store. scope ( state: \. screenC, action: \. screenC) {
134
- ScreenCView ( store: store)
135
- }
100
+ switch store. case {
101
+ case let . screenA( store) :
102
+ ScreenAView ( store: store)
103
+ case let . screenB( store) :
104
+ ScreenBView ( store: store)
105
+ case let . screenC( store) :
106
+ ScreenCView ( store: store)
136
107
}
137
108
}
138
109
. safeAreaInset ( edge: . bottom) {
@@ -318,15 +289,15 @@ struct ScreenAView: View {
318
289
Section {
319
290
NavigationLink (
320
291
" Go to screen A " ,
321
- state: NavigationDemo . Path. State. screenA ( . init ( count: store. count) )
292
+ state: NavigationDemo . Path. State. screenA ( ScreenA . State ( count: store. count) )
322
293
)
323
294
NavigationLink (
324
295
" Go to screen B " ,
325
- state: NavigationDemo . Path. State. screenB ( )
296
+ state: NavigationDemo . Path. State. screenB ( ScreenB . State ( ) )
326
297
)
327
298
NavigationLink (
328
299
" Go to screen C " ,
329
- state: NavigationDemo . Path. State. screenC ( . init ( count: store. count) )
300
+ state: NavigationDemo . Path. State. screenC ( ScreenC . State ( count: store. count) )
330
301
)
331
302
}
332
303
}
@@ -460,11 +431,11 @@ struct ScreenCView: View {
460
431
)
461
432
NavigationLink (
462
433
" Go to screen B " ,
463
- state: NavigationDemo . Path. State. screenB ( )
434
+ state: NavigationDemo . Path. State. screenB ( ScreenB . State ( ) )
464
435
)
465
436
NavigationLink (
466
437
" Go to screen C " ,
467
- state: NavigationDemo . Path. State. screenC ( )
438
+ state: NavigationDemo . Path. State. screenC ( ScreenC . State ( ) )
468
439
)
469
440
}
470
441
}
@@ -476,13 +447,7 @@ struct ScreenCView: View {
476
447
477
448
#Preview {
478
449
NavigationDemoView (
479
- store: Store (
480
- initialState: NavigationDemo . State (
481
- path: StackState ( [
482
- . screenA( ScreenA . State ( ) )
483
- ] )
484
- )
485
- ) {
450
+ store: Store ( initialState: NavigationDemo . State ( ) ) {
486
451
NavigationDemo ( )
487
452
}
488
453
)
0 commit comments