Skip to content

Commit 6bc2c6a

Browse files
tgrapperonmbrandonw
authored andcommitted
Modernize SwiftUI Case Studies (#1208)
* Replace `.navigationBarTitle` by `.navigationTitle` * Remove .background from Animation * Update 01-GettingStarted-Bindings-Basics.swift * Update 01-GettingStarted-Bindings-Forms.swift * Update 01-GettingStarted-Composition-TwoCounters.swift * Update 01-GettingStarted-Counter.swift * Update 01-GettingStarted-FocusState.swift * Update 01-GettingStarted-OptionalState.swift * Update 01-GettingStarted-SharedState.swift * Update 02-Effects-Basics.swift * Update 02-Effects-Cancellation.swift * Update 02-Effects-LongLiving.swift * Update 02-Effects-Refreshable.swift * Update 02-Effects-Timers.swift * Update 02-Effects-WebSocket.swift * Update 02-Effects-SystemEnvironment.swift * Update 03-Navigation-NavigateAndLoad.swift * Update 04-HigherOrderReducers-ElmLikeSubscriptions.swift * Update 04-HigherOrderReducers-Recursion.swift * Update 04-HigherOrderReducers-ReusableFavoriting.swift * Update CircularProgressView.swift * Update DownloadComponent.swift * Reindent `XCTestDynamicOverlay` block * Use a dedicated shape for the clock hand This is a little much simpler I guess. * Go back to use a Path, much simpler to understand. * Use more natural modifiers to apply styles * small updates * Center the "clocks" horizontally Co-authored-by: Brandon Williams <[email protected]>
1 parent 2678418 commit 6bc2c6a

File tree

39 files changed

+216
-193
lines changed

39 files changed

+216
-193
lines changed

Examples/CaseStudies/SwiftUICaseStudies/00-RootView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ struct RootView: View {
287287
)
288288
}
289289
}
290-
.navigationBarTitle("Case Studies")
290+
.navigationTitle("Case Studies")
291291
.onAppear { viewStore.send(.onAppear) }
292292
}
293293
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ struct AlertAndConfirmationDialogView: View {
9999
Button("Confirmation Dialog") { viewStore.send(.confirmationDialogButtonTapped) }
100100
}
101101
}
102-
.navigationBarTitle("Alerts & Dialogs")
102+
.navigationTitle("Alerts & Dialogs")
103103
.alert(
104104
self.store.scope(state: \.alert),
105105
dismiss: .alertDismissed

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,6 @@ let animationsReducer = Reducer<AnimationsState, AnimationsAction, AnimationsEnv
105105
}
106106

107107
struct AnimationsView: View {
108-
@Environment(\.colorScheme) var colorScheme
109108
let store: Store<AnimationsState, AnimationsAction>
110109

111110
var body: some View {
@@ -150,7 +149,6 @@ struct AnimationsView: View {
150149
Button("Reset") { viewStore.send(.resetButtonTapped) }
151150
.padding([.horizontal, .bottom])
152151
}
153-
.background(self.colorScheme == .dark ? Color.black : .white)
154152
.alert(self.store.scope(state: \.alert), dismiss: .dismissAlert)
155153
.navigationBarTitleDisplayMode(.inline)
156154
}

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

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -75,43 +75,40 @@ struct BindingBasicsView: View {
7575
text: viewStore.binding(get: \.text, send: BindingBasicsAction.textChanged)
7676
)
7777
.disableAutocorrection(true)
78-
.foregroundColor(viewStore.toggleIsOn ? .gray : .primary)
78+
.foregroundStyle(viewStore.toggleIsOn ? Color.secondary : .primary)
7979
Text(alternate(viewStore.text))
8080
}
8181
.disabled(viewStore.toggleIsOn)
82-
82+
8383
Toggle(
84+
"Disable other controls",
8485
isOn: viewStore.binding(get: \.toggleIsOn, send: BindingBasicsAction.toggleChanged)
8586
.resignFirstResponder()
86-
) {
87-
Text("Disable other controls")
88-
}
87+
)
8988

9089
Stepper(
91-
value: viewStore.binding(
92-
get: \.stepCount, send: BindingBasicsAction.stepCountChanged),
90+
"Max slider value: \(viewStore.stepCount)",
91+
value: viewStore.binding(get: \.stepCount, send: BindingBasicsAction.stepCountChanged),
9392
in: 0...100
94-
) {
95-
Text("Max slider value: \(viewStore.stepCount)")
96-
.font(.body.monospacedDigit())
97-
}
93+
)
9894
.disabled(viewStore.toggleIsOn)
9995

10096
HStack {
10197
Text("Slider value: \(Int(viewStore.sliderValue))")
102-
.font(.body.monospacedDigit())
10398
Slider(
10499
value: viewStore.binding(
105100
get: \.sliderValue,
106101
send: BindingBasicsAction.sliderValueChanged
107102
),
108103
in: 0...Double(viewStore.stepCount)
109104
)
105+
.tint(.accentColor)
110106
}
111107
.disabled(viewStore.toggleIsOn)
112108
}
113109
}
114-
.navigationBarTitle("Bindings basics")
110+
.monospacedDigit()
111+
.navigationTitle("Bindings basics")
115112
}
116113
}
117114

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

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,7 @@ struct BindingFormView: View {
6060
HStack {
6161
TextField("Type here", text: viewStore.binding(\.$text))
6262
.disableAutocorrection(true)
63-
.foregroundColor(viewStore.toggleIsOn ? .gray : .primary)
64-
63+
.foregroundStyle(viewStore.toggleIsOn ? Color.secondary : .primary)
6564
Text(alternate(viewStore.text))
6665
}
6766
.disabled(viewStore.toggleIsOn)
@@ -72,27 +71,29 @@ struct BindingFormView: View {
7271
.resignFirstResponder()
7372
)
7473

75-
Stepper(value: viewStore.binding(\.$stepCount), in: 0...100) {
76-
Text("Max slider value: \(viewStore.stepCount)")
77-
.font(.body.monospacedDigit())
78-
}
74+
Stepper(
75+
"Max slider value: \(viewStore.stepCount)",
76+
value: viewStore.binding(\.$stepCount),
77+
in: 0...100
78+
)
7979
.disabled(viewStore.toggleIsOn)
8080

8181
HStack {
8282
Text("Slider value: \(Int(viewStore.sliderValue))")
83-
.font(.body.monospacedDigit())
8483

8584
Slider(value: viewStore.binding(\.$sliderValue), in: 0...Double(viewStore.stepCount))
85+
.tint(.accentColor)
8686
}
8787
.disabled(viewStore.toggleIsOn)
8888

8989
Button("Reset") {
9090
viewStore.send(.resetButtonTapped)
9191
}
92-
.foregroundColor(.red)
92+
.tint(.red)
9393
}
9494
}
95-
.navigationBarTitle("Bindings form")
95+
.monospacedDigit()
96+
.navigationTitle("Bindings form")
9697
}
9798
}
9899

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

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,27 +42,25 @@ struct TwoCountersView: View {
4242
Section {
4343
AboutView(readMe: readMe)
4444
}
45+
4546
HStack {
4647
Text("Counter 1")
47-
48+
Spacer()
4849
CounterView(
4950
store: self.store.scope(state: \.counter1, action: TwoCountersAction.counter1)
5051
)
51-
.buttonStyle(.borderless)
52-
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .trailing)
5352
}
53+
5454
HStack {
5555
Text("Counter 2")
56-
56+
Spacer()
5757
CounterView(
5858
store: self.store.scope(state: \.counter2, action: TwoCountersAction.counter2)
5959
)
60-
.buttonStyle(.borderless)
61-
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .trailing)
62-
6360
}
6461
}
65-
.navigationBarTitle("Two counter demo")
62+
.buttonStyle(.borderless)
63+
.navigationTitle("Two counter demo")
6664
}
6765
}
6866

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

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,20 @@ struct CounterView: View {
3737
var body: some View {
3838
WithViewStore(self.store) { viewStore in
3939
HStack {
40-
Button("") { viewStore.send(.decrementButtonTapped) }
40+
Button {
41+
viewStore.send(.decrementButtonTapped)
42+
} label: {
43+
Image(systemName: "minus")
44+
}
45+
4146
Text("\(viewStore.count)")
42-
.font(.body.monospacedDigit())
43-
Button("+") { viewStore.send(.incrementButtonTapped) }
47+
.monospacedDigit()
48+
49+
Button {
50+
viewStore.send(.incrementButtonTapped)
51+
} label: {
52+
Image(systemName: "plus")
53+
}
4454
}
4555
}
4656
}
@@ -57,11 +67,11 @@ struct CounterDemoView: View {
5767

5868
Section {
5969
CounterView(store: self.store)
60-
.buttonStyle(.borderless)
61-
.frame(maxWidth: .infinity, maxHeight: .infinity)
70+
.frame(maxWidth: .infinity)
6271
}
6372
}
64-
.navigationBarTitle("Counter demo")
73+
.buttonStyle(.borderless)
74+
.navigationTitle("Counter demo")
6575
}
6676
}
6777

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

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,27 +49,25 @@ struct FocusDemoView: View {
4949

5050
var body: some View {
5151
WithViewStore(self.store) { viewStore in
52-
VStack(alignment: .leading, spacing: 32) {
53-
Text(template: readMe, .caption)
52+
Form {
53+
AboutView(readMe: readMe)
5454

5555
VStack {
5656
TextField("Username", text: viewStore.binding(\.$username))
5757
.focused($focusedField, equals: .username)
5858

5959
SecureField("Password", text: viewStore.binding(\.$password))
6060
.focused($focusedField, equals: .password)
61-
6261
Button("Sign In") {
6362
viewStore.send(.signInButtonTapped)
6463
}
64+
.buttonStyle(.borderedProminent)
6565
}
66-
67-
Spacer()
66+
.textFieldStyle(.roundedBorder)
6867
}
69-
.padding()
7068
.synchronize(viewStore.binding(\.$focusedField), self.$focusedField)
7169
}
72-
.navigationBarTitle("Focus demo")
70+
.navigationTitle("Focus demo")
7371
}
7472
}
7573

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,19 +68,18 @@ struct OptionalBasicsView: View {
6868
action: OptionalBasicsAction.optionalCounter
6969
),
7070
then: { store in
71-
VStack(alignment: .leading, spacing: 16) {
7271
Text(template: "`CounterState` is non-`nil`")
7372
CounterView(store: store)
74-
.buttonStyle(.borderless)
75-
}
73+
.buttonStyle(.borderless)
74+
.frame(maxWidth: .infinity)
7675
},
7776
else: {
7877
Text(template: "`CounterState` is `nil`")
7978
}
8079
)
8180
}
8281
}
83-
.navigationBarTitle("Optional state")
82+
.navigationTitle("Optional state")
8483
}
8584
}
8685

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

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -193,20 +193,27 @@ struct SharedStateCounterView: View {
193193

194194
VStack(spacing: 16) {
195195
HStack {
196-
Button("") { viewStore.send(.decrementButtonTapped) }
196+
Button {
197+
viewStore.send(.decrementButtonTapped)
198+
} label: {
199+
Image(systemName: "minus")
200+
}
197201

198202
Text("\(viewStore.count)")
199-
.font(.body.monospacedDigit())
200-
201-
Button("+") { viewStore.send(.incrementButtonTapped) }
203+
.monospacedDigit()
204+
205+
Button {
206+
viewStore.send(.incrementButtonTapped)
207+
} label: {
208+
Image(systemName: "plus")
209+
}
202210
}
203211

204212
Button("Is this prime?") { viewStore.send(.isPrimeButtonTapped) }
205213
}
206214
}
207-
.padding(16)
208-
.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity, alignment: .top)
209-
.navigationBarTitle("Shared State Demo")
215+
.padding(.top)
216+
.navigationTitle("Shared State Demo")
210217
.alert(self.store.scope(state: \.alert), dismiss: .alertDismissed)
211218
}
212219
}
@@ -238,9 +245,8 @@ struct SharedStateProfileView: View {
238245
Button("Reset") { viewStore.send(.resetCounterButtonTapped) }
239246
}
240247
}
241-
.padding(16)
242-
.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity, alignment: .top)
243-
.navigationBarTitle("Profile")
248+
.padding(.top)
249+
.navigationTitle("Profile")
244250
}
245251
}
246252
}

0 commit comments

Comments
 (0)