Skip to content

Commit 6f1bc25

Browse files
Modernize examples (#525)
* Refactor some of the example code. * wip * wip * wip * lots more updates * fixes * another fix * todo simplification * fix compiler issue in 11.7 * Update Examples/Todos/Todos/Todos.swift Co-authored-by: Stephen Celis <[email protected]> Co-authored-by: Stephen Celis <[email protected]>
1 parent 3c3fbc8 commit 6f1bc25

File tree

68 files changed

+483
-488
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

68 files changed

+483
-488
lines changed

.github/workflows/ci.yml

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ on:
99
- '*'
1010

1111
jobs:
12-
tests:
12+
library:
1313
runs-on: macos-10.15
1414
strategy:
1515
matrix:
@@ -22,7 +22,21 @@ jobs:
2222
- name: Select Xcode ${{ matrix.xcode }}
2323
run: sudo xcode-select -s /Applications/Xcode_${{ matrix.xcode }}.app
2424
- name: Run tests
25-
run: make test
25+
run: make test-library
26+
27+
examples:
28+
runs-on: macos-10.15
29+
strategy:
30+
matrix:
31+
xcode:
32+
- 11.7
33+
- 12.4
34+
steps:
35+
- uses: actions/checkout@v2
36+
- name: Select Xcode ${{ matrix.xcode }}
37+
run: sudo xcode-select -s /Applications/Xcode_${{ matrix.xcode }}.app
38+
- name: Run tests
39+
run: make test-examples
2640

2741
# NB: GitHub's Big Sur instances are super flaky. We should revisit later.
2842
# bigsur-tests:

Examples/CaseStudies/SwiftUICaseStudies/00-Core.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ struct RootEnvironment {
7878
downloadClient: .live,
7979
favorite: favorite(id:isFavorite:),
8080
fetchNumber: liveFetchNumber,
81-
mainQueue: DispatchQueue.main.eraseToAnyScheduler(),
81+
mainQueue: .main,
8282
numberFact: liveNumberFact(for:),
8383
userDidTakeScreenshot: liveUserDidTakeScreenshot,
8484
uuid: UUID.init,

Examples/CaseStudies/SwiftUICaseStudies/00-RootView.swift

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ struct RootView: View {
1414
"Basics",
1515
destination: CounterDemoView(
1616
store: self.store.scope(
17-
state: { $0.counter },
17+
state: \.counter,
1818
action: RootAction.counter
1919
)
2020
)
@@ -24,7 +24,7 @@ struct RootView: View {
2424
"Pullback and combine",
2525
destination: TwoCountersView(
2626
store: self.store.scope(
27-
state: { $0.twoCounters },
27+
state: \.twoCounters,
2828
action: RootAction.twoCounters
2929
)
3030
)
@@ -34,7 +34,7 @@ struct RootView: View {
3434
"Bindings",
3535
destination: BindingBasicsView(
3636
store: self.store.scope(
37-
state: { $0.bindingBasics },
37+
state: \.bindingBasics,
3838
action: RootAction.bindingBasics
3939
)
4040
)
@@ -44,7 +44,7 @@ struct RootView: View {
4444
"Form bindings",
4545
destination: BindingFormView(
4646
store: self.store.scope(
47-
state: { $0.bindingForm },
47+
state: \.bindingForm,
4848
action: RootAction.bindingForm
4949
)
5050
)
@@ -54,7 +54,7 @@ struct RootView: View {
5454
"Optional state",
5555
destination: OptionalBasicsView(
5656
store: self.store.scope(
57-
state: { $0.optionalBasics },
57+
state: \.optionalBasics,
5858
action: RootAction.optionalBasics
5959
)
6060
)
@@ -64,7 +64,7 @@ struct RootView: View {
6464
"Shared state",
6565
destination: SharedStateView(
6666
store: self.store.scope(
67-
state: { $0.shared },
67+
state: \.shared,
6868
action: RootAction.shared
6969
)
7070
)
@@ -74,7 +74,7 @@ struct RootView: View {
7474
"Alerts and Action Sheets",
7575
destination: AlertAndSheetView(
7676
store: self.store.scope(
77-
state: { $0.alertAndActionSheet },
77+
state: \.alertAndActionSheet,
7878
action: RootAction.alertAndActionSheet
7979
)
8080
)
@@ -84,7 +84,7 @@ struct RootView: View {
8484
"Animations",
8585
destination: AnimationsView(
8686
store: self.store.scope(
87-
state: { $0.animation },
87+
state: \.animation,
8888
action: RootAction.animation
8989
)
9090
)
@@ -96,7 +96,7 @@ struct RootView: View {
9696
"Basics",
9797
destination: EffectsBasicsView(
9898
store: self.store.scope(
99-
state: { $0.effectsBasics },
99+
state: \.effectsBasics,
100100
action: RootAction.effectsBasics
101101
)
102102
)
@@ -106,7 +106,7 @@ struct RootView: View {
106106
"Cancellation",
107107
destination: EffectsCancellationView(
108108
store: self.store.scope(
109-
state: { $0.effectsCancellation },
109+
state: \.effectsCancellation,
110110
action: RootAction.effectsCancellation)
111111
)
112112
)
@@ -115,7 +115,7 @@ struct RootView: View {
115115
"Long-living effects",
116116
destination: LongLivingEffectsView(
117117
store: self.store.scope(
118-
state: { $0.longLivingEffects },
118+
state: \.longLivingEffects,
119119
action: RootAction.longLivingEffects
120120
)
121121
)
@@ -125,7 +125,7 @@ struct RootView: View {
125125
"Timers",
126126
destination: TimersView(
127127
store: self.store.scope(
128-
state: { $0.timers },
128+
state: \.timers,
129129
action: RootAction.timers
130130
)
131131
)
@@ -135,7 +135,7 @@ struct RootView: View {
135135
"System environment",
136136
destination: MultipleDependenciesView(
137137
store: self.store.scope(
138-
state: { $0.multipleDependencies },
138+
state: \.multipleDependencies,
139139
action: RootAction.multipleDependencies
140140
)
141141
)
@@ -145,7 +145,7 @@ struct RootView: View {
145145
"Web socket",
146146
destination: WebSocketView(
147147
store: self.store.scope(
148-
state: { $0.webSocket },
148+
state: \.webSocket,
149149
action: RootAction.webSocket
150150
)
151151
)
@@ -157,7 +157,7 @@ struct RootView: View {
157157
"Navigate and load data",
158158
destination: NavigateAndLoadView(
159159
store: self.store.scope(
160-
state: { $0.navigateAndLoad },
160+
state: \.navigateAndLoad,
161161
action: RootAction.navigateAndLoad
162162
)
163163
)
@@ -167,7 +167,7 @@ struct RootView: View {
167167
"Load data then navigate",
168168
destination: LoadThenNavigateView(
169169
store: self.store.scope(
170-
state: { $0.loadThenNavigate },
170+
state: \.loadThenNavigate,
171171
action: RootAction.loadThenNavigate
172172
)
173173
)
@@ -177,7 +177,7 @@ struct RootView: View {
177177
"Lists: Navigate and load data",
178178
destination: NavigateAndLoadListView(
179179
store: self.store.scope(
180-
state: { $0.navigateAndLoadList },
180+
state: \.navigateAndLoadList,
181181
action: RootAction.navigateAndLoadList
182182
)
183183
)
@@ -187,7 +187,7 @@ struct RootView: View {
187187
"Lists: Load data then navigate",
188188
destination: LoadThenNavigateListView(
189189
store: self.store.scope(
190-
state: { $0.loadThenNavigateList },
190+
state: \.loadThenNavigateList,
191191
action: RootAction.loadThenNavigateList
192192
)
193193
)
@@ -197,7 +197,7 @@ struct RootView: View {
197197
"Sheets: Present and load data",
198198
destination: PresentAndLoadView(
199199
store: self.store.scope(
200-
state: { $0.presentAndLoad },
200+
state: \.presentAndLoad,
201201
action: RootAction.presentAndLoad
202202
)
203203
)
@@ -207,7 +207,7 @@ struct RootView: View {
207207
"Sheets: Load data then present",
208208
destination: LoadThenPresentView(
209209
store: self.store.scope(
210-
state: { $0.loadThenPresent },
210+
state: \.loadThenPresent,
211211
action: RootAction.loadThenPresent
212212
)
213213
)
@@ -219,7 +219,7 @@ struct RootView: View {
219219
"Reusable favoriting component",
220220
destination: EpisodesView(
221221
store: self.store.scope(
222-
state: { $0.episodes },
222+
state: \.episodes,
223223
action: RootAction.episodes
224224
)
225225
)
@@ -229,7 +229,7 @@ struct RootView: View {
229229
"Reusable offline download component",
230230
destination: CitiesView(
231231
store: self.store.scope(
232-
state: { $0.map },
232+
state: \.map,
233233
action: RootAction.map
234234
)
235235
)
@@ -239,7 +239,7 @@ struct RootView: View {
239239
"Lifecycle",
240240
destination: LifecycleDemoView(
241241
store: self.store.scope(
242-
state: { $0.lifecycle },
242+
state: \.lifecycle,
243243
action: RootAction.lifecycle
244244
)
245245
)
@@ -249,7 +249,7 @@ struct RootView: View {
249249
"Strict reducers",
250250
destination: DieRollView(
251251
store: self.store.scope(
252-
state: { $0.dieRoll },
252+
state: \.dieRoll,
253253
action: RootAction.dieRoll
254254
)
255255
)
@@ -259,7 +259,7 @@ struct RootView: View {
259259
"Elm-like subscriptions",
260260
destination: ClockView(
261261
store: self.store.scope(
262-
state: { $0.clock },
262+
state: \.clock,
263263
action: RootAction.clock
264264
)
265265
)
@@ -269,7 +269,7 @@ struct RootView: View {
269269
"Recursive state and actions",
270270
destination: NestedView(
271271
store: self.store.scope(
272-
state: { $0.nested },
272+
state: \.nested,
273273
action: RootAction.nested
274274
)
275275
)

Examples/CaseStudies/SwiftUICaseStudies/01-GettingStarted-AlertsAndActionSheets.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,13 @@ struct AlertAndSheetView: View {
101101

102102
Button("Alert") { viewStore.send(.alertButtonTapped) }
103103
.alert(
104-
self.store.scope(state: { $0.alert }),
104+
self.store.scope(state: \.alert),
105105
dismiss: .alertDismissed
106106
)
107107

108108
Button("Action sheet") { viewStore.send(.actionSheetButtonTapped) }
109109
.actionSheet(
110-
self.store.scope(state: { $0.actionSheet }),
110+
self.store.scope(state: \.actionSheet),
111111
dismiss: .actionSheetDismissed
112112
)
113113
}

Examples/CaseStudies/SwiftUICaseStudies/01-GettingStarted-Animations.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ struct AnimationsView: View {
116116
"Big mode",
117117
isOn:
118118
viewStore
119-
.binding(get: { $0.isCircleScaled }, send: AnimationsAction.circleScaleToggleChanged)
119+
.binding(get: \.isCircleScaled, send: AnimationsAction.circleScaleToggleChanged)
120120
.animation(.interactiveSpring(response: 0.25, dampingFraction: 0.1))
121121
)
122122
.padding()
@@ -137,7 +137,7 @@ struct AnimationsView_Previews: PreviewProvider {
137137
initialState: AnimationsState(circleCenter: CGPoint(x: 50, y: 50)),
138138
reducer: animationsReducer,
139139
environment: AnimationsEnvironment(
140-
mainQueue: DispatchQueue.main.eraseToAnyScheduler()
140+
mainQueue: .main
141141
)
142142
)
143143
)
@@ -149,7 +149,7 @@ struct AnimationsView_Previews: PreviewProvider {
149149
initialState: AnimationsState(circleCenter: CGPoint(x: 50, y: 50)),
150150
reducer: animationsReducer,
151151
environment: AnimationsEnvironment(
152-
mainQueue: DispatchQueue.main.eraseToAnyScheduler()
152+
mainQueue: .main
153153
)
154154
)
155155
)

Examples/CaseStudies/SwiftUICaseStudies/01-GettingStarted-Bindings-Basics.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ struct BindingBasicsView: View {
6969
HStack {
7070
TextField(
7171
"Type here",
72-
text: viewStore.binding(get: { $0.text }, send: BindingBasicsAction.textChange)
72+
text: viewStore.binding(get: \.text, send: BindingBasicsAction.textChange)
7373
)
7474
.disableAutocorrection(true)
7575
.foregroundColor(viewStore.toggleIsOn ? .gray : .primary)
@@ -78,14 +78,14 @@ struct BindingBasicsView: View {
7878
.disabled(viewStore.toggleIsOn)
7979

8080
Toggle(
81-
isOn: viewStore.binding(get: { $0.toggleIsOn }, send: BindingBasicsAction.toggleChange)
81+
isOn: viewStore.binding(get: \.toggleIsOn, send: BindingBasicsAction.toggleChange)
8282
) {
8383
Text("Disable other controls")
8484
}
8585

8686
Stepper(
8787
value: viewStore.binding(
88-
get: { $0.stepCount }, send: BindingBasicsAction.stepCountChanged),
88+
get: \.stepCount, send: BindingBasicsAction.stepCountChanged),
8989
in: 0...100
9090
) {
9191
Text("Max slider value: \(viewStore.stepCount)")
@@ -98,7 +98,7 @@ struct BindingBasicsView: View {
9898
.font(Font.body.monospacedDigit())
9999
Slider(
100100
value: viewStore.binding(
101-
get: { $0.sliderValue },
101+
get: \.sliderValue,
102102
send: BindingBasicsAction.sliderValueChanged
103103
),
104104
in: 0...Double(viewStore.stepCount)

Examples/CaseStudies/SwiftUICaseStudies/01-GettingStarted-Composition-TwoCounters.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ struct TwoCountersView: View {
4444
Text("Counter 1")
4545

4646
CounterView(
47-
store: self.store.scope(state: { $0.counter1 }, action: TwoCountersAction.counter1)
47+
store: self.store.scope(state: \.counter1, action: TwoCountersAction.counter1)
4848
)
4949
.buttonStyle(BorderlessButtonStyle())
5050
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .trailing)
@@ -53,7 +53,7 @@ struct TwoCountersView: View {
5353
Text("Counter 2")
5454

5555
CounterView(
56-
store: self.store.scope(state: { $0.counter2 }, action: TwoCountersAction.counter2)
56+
store: self.store.scope(state: \.counter2, action: TwoCountersAction.counter2)
5757
)
5858
.buttonStyle(BorderlessButtonStyle())
5959
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .trailing)

Examples/CaseStudies/SwiftUICaseStudies/01-GettingStarted-OptionalState.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,9 @@ struct OptionalBasicsView: View {
6161

6262
IfLetStore(
6363
self.store.scope(
64-
state: { $0.optionalCounter }, action: OptionalBasicsAction.optionalCounter),
64+
state: \.optionalCounter,
65+
action: OptionalBasicsAction.optionalCounter
66+
),
6567
then: { store in
6668
VStack(alignment: .leading, spacing: 16) {
6769
Text(template: "`CounterState` is non-`nil`", .body)

0 commit comments

Comments
 (0)