Skip to content

Commit abb3914

Browse files
committed
Clean up sample code (#1156)
We employ a few tricks that might be confusing folks that are unfamiliar with them, so this dials some things down in the sample code and docs. We could maybe take it further, but this is a start.
1 parent 625a3e0 commit abb3914

File tree

65 files changed

+1152
-1063
lines changed

Some content is hidden

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

65 files changed

+1152
-1063
lines changed

Examples/CaseStudies/SwiftUICaseStudies/00-RootView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ struct RootView: View {
303303
struct RootView_Previews: PreviewProvider {
304304
static var previews: some View {
305305
RootView(
306-
store: .init(
306+
store: Store(
307307
initialState: RootState(),
308308
reducer: rootReducer,
309309
environment: .live

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

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,11 @@ let alertAndConfirmationDialogReducer = Reducer<
4343

4444
switch action {
4545
case .alertButtonTapped:
46-
state.alert = .init(
47-
title: .init("Alert!"),
48-
message: .init("This is an alert"),
49-
primaryButton: .cancel(.init("Cancel")),
50-
secondaryButton: .default(.init("Increment"), action: .send(.incrementButtonTapped))
46+
state.alert = AlertState(
47+
title: TextState("Alert!"),
48+
message: TextState("This is an alert"),
49+
primaryButton: .cancel(TextState("Cancel")),
50+
secondaryButton: .default(TextState("Increment"), action: .send(.incrementButtonTapped))
5151
)
5252
return .none
5353

@@ -56,13 +56,13 @@ let alertAndConfirmationDialogReducer = Reducer<
5656
return .none
5757

5858
case .confirmationDialogButtonTapped:
59-
state.confirmationDialog = .init(
60-
title: .init("Confirmation dialog"),
61-
message: .init("This is a confirmation dialog."),
59+
state.confirmationDialog = ConfirmationDialogState(
60+
title: TextState("Confirmation dialog"),
61+
message: TextState("This is a confirmation dialog."),
6262
buttons: [
63-
.cancel(.init("Cancel")),
64-
.default(.init("Increment"), action: .send(.incrementButtonTapped)),
65-
.default(.init("Decrement"), action: .send(.decrementButtonTapped)),
63+
.cancel(TextState("Cancel")),
64+
.default(TextState("Increment"), action: .send(.incrementButtonTapped)),
65+
.default(TextState("Decrement"), action: .send(.decrementButtonTapped)),
6666
]
6767
)
6868
return .none
@@ -72,12 +72,12 @@ let alertAndConfirmationDialogReducer = Reducer<
7272
return .none
7373

7474
case .decrementButtonTapped:
75-
state.alert = .init(title: .init("Decremented!"))
75+
state.alert = AlertState(title: TextState("Decremented!"))
7676
state.count -= 1
7777
return .none
7878

7979
case .incrementButtonTapped:
80-
state.alert = .init(title: .init("Incremented!"))
80+
state.alert = AlertState(title: TextState("Incremented!"))
8181
state.count += 1
8282
return .none
8383
}
@@ -113,10 +113,10 @@ struct AlertAndConfirmationDialog_Previews: PreviewProvider {
113113
static var previews: some View {
114114
NavigationView {
115115
AlertAndConfirmationDialogView(
116-
store: .init(
117-
initialState: .init(),
116+
store: Store(
117+
initialState: AlertAndConfirmationDialogState(),
118118
reducer: alertAndConfirmationDialogReducer,
119-
environment: .init()
119+
environment: AlertAndConfirmationDialogEnvironment()
120120
)
121121
)
122122
}

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -78,18 +78,18 @@ let animationsReducer = Reducer<AnimationsState, AnimationsAction, AnimationsEnv
7878
)
7979

8080
case .resetButtonTapped:
81-
state.alert = .init(
82-
title: .init("Reset state?"),
81+
state.alert = AlertState(
82+
title: TextState("Reset state?"),
8383
primaryButton: .destructive(
84-
.init("Reset"),
84+
TextState("Reset"),
8585
action: .send(.resetConfirmationButtonTapped, animation: .default)
8686
),
87-
secondaryButton: .cancel(.init("Cancel"))
87+
secondaryButton: .cancel(TextState("Cancel"))
8888
)
8989
return .none
9090

9191
case .resetConfirmationButtonTapped:
92-
state = .init()
92+
state = AnimationsState()
9393
return .none
9494

9595
case let .setColor(color):
@@ -159,7 +159,7 @@ struct AnimationsView_Previews: PreviewProvider {
159159
NavigationView {
160160
AnimationsView(
161161
store: Store(
162-
initialState: .init(),
162+
initialState: AnimationsState(),
163163
reducer: animationsReducer,
164164
environment: AnimationsEnvironment(
165165
mainQueue: QueueScheduler.main
@@ -171,7 +171,7 @@ struct AnimationsView_Previews: PreviewProvider {
171171
NavigationView {
172172
AnimationsView(
173173
store: Store(
174-
initialState: .init(),
174+
initialState: AnimationsState(),
175175
reducer: animationsReducer,
176176
environment: AnimationsEnvironment(
177177
mainQueue: QueueScheduler.main

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
return .none
4343

4444
case .resetButtonTapped:
45-
state = .init()
45+
state = BindingFormState()
4646
return .none
4747
}
4848
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,9 @@
9090
NavigationView {
9191
FocusDemoView(
9292
store: Store(
93-
initialState: .init(),
93+
initialState: FocusDemoState(),
9494
reducer: focusDemoReducer,
95-
environment: .init()
95+
environment: FocusDemoEnvironment()
9696
)
9797
)
9898
}

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,12 @@ let sharedStateCounterReducer = Reducer<
105105
return .none
106106

107107
case .isPrimeButtonTapped:
108-
state.alert = .init(
109-
title: isPrime(state.count)
110-
? .init("👍 The number \(state.count) is prime!")
111-
: .init("👎 The number \(state.count) is not prime :(")
108+
state.alert = AlertState(
109+
title: TextState(
110+
isPrime(state.count)
111+
? "👍 The number \(state.count) is prime!"
112+
: "👎 The number \(state.count) is not prime :("
113+
)
112114
)
113115
return .none
114116
}

Examples/CaseStudies/SwiftUICaseStudies/02-Effects-Basics.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,9 @@ struct EffectsBasicsView: View {
114114
ProgressView()
115115
}
116116

117-
viewStore.numberFact.map(Text.init)
117+
if let numberFact = viewStore.numberFact {
118+
Text(numberFact)
119+
}
118120
}
119121
}
120122
}

Examples/CaseStudies/SwiftUICaseStudies/02-Effects-Refreshable.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,10 @@ let refreshableReducer = Reducer<
108108
struct Refreshable_Previews: PreviewProvider {
109109
static var previews: some View {
110110
RefreshableView(
111-
store: .init(
112-
initialState: .init(),
111+
store: Store(
112+
initialState: RefreshableState(),
113113
reducer: refreshableReducer,
114-
environment: .init(
114+
environment: RefreshableEnvironment(
115115
fact: .live,
116116
mainQueue: QueueScheduler.main
117117
)

Examples/CaseStudies/SwiftUICaseStudies/02-Effects-SystemEnvironment.swift

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ let multipleDependenciesReducer = Reducer<
5050
.delay(1, on: environment.mainQueue)
5151

5252
case .alertDelayReceived:
53-
state.alert = .init(title: .init("Here's an alert after a delay!"))
53+
state.alert = AlertState(title: TextState("Here's an alert after a delay!"))
5454
return .none
5555

5656
case .alertDismissed:
@@ -97,12 +97,16 @@ struct MultipleDependenciesView: View {
9797
) {
9898
HStack {
9999
Button("Date") { viewStore.send(.dateButtonTapped) }
100-
viewStore.dateString.map(Text.init)
100+
if let dateString = viewStore.dateString {
101+
Text(dateString)
102+
}
101103
}
102104

103105
HStack {
104106
Button("UUID") { viewStore.send(.uuidButtonTapped) }
105-
viewStore.uuidString.map(Text.init)
107+
if let uuidString = viewStore.uuidString {
108+
Text(uuidString)
109+
}
106110
}
107111

108112
Button("Delayed Alert") { viewStore.send(.alertButtonTapped) }
@@ -118,7 +122,9 @@ struct MultipleDependenciesView: View {
118122
) {
119123
HStack {
120124
Button("Fetch Number") { viewStore.send(.fetchNumberButtonTapped) }
121-
viewStore.fetchedNumberString.map(Text.init)
125+
if let fetchedNumberString = viewStore.fetchedNumberString {
126+
Text(fetchedNumberString)
127+
}
122128

123129
Spacer()
124130

@@ -139,7 +145,7 @@ struct MultipleDependenciesView_Previews: PreviewProvider {
139145
NavigationView {
140146
MultipleDependenciesView(
141147
store: Store(
142-
initialState: .init(),
148+
initialState: MultipleDependenciesState(),
143149
reducer: multipleDependenciesReducer,
144150
environment: .live(
145151
environment: MultipleDependenciesEnvironment(

Examples/CaseStudies/SwiftUICaseStudies/02-Effects-WebSocket.swift

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ let webSocketReducer = Reducer<WebSocketState, WebSocketAction, WebSocketEnviron
107107

108108
case let .sendResponse(error):
109109
if error != nil {
110-
state.alert = .init(title: .init("Could not send socket message. Try again."))
110+
state.alert = AlertState(title: TextState("Could not send socket message. Try again."))
111111
}
112112
return .none
113113

@@ -119,7 +119,9 @@ let webSocketReducer = Reducer<WebSocketState, WebSocketAction, WebSocketEnviron
119119
let .webSocket(.didCompleteWithError(error)):
120120
state.connectivityState = .disconnected
121121
if error != nil {
122-
state.alert = .init(title: .init("Disconnected from socket for some reason. Try again."))
122+
state.alert = AlertState(
123+
title: TextState("Disconnected from socket for some reason. Try again.")
124+
)
123125
}
124126
return .cancel(id: WebSocketId())
125127

@@ -265,7 +267,7 @@ extension WebSocketClient {
265267
case let .success(.some(message)):
266268
callback(.success(message))
267269
case .success(.none):
268-
callback(.failure(NSError.init(domain: "co.pointfree", code: 1)))
270+
callback(.failure(NSError(domain: "co.pointfree", code: 1)))
269271
case let .failure(error):
270272
callback(.failure(error as NSError))
271273
}
@@ -347,7 +349,7 @@ struct WebSocketView_Previews: PreviewProvider {
347349
NavigationView {
348350
WebSocketView(
349351
store: Store(
350-
initialState: .init(receivedMessages: ["Echo"]),
352+
initialState: WebSocketState(receivedMessages: ["Echo"]),
351353
reducer: webSocketReducer,
352354
environment: WebSocketEnvironment(
353355
mainQueue: QueueScheduler.main,

0 commit comments

Comments
 (0)