Skip to content

Commit d73c5bc

Browse files
committed
Clean up compiler checks. (#1167)
1 parent 8688396 commit d73c5bc

File tree

14 files changed

+1283
-1448
lines changed

14 files changed

+1283
-1448
lines changed

Examples/CaseStudies/SwiftUICaseStudies/00-Core.swift

Lines changed: 16 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,14 @@ struct RootState {
77
var alertAndConfirmationDialog = AlertAndConfirmationDialogState()
88
var animation = AnimationsState()
99
var bindingBasics = BindingBasicsState()
10-
#if compiler(>=5.4)
11-
var bindingForm = BindingFormState()
12-
#endif
10+
var bindingForm = BindingFormState()
1311
var clock = ClockState()
1412
var counter = CounterState()
1513
var effectsBasics = EffectsBasicsState()
1614
var effectsCancellation = EffectsCancellationState()
1715
var effectsTimers = TimersState()
1816
var episodes = EpisodesState(episodes: .mocks)
19-
#if compiler(>=5.5)
20-
var focusDemo = FocusDemoState()
21-
#endif
17+
var focusDemo = FocusDemoState()
2218
var lifecycle = LifecycleDemoState()
2319
var loadThenNavigate = LoadThenNavigateState()
2420
var loadThenNavigateList = LoadThenNavigateListState()
@@ -42,17 +38,13 @@ enum RootAction {
4238
case alertAndConfirmationDialog(AlertAndConfirmationDialogAction)
4339
case animation(AnimationsAction)
4440
case bindingBasics(BindingBasicsAction)
45-
#if compiler(>=5.4)
46-
case bindingForm(BindingFormAction)
47-
#endif
41+
case bindingForm(BindingFormAction)
4842
case clock(ClockAction)
4943
case counter(CounterAction)
5044
case effectsBasics(EffectsBasicsAction)
5145
case effectsCancellation(EffectsCancellationAction)
5246
case episodes(EpisodesAction)
53-
#if compiler(>=5.5)
54-
case focusDemo(FocusDemoAction)
55-
#endif
47+
case focusDemo(FocusDemoAction)
5648
case lifecycle(LifecycleDemoAction)
5749
case loadThenNavigate(LoadThenNavigateAction)
5850
case loadThenNavigateList(LoadThenNavigateListAction)
@@ -126,20 +118,12 @@ let rootReducer = Reducer<RootState, RootAction, RootEnvironment>.combine(
126118
action: /RootAction.bindingBasics,
127119
environment: { _ in .init() }
128120
),
129-
.init { state, action, environment in
130-
#if compiler(>=5.4)
131-
return
132-
bindingFormReducer
133-
.pullback(
134-
state: \.bindingForm,
135-
action: /RootAction.bindingForm,
136-
environment: { _ in .init() }
137-
)
138-
.run(&state, action, environment)
139-
#else
140-
return .none
141-
#endif
142-
},
121+
bindingFormReducer
122+
.pullback(
123+
state: \.bindingForm,
124+
action: /RootAction.bindingForm,
125+
environment: { _ in .init() }
126+
),
143127
clockReducer
144128
.pullback(
145129
state: \.clock,
@@ -170,20 +154,12 @@ let rootReducer = Reducer<RootState, RootAction, RootEnvironment>.combine(
170154
action: /RootAction.episodes,
171155
environment: { .init(favorite: $0.favorite, mainQueue: $0.mainQueue) }
172156
),
173-
.init { state, action, environment in
174-
#if compiler(>=5.5)
175-
return
176-
focusDemoReducer
177-
.pullback(
178-
state: \.focusDemo,
179-
action: /RootAction.focusDemo,
180-
environment: { _ in .init() }
181-
)
182-
.run(&state, action, environment)
183-
#else
184-
return .none
185-
#endif
186-
},
157+
focusDemoReducer
158+
.pullback(
159+
state: \.focusDemo,
160+
action: /RootAction.focusDemo,
161+
environment: { _ in .init() }
162+
),
187163
lifecycleDemoReducer
188164
.pullback(
189165
state: \.lifecycle,

Examples/CaseStudies/SwiftUICaseStudies/00-RootView.swift

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,15 @@ struct RootView: View {
4141
)
4242
)
4343

44-
#if compiler(>=5.4)
45-
NavigationLink(
46-
"Form bindings",
47-
destination: BindingFormView(
48-
store: self.store.scope(
49-
state: \.bindingForm,
50-
action: RootAction.bindingForm
51-
)
44+
NavigationLink(
45+
"Form bindings",
46+
destination: BindingFormView(
47+
store: self.store.scope(
48+
state: \.bindingForm,
49+
action: RootAction.bindingForm
5250
)
5351
)
54-
#endif
52+
)
5553

5654
NavigationLink(
5755
"Optional state",
@@ -83,17 +81,15 @@ struct RootView: View {
8381
)
8482
)
8583

86-
#if compiler(>=5.5)
87-
NavigationLink(
88-
"Focus State",
89-
destination: FocusDemoView(
90-
store: self.store.scope(
91-
state: \.focusDemo,
92-
action: RootAction.focusDemo
93-
)
84+
NavigationLink(
85+
"Focus State",
86+
destination: FocusDemoView(
87+
store: self.store.scope(
88+
state: \.focusDemo,
89+
action: RootAction.focusDemo
9490
)
9591
)
96-
#endif
92+
)
9793

9894
NavigationLink(
9995
"Animations",
@@ -136,17 +132,15 @@ struct RootView: View {
136132
)
137133
)
138134

139-
#if compiler(>=5.5)
140-
NavigationLink(
141-
"Refreshable",
142-
destination: RefreshableView(
143-
store: self.store.scope(
144-
state: \.refreshable,
145-
action: RootAction.refreshable
146-
)
135+
NavigationLink(
136+
"Refreshable",
137+
destination: RefreshableView(
138+
store: self.store.scope(
139+
state: \.refreshable,
140+
action: RootAction.refreshable
147141
)
148142
)
149-
#endif
143+
)
150144

151145
NavigationLink(
152146
"Timers",
Lines changed: 106 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -1,122 +1,120 @@
1-
#if compiler(>=5.4)
2-
import ComposableArchitecture
3-
import SwiftUI
4-
5-
private let readMe = """
6-
This file demonstrates how to handle two-way bindings in the Composable Architecture using \
7-
bindable state and actions.
8-
9-
Bindable state and actions allow you to safely eliminate the boilerplate caused by needing to \
10-
have a unique action for every UI control. Instead, all UI bindings can be consolidated into a \
11-
single `binding` action that holds onto a `BindingAction` value, and all bindable state can be \
12-
safeguarded with the `BindableState` property wrapper.
13-
14-
It is instructive to compare this case study to the "Binding Basics" case study.
15-
"""
16-
17-
// The state for this screen holds a bunch of values that will drive
18-
struct BindingFormState: Equatable {
19-
@BindableState var sliderValue = 5.0
20-
@BindableState var stepCount = 10
21-
@BindableState var text = ""
22-
@BindableState var toggleIsOn = false
1+
import ComposableArchitecture
2+
import SwiftUI
3+
4+
private let readMe = """
5+
This file demonstrates how to handle two-way bindings in the Composable Architecture using \
6+
bindable state and actions.
7+
8+
Bindable state and actions allow you to safely eliminate the boilerplate caused by needing to \
9+
have a unique action for every UI control. Instead, all UI bindings can be consolidated into a \
10+
single `binding` action that holds onto a `BindingAction` value, and all bindable state can be \
11+
safeguarded with the `BindableState` property wrapper.
12+
13+
It is instructive to compare this case study to the "Binding Basics" case study.
14+
"""
15+
16+
// The state for this screen holds a bunch of values that will drive
17+
struct BindingFormState: Equatable {
18+
@BindableState var sliderValue = 5.0
19+
@BindableState var stepCount = 10
20+
@BindableState var text = ""
21+
@BindableState var toggleIsOn = false
22+
}
23+
24+
enum BindingFormAction: BindableAction, Equatable {
25+
case binding(BindingAction<BindingFormState>)
26+
case resetButtonTapped
27+
}
28+
29+
struct BindingFormEnvironment {}
30+
31+
let bindingFormReducer = Reducer<
32+
BindingFormState, BindingFormAction, BindingFormEnvironment
33+
> {
34+
state, action, _ in
35+
switch action {
36+
case .binding(\.$stepCount):
37+
state.sliderValue = .minimum(state.sliderValue, Double(state.stepCount))
38+
return .none
39+
40+
case .binding:
41+
return .none
42+
43+
case .resetButtonTapped:
44+
state = BindingFormState()
45+
return .none
2346
}
47+
}
48+
.binding()
49+
50+
struct BindingFormView: View {
51+
let store: Store<BindingFormState, BindingFormAction>
52+
53+
var body: some View {
54+
WithViewStore(self.store) { viewStore in
55+
Form {
56+
Section(header: Text(template: readMe, .caption)) {
57+
HStack {
58+
TextField("Type here", text: viewStore.binding(\.$text))
59+
.disableAutocorrection(true)
60+
.foregroundColor(viewStore.toggleIsOn ? .gray : .primary)
61+
62+
Text(alternate(viewStore.text))
63+
}
64+
.disabled(viewStore.toggleIsOn)
2465

25-
enum BindingFormAction: BindableAction, Equatable {
26-
case binding(BindingAction<BindingFormState>)
27-
case resetButtonTapped
28-
}
66+
Toggle(
67+
"Disable other controls",
68+
isOn: viewStore.binding(\.$toggleIsOn)
69+
.resignFirstResponder()
70+
)
2971

30-
struct BindingFormEnvironment {}
72+
Stepper(value: viewStore.binding(\.$stepCount), in: 0...100) {
73+
Text("Max slider value: \(viewStore.stepCount)")
74+
.font(.body.monospacedDigit())
75+
}
76+
.disabled(viewStore.toggleIsOn)
3177

32-
let bindingFormReducer = Reducer<
33-
BindingFormState, BindingFormAction, BindingFormEnvironment
34-
> {
35-
state, action, _ in
36-
switch action {
37-
case .binding(\.$stepCount):
38-
state.sliderValue = .minimum(state.sliderValue, Double(state.stepCount))
39-
return .none
78+
HStack {
79+
Text("Slider value: \(Int(viewStore.sliderValue))")
80+
.font(.body.monospacedDigit())
4081

41-
case .binding:
42-
return .none
82+
Slider(value: viewStore.binding(\.$sliderValue), in: 0...Double(viewStore.stepCount))
83+
}
84+
.disabled(viewStore.toggleIsOn)
4385

44-
case .resetButtonTapped:
45-
state = BindingFormState()
46-
return .none
47-
}
48-
}
49-
.binding()
50-
51-
struct BindingFormView: View {
52-
let store: Store<BindingFormState, BindingFormAction>
53-
54-
var body: some View {
55-
WithViewStore(self.store) { viewStore in
56-
Form {
57-
Section(header: Text(template: readMe, .caption)) {
58-
HStack {
59-
TextField("Type here", text: viewStore.binding(\.$text))
60-
.disableAutocorrection(true)
61-
.foregroundColor(viewStore.toggleIsOn ? .gray : .primary)
62-
63-
Text(alternate(viewStore.text))
64-
}
65-
.disabled(viewStore.toggleIsOn)
66-
67-
Toggle(
68-
"Disable other controls",
69-
isOn: viewStore.binding(\.$toggleIsOn)
70-
.resignFirstResponder()
71-
)
72-
73-
Stepper(value: viewStore.binding(\.$stepCount), in: 0...100) {
74-
Text("Max slider value: \(viewStore.stepCount)")
75-
.font(.body.monospacedDigit())
76-
}
77-
.disabled(viewStore.toggleIsOn)
78-
79-
HStack {
80-
Text("Slider value: \(Int(viewStore.sliderValue))")
81-
.font(.body.monospacedDigit())
82-
83-
Slider(value: viewStore.binding(\.$sliderValue), in: 0...Double(viewStore.stepCount))
84-
}
85-
.disabled(viewStore.toggleIsOn)
86-
87-
Button("Reset") {
88-
viewStore.send(.resetButtonTapped)
89-
}
90-
.foregroundColor(.red)
86+
Button("Reset") {
87+
viewStore.send(.resetButtonTapped)
9188
}
89+
.foregroundColor(.red)
9290
}
9391
}
94-
.navigationBarTitle("Bindings form")
9592
}
93+
.navigationBarTitle("Bindings form")
9694
}
97-
98-
private func alternate(_ string: String) -> String {
99-
string
100-
.enumerated()
101-
.map { idx, char in
102-
idx.isMultiple(of: 2)
103-
? char.uppercased()
104-
: char.lowercased()
105-
}
106-
.joined()
107-
}
108-
109-
struct BindingFormView_Previews: PreviewProvider {
110-
static var previews: some View {
111-
NavigationView {
112-
BindingFormView(
113-
store: Store(
114-
initialState: BindingFormState(),
115-
reducer: bindingFormReducer,
116-
environment: BindingFormEnvironment()
117-
)
95+
}
96+
97+
private func alternate(_ string: String) -> String {
98+
string
99+
.enumerated()
100+
.map { idx, char in
101+
idx.isMultiple(of: 2)
102+
? char.uppercased()
103+
: char.lowercased()
104+
}
105+
.joined()
106+
}
107+
108+
struct BindingFormView_Previews: PreviewProvider {
109+
static var previews: some View {
110+
NavigationView {
111+
BindingFormView(
112+
store: Store(
113+
initialState: BindingFormState(),
114+
reducer: bindingFormReducer,
115+
environment: BindingFormEnvironment()
118116
)
119-
}
117+
)
120118
}
121119
}
122-
#endif
120+
}

0 commit comments

Comments
 (0)