-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
EnumTestCase.swift
142 lines (138 loc) · 3.85 KB
/
EnumTestCase.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
@_spi(Logging) import ComposableArchitecture
import SwiftUI
struct EnumView: View {
@State var store = Store(initialState: Feature.State()) {
Feature()
}
struct ViewState: Equatable {
enum Tag { case feature1, feature2, none }
let tag: Tag
init(state: Feature.State) {
switch state.destination {
case .feature1:
self.tag = .feature1
case .feature2:
self.tag = .feature2
case .none:
self.tag = .none
}
}
}
var body: some View {
Form {
WithViewStore(self.store, observe: ViewState.init) { viewStore in
let _ = Logger.shared.log("\(Self.self).body")
Section {
switch viewStore.tag {
case .feature1:
Button("Toggle feature 1 off") {
self.store.send(.toggle1ButtonTapped)
}
Button("Toggle feature 2 on") {
self.store.send(.toggle2ButtonTapped)
}
case .feature2:
Button("Toggle feature 1 on") {
self.store.send(.toggle1ButtonTapped)
}
Button("Toggle feature 2 off") {
self.store.send(.toggle2ButtonTapped)
}
case .none:
Button("Toggle feature 1 on") {
self.store.send(.toggle1ButtonTapped)
}
Button("Toggle feature 2 on") {
self.store.send(.toggle2ButtonTapped)
}
}
}
}
}
IfLetStore(
self.store.scope(state: \.$destination, action: { .destination($0) }),
state: /Feature.Destination.State.feature1,
action: { .feature1($0) }
) { store in
Section {
Form {
BasicsView(store: store)
}
} header: {
Text("Feature 1")
}
}
IfLetStore(
self.store.scope(state: \.$destination, action: { .destination($0) }),
state: /Feature.Destination.State.feature2,
action: { .feature2($0) }
) { store in
Section {
Form {
BasicsView(store: store)
}
} header: {
Text("Feature 2")
}
}
}
struct Feature: Reducer {
struct State: Equatable {
@PresentationState var destination: Destination.State?
}
enum Action {
case destination(PresentationAction<Destination.Action>)
case toggle1ButtonTapped
case toggle2ButtonTapped
}
struct Destination: Reducer {
enum State: Equatable {
case feature1(BasicsView.Feature.State)
case feature2(BasicsView.Feature.State)
}
enum Action {
case feature1(BasicsView.Feature.Action)
case feature2(BasicsView.Feature.Action)
}
var body: some ReducerOf<Self> {
Scope(state: /State.feature1, action: /Action.feature1) {
BasicsView.Feature()
}
Scope(state: /State.feature2, action: /Action.feature2) {
BasicsView.Feature()
}
}
}
var body: some ReducerOf<Self> {
Reduce { state, action in
switch action {
case .destination:
return .none
case .toggle1ButtonTapped:
switch state.destination {
case .feature1:
state.destination = nil
case .feature2:
state.destination = .feature1(BasicsView.Feature.State())
case .none:
state.destination = .feature1(BasicsView.Feature.State())
}
return .none
case .toggle2ButtonTapped:
switch state.destination {
case .feature1:
state.destination = .feature2(BasicsView.Feature.State())
case .feature2:
state.destination = nil
case .none:
state.destination = .feature2(BasicsView.Feature.State())
}
return .none
}
}
.ifLet(\.$destination, action: /Action.destination) {
Destination()
}
}
}
}