Skip to content

Commit bf0fb9d

Browse files
Make ButtonState actions more flexible (pointfreeco#71)
* Allow alert state actions to be mapped Co-authored-by: Skyler Smith <[email protected]> * wip * wip * reorganize deprecations * fix Co-authored-by: Skyler Smith <[email protected]>
1 parent a2154e6 commit bf0fb9d

File tree

15 files changed

+733
-427
lines changed

15 files changed

+733
-427
lines changed

Examples/CaseStudies/09-Routing.swift

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,21 +91,25 @@ struct Routing: View {
9191
.navigationTitle("Routing")
9292
.alert(unwrapping: self.$destination, case: /Destination.alert) { action in
9393
switch action {
94-
case .randomize:
94+
case .randomize?:
9595
self.count = .random(in: 0...1_000)
96-
case .reset:
96+
case .reset?:
9797
self.count = 0
98+
case nil:
99+
break
98100
}
99101
}
100102
.confirmationDialog(
101103
unwrapping: self.$destination,
102104
case: /Destination.confirmationDialog
103105
) { action in
104106
switch action {
105-
case .decrement:
107+
case .decrement?:
106108
self.count -= 1
107-
case .increment:
109+
case .increment?:
108110
self.count += 1
111+
case nil:
112+
break
109113
}
110114
}
111115
.sheet(unwrapping: self.$destination, case: /Destination.sheet) { $count in

Examples/Inventory/Item.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ struct Item: Equatable, Identifiable {
2323
var green: CGFloat = 0
2424
var blue: CGFloat = 0
2525

26-
static var defaults: [Self] = [
26+
static let defaults: [Self] = [
2727
.red,
2828
.green,
2929
.blue,

Examples/Inventory/ItemRow.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,12 @@ class ItemRowModel: Identifiable, ObservableObject {
3939
)
4040
}
4141

42-
func alertButtonTapped(_ action: AlertAction) {
42+
func alertButtonTapped(_ action: AlertAction?) {
4343
switch action {
44-
case .deleteConfirmation:
44+
case .deleteConfirmation?:
4545
self.onDelete()
46+
case nil:
47+
break
4648
}
4749
}
4850

Examples/Standups/Standups/RecordMeeting.swift

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,14 @@ class RecordMeetingModel: ObservableObject {
6868
self.destination = .alert(.endMeeting(isDiscardable: true))
6969
}
7070

71-
func alertButtonTapped(_ action: AlertAction) async {
71+
func alertButtonTapped(_ action: AlertAction?) async {
7272
switch action {
73-
case .confirmSave:
73+
case .confirmSave?:
7474
await self.finishMeeting()
75-
76-
case .confirmDiscard:
75+
case .confirmDiscard?:
7776
self.isDismissed = true
77+
case nil:
78+
break
7879
}
7980
}
8081

Examples/Standups/Standups/StandupDetail.swift

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,21 +54,24 @@ class StandupDetailModel: ObservableObject {
5454
self.destination = .alert(.deleteStandup)
5555
}
5656

57-
func alertButtonTapped(_ action: AlertAction) async {
57+
func alertButtonTapped(_ action: AlertAction?) async {
5858
switch action {
59-
case .confirmDeletion:
59+
case .confirmDeletion?:
6060
self.onConfirmDeletion()
6161
self.isDismissed = true
6262

63-
case .continueWithoutRecording:
63+
case .continueWithoutRecording?:
6464
self.destination = .record(
6565
withDependencies(from: self) {
6666
RecordMeetingModel(standup: self.standup)
6767
}
6868
)
6969

70-
case .openSettings:
70+
case .openSettings?:
7171
await self.openSettings()
72+
73+
case nil:
74+
break
7275
}
7376
}
7477

Examples/Standups/Standups/StandupsList.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,16 +109,18 @@ final class StandupsListModel: ObservableObject {
109109
}
110110
}
111111

112-
func alertButtonTapped(_ action: AlertAction) {
112+
func alertButtonTapped(_ action: AlertAction?) {
113113
switch action {
114-
case .confirmLoadMockData:
114+
case .confirmLoadMockData?:
115115
withAnimation {
116116
self.standups = [
117117
.mock,
118118
.designMock,
119119
.engineeringMock,
120120
]
121121
}
122+
case nil:
123+
break
122124
}
123125
}
124126
}

Sources/SwiftUINavigation/Alert.swift

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ extension View {
118118
@available(iOS 15, macOS 12, tvOS 15, watchOS 8, *)
119119
public func alert<Value>(
120120
unwrapping value: Binding<AlertState<Value>?>,
121-
action handler: @escaping (Value) -> Void = { (_: Void) in }
121+
action handler: @escaping (Value?) -> Void = { (_: Never?) in }
122122
) -> some View {
123123
self.alert(
124124
(value.wrappedValue?.title).map(Text.init) ?? Text(""),
@@ -151,7 +151,7 @@ extension View {
151151
@available(iOS 15, macOS 12, tvOS 15, watchOS 8, *)
152152
public func alert<Value>(
153153
unwrapping value: Binding<AlertState<Value>?>,
154-
action handler: @escaping (Value) async -> Void = { (_: Void) async in }
154+
action handler: @escaping (Value?) async -> Void = { (_: Never?) async in }
155155
) -> some View {
156156
self.alert(
157157
(value.wrappedValue?.title).map(Text.init) ?? Text(""),
@@ -187,7 +187,7 @@ extension View {
187187
public func alert<Enum, Value>(
188188
unwrapping `enum`: Binding<Enum?>,
189189
case casePath: CasePath<Enum, AlertState<Value>>,
190-
action handler: @escaping (Value) -> Void = { (_: Void) in }
190+
action handler: @escaping (Value?) -> Void = { (_: Never?) in }
191191
) -> some View {
192192
self.alert(unwrapping: `enum`.case(casePath), action: handler)
193193
}
@@ -216,15 +216,15 @@ extension View {
216216
public func alert<Enum, Value>(
217217
unwrapping `enum`: Binding<Enum?>,
218218
case casePath: CasePath<Enum, AlertState<Value>>,
219-
action handler: @escaping (Value) async -> Void = { (_: Void) async in }
219+
action handler: @escaping (Value?) async -> Void = { (_: Never?) async in }
220220
) -> some View {
221221
self.alert(unwrapping: `enum`.case(casePath), action: handler)
222222
}
223223
#else
224224
@available(iOS 15, macOS 12, tvOS 15, watchOS 8, *)
225225
public func alert<Value>(
226226
unwrapping value: Binding<AlertState<Value>?>,
227-
action handler: @escaping (Value) -> Void
227+
action handler: @escaping (Value?) -> Void
228228
) -> some View {
229229
self.alert(
230230
(value.wrappedValue?.title).map(Text.init) ?? Text(""),
@@ -242,7 +242,7 @@ extension View {
242242
@available(iOS 15, macOS 12, tvOS 15, watchOS 8, *)
243243
public func alert<Value>(
244244
unwrapping value: Binding<AlertState<Value>?>,
245-
action handler: @escaping (Value) async -> Void
245+
action handler: @escaping (Value?) async -> Void
246246
) -> some View {
247247
self.alert(
248248
(value.wrappedValue?.title).map(Text.init) ?? Text(""),
@@ -267,7 +267,7 @@ extension View {
267267
presenting: value.wrappedValue,
268268
actions: {
269269
ForEach($0.buttons) {
270-
Button($0, action: { (_: Never) in fatalError() })
270+
Button($0) { _ in }
271271
}
272272
},
273273
message: { $0.message.map { Text($0) } }
@@ -278,7 +278,7 @@ extension View {
278278
public func alert<Enum, Value>(
279279
unwrapping `enum`: Binding<Enum?>,
280280
case casePath: CasePath<Enum, AlertState<Value>>,
281-
action handler: @escaping (Value) -> Void
281+
action handler: @escaping (Value?) -> Void
282282
) -> some View {
283283
self.alert(unwrapping: `enum`.case(casePath), action: handler)
284284
}
@@ -287,7 +287,7 @@ extension View {
287287
public func alert<Enum, Value>(
288288
unwrapping `enum`: Binding<Enum?>,
289289
case casePath: CasePath<Enum, AlertState<Value>>,
290-
action handler: @escaping (Value) async -> Void
290+
action handler: @escaping (Value?) async -> Void
291291
) -> some View {
292292
self.alert(unwrapping: `enum`.case(casePath), action: handler)
293293
}
@@ -297,7 +297,7 @@ extension View {
297297
unwrapping `enum`: Binding<Enum?>,
298298
case casePath: CasePath<Enum, AlertState<Never>>
299299
) -> some View {
300-
self.alert(unwrapping: `enum`.case(casePath), action: { (_: Never) in fatalError() })
300+
self.alert(unwrapping: `enum`.case(casePath)) { (_: Never?) in }
301301
}
302302
#endif
303303

Sources/SwiftUINavigation/ConfirmationDialog.swift

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ extension View {
126126
@available(iOS 15, macOS 12, tvOS 15, watchOS 8, *)
127127
public func confirmationDialog<Value>(
128128
unwrapping value: Binding<ConfirmationDialogState<Value>?>,
129-
action handler: @escaping (Value) -> Void = { (_: Void) in }
129+
action handler: @escaping (Value?) -> Void = { (_: Never?) in }
130130
) -> some View {
131131
self.confirmationDialog(
132132
value.wrappedValue.flatMap { Text($0.title) } ?? Text(""),
@@ -160,7 +160,7 @@ extension View {
160160
@available(iOS 15, macOS 12, tvOS 15, watchOS 8, *)
161161
public func confirmationDialog<Value>(
162162
unwrapping value: Binding<ConfirmationDialogState<Value>?>,
163-
action handler: @escaping (Value) async -> Void = { (_: Void) async in }
163+
action handler: @escaping (Value?) async -> Void = { (_: Never?) async in }
164164
) -> some View {
165165
self.confirmationDialog(
166166
value.wrappedValue.flatMap { Text($0.title) } ?? Text(""),
@@ -195,7 +195,7 @@ extension View {
195195
public func confirmationDialog<Enum, Value>(
196196
unwrapping `enum`: Binding<Enum?>,
197197
case casePath: CasePath<Enum, ConfirmationDialogState<Value>>,
198-
action handler: @escaping (Value) -> Void = { (_: Void) in }
198+
action handler: @escaping (Value?) -> Void = { (_: Never?) in }
199199
) -> some View {
200200
self.confirmationDialog(
201201
unwrapping: `enum`.case(casePath),
@@ -225,7 +225,7 @@ extension View {
225225
public func confirmationDialog<Enum, Value>(
226226
unwrapping `enum`: Binding<Enum?>,
227227
case casePath: CasePath<Enum, ConfirmationDialogState<Value>>,
228-
action handler: @escaping (Value) async -> Void = { (_: Void) async in }
228+
action handler: @escaping (Value?) async -> Void = { (_: Never?) async in }
229229
) -> some View {
230230
self.confirmationDialog(
231231
unwrapping: `enum`.case(casePath),
@@ -236,7 +236,7 @@ extension View {
236236
@available(iOS 15, macOS 12, tvOS 15, watchOS 8, *)
237237
public func confirmationDialog<Value>(
238238
unwrapping value: Binding<ConfirmationDialogState<Value>?>,
239-
action handler: @escaping (Value) -> Void
239+
action handler: @escaping (Value?) -> Void
240240
) -> some View {
241241
self.confirmationDialog(
242242
value.wrappedValue.flatMap { Text($0.title) } ?? Text(""),
@@ -255,7 +255,7 @@ extension View {
255255
@available(iOS 15, macOS 12, tvOS 15, watchOS 8, *)
256256
public func confirmationDialog<Value>(
257257
unwrapping value: Binding<ConfirmationDialogState<Value>?>,
258-
action handler: @escaping (Value) async -> Void
258+
action handler: @escaping (Value?) async -> Void
259259
) -> some View {
260260
self.confirmationDialog(
261261
value.wrappedValue.flatMap { Text($0.title) } ?? Text(""),
@@ -275,17 +275,14 @@ extension View {
275275
public func confirmationDialog(
276276
unwrapping value: Binding<ConfirmationDialogState<Never>?>
277277
) -> some View {
278-
self.confirmationDialog(
279-
unwrapping: value,
280-
action: { (_: Never) in fatalError() }
281-
)
278+
self.confirmationDialog(unwrapping: value) { _ in }
282279
}
283280

284281
@available(iOS 15, macOS 12, tvOS 15, watchOS 8, *)
285282
public func confirmationDialog<Enum, Value>(
286283
unwrapping `enum`: Binding<Enum?>,
287284
case casePath: CasePath<Enum, ConfirmationDialogState<Value>>,
288-
action handler: @escaping (Value) -> Void
285+
action handler: @escaping (Value?) -> Void
289286
) -> some View {
290287
self.confirmationDialog(
291288
unwrapping: `enum`.case(casePath),
@@ -297,7 +294,7 @@ extension View {
297294
public func confirmationDialog<Enum, Value>(
298295
unwrapping `enum`: Binding<Enum?>,
299296
case casePath: CasePath<Enum, ConfirmationDialogState<Value>>,
300-
action handler: @escaping (Value) async -> Void
297+
action handler: @escaping (Value?) async -> Void
301298
) -> some View {
302299
self.confirmationDialog(
303300
unwrapping: `enum`.case(casePath),
@@ -310,10 +307,7 @@ extension View {
310307
unwrapping `enum`: Binding<Enum?>,
311308
case casePath: CasePath<Enum, ConfirmationDialogState<Never>>
312309
) -> some View {
313-
self.confirmationDialog(
314-
unwrapping: `enum`.case(casePath),
315-
action: { (_: Never) in fatalError() }
316-
)
310+
self.confirmationDialog(unwrapping: `enum`.case(casePath)) { _ in }
317311
}
318312
#endif
319313

0 commit comments

Comments
 (0)