Skip to content

Commit 4449bc2

Browse files
authored
Add TextState (for AlertState, ActionSheetState, etc.) (#359)
* Use SwiftUI.Text with {Alert,ActionSheet}State Fix #293, #318. * Use public interface * Availability * TextState * Note * Simplify * Update LocalizedStringTests.swift * Fix warnings * Fix docs
1 parent 6773c2e commit 4449bc2

27 files changed

+509
-173
lines changed

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,12 @@ let alertAndSheetReducer = Reducer<
4444
switch action {
4545
case .actionSheetButtonTapped:
4646
state.actionSheet = .init(
47-
title: "Action sheet",
48-
message: "This is an action sheet.",
47+
title: .init("Action sheet"),
48+
message: .init("This is an action sheet."),
4949
buttons: [
5050
.cancel(),
51-
.default("Increment", send: .incrementButtonTapped),
52-
.default("Decrement", send: .decrementButtonTapped),
51+
.default(.init("Increment"), send: .incrementButtonTapped),
52+
.default(.init("Decrement"), send: .decrementButtonTapped),
5353
]
5454
)
5555
return .none
@@ -63,10 +63,10 @@ let alertAndSheetReducer = Reducer<
6363

6464
case .alertButtonTapped:
6565
state.alert = .init(
66-
title: "Alert!",
67-
message: "This is an alert",
66+
title: .init("Alert!"),
67+
message: .init("This is an alert"),
6868
primaryButton: .cancel(),
69-
secondaryButton: .default("Increment", send: .incrementButtonTapped)
69+
secondaryButton: .default(.init("Increment"), send: .incrementButtonTapped)
7070
)
7171
return .none
7272

@@ -78,12 +78,12 @@ let alertAndSheetReducer = Reducer<
7878
return .none
7979

8080
case .decrementButtonTapped:
81-
state.alert = .init(title: "Decremented!")
81+
state.alert = .init(title: .init("Decremented!"))
8282
state.count -= 1
8383
return .none
8484

8585
case .incrementButtonTapped:
86-
state.alert = .init(title: "Incremented!")
86+
state.alert = .init(title: .init("Incremented!"))
8787
state.count += 1
8888
return .none
8989
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,8 @@ let sharedStateCounterReducer = Reducer<
107107
case .isPrimeButtonTapped:
108108
state.alert = .init(
109109
title: isPrime(state.count)
110-
? "👍 The number \(state.count) is prime!"
111-
: "👎 The number \(state.count) is not prime :("
110+
? .init("👍 The number \(state.count) is prime!")
111+
: .init("👎 The number \(state.count) is not prime :(")
112112
)
113113
return .none
114114
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ let webSocketReducer = Reducer<WebSocketState, WebSocketAction, WebSocketEnviron
109109

110110
case let .sendResponse(error):
111111
if error != nil {
112-
state.alert = .init(title: "Could not send socket message. Try again.")
112+
state.alert = .init(title: .init("Could not send socket message. Try again."))
113113
}
114114
return .none
115115

@@ -121,7 +121,7 @@ let webSocketReducer = Reducer<WebSocketState, WebSocketAction, WebSocketEnviron
121121
let .webSocket(.didCompleteWithError(error)):
122122
state.connectivityState = .disconnected
123123
if error != nil {
124-
state.alert = .init(title: "Disconnected from socket for some reason. Try again.")
124+
state.alert = .init(title: .init("Disconnected from socket for some reason. Try again."))
125125
}
126126
return .cancel(id: WebSocketId())
127127

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ let multipleDependenciesReducer = Reducer<
5050
.eraseToEffect()
5151

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

5656
case .alertDismissed:

Examples/CaseStudies/SwiftUICaseStudies/04-HigherOrderReducers-ResuableOfflineDownloads/DownloadComponent.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -118,19 +118,19 @@ extension Reducer {
118118
}
119119

120120
private let deleteAlert = AlertState(
121-
title: "Do you want to delete this map from your offline storage?",
122-
primaryButton: .destructive("Delete", send: .deleteButtonTapped),
121+
title: .init("Do you want to delete this map from your offline storage?"),
122+
primaryButton: .destructive(.init("Delete"), send: .deleteButtonTapped),
123123
secondaryButton: nevermindButton
124124
)
125125

126126
private let cancelAlert = AlertState(
127-
title: "Do you want to cancel downloading this map?",
128-
primaryButton: .destructive("Cancel", send: .cancelButtonTapped),
127+
title: .init("Do you want to cancel downloading this map?"),
128+
primaryButton: .destructive(.init("Cancel"), send: .cancelButtonTapped),
129129
secondaryButton: nevermindButton
130130
)
131131

132132
let nevermindButton = AlertState<DownloadComponentAction.AlertAction>.Button
133-
.default("Nevermind", send: .nevermindButtonTapped)
133+
.default(.init("Nevermind"), send: .nevermindButtonTapped)
134134

135135
struct DownloadComponent<ID: Equatable>: View {
136136
let store: Store<DownloadComponentState<ID>, DownloadComponentAction>

Examples/CaseStudies/SwiftUICaseStudies/04-HigherOrderReducers-ReusableFavoriting.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ extension Reducer {
7878
.cancellable(id: FavoriteCancelId(id: state.id), cancelInFlight: true)
7979

8080
case let .response(.failure(error)):
81-
state.alert = .init(title: .init(error.localizedDescription))
81+
state.alert = .init(title: TextState(error.localizedDescription))
8282
return .none
8383

8484
case let .response(.success(isFavorite)):

Examples/CaseStudies/SwiftUICaseStudiesTests/01-GettingStarted-AlertsAndActionSheetsTests.swift

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ class AlertsAndActionSheetsTests: XCTestCase {
1616
store.assert(
1717
.send(.alertButtonTapped) {
1818
$0.alert = .init(
19-
title: "Alert!",
20-
message: "This is an alert",
19+
title: .init("Alert!"),
20+
message: .init("This is an alert"),
2121
primaryButton: .cancel(),
22-
secondaryButton: .default("Increment", send: .incrementButtonTapped)
22+
secondaryButton: .default(.init("Increment"), send: .incrementButtonTapped)
2323
)
2424
},
2525
.send(.incrementButtonTapped) {
26-
$0.alert = .init(title: "Incremented!")
26+
$0.alert = .init(title: .init("Incremented!"))
2727
$0.count = 1
2828
},
2929
.send(.alertDismissed) {
@@ -42,17 +42,17 @@ class AlertsAndActionSheetsTests: XCTestCase {
4242
store.assert(
4343
.send(.actionSheetButtonTapped) {
4444
$0.actionSheet = .init(
45-
title: "Action sheet",
46-
message: "This is an action sheet.",
45+
title: .init("Action sheet"),
46+
message: .init("This is an action sheet."),
4747
buttons: [
4848
.cancel(),
49-
.default("Increment", send: .incrementButtonTapped),
50-
.default("Decrement", send: .decrementButtonTapped),
49+
.default(.init("Increment"), send: .incrementButtonTapped),
50+
.default(.init("Decrement"), send: .decrementButtonTapped),
5151
]
5252
)
5353
},
5454
.send(.incrementButtonTapped) {
55-
$0.alert = .init(title: "Incremented!")
55+
$0.alert = .init(title: .init("Incremented!"))
5656
$0.count = 1
5757
},
5858
.send(.actionSheetDismissed) {

Examples/CaseStudies/SwiftUICaseStudiesTests/01-GettingStarted-SharedStateTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ class SharedStateTests: XCTestCase {
8080
store.assert(
8181
.send(.isPrimeButtonTapped) {
8282
$0.alert = .init(
83-
title: "👍 The number \($0.count) is prime!"
83+
title: .init("👍 The number \($0.count) is prime!")
8484
)
8585
},
8686
.send(.alertDismissed) {
@@ -100,7 +100,7 @@ class SharedStateTests: XCTestCase {
100100
store.assert(
101101
.send(.isPrimeButtonTapped) {
102102
$0.alert = .init(
103-
title: "👎 The number \($0.count) is not prime :("
103+
title: .init("👎 The number \($0.count) is not prime :(")
104104
)
105105
},
106106
.send(.alertDismissed) {

Examples/CaseStudies/SwiftUICaseStudiesTests/02-Effects-WebSocketTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ class WebSocketTests: XCTestCase {
9696
$0.messageToSend = ""
9797
},
9898
.receive(.sendResponse(NSError(domain: "", code: 1))) {
99-
$0.alert = .init(title: "Could not send socket message. Try again.")
99+
$0.alert = .init(title: .init("Could not send socket message. Try again."))
100100
},
101101

102102
// Disconnect from the socket

Examples/CaseStudies/SwiftUICaseStudiesTests/04-HigherOrderReducers-ReusableFavoritingTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ class ReusableComponentsFavoritingTests: XCTestCase {
6666
.episode(index: 2, action: .favorite(.response(.failure(FavoriteError(error: error)))))
6767
) {
6868
$0.episodes[2].alert = .init(
69-
title: "The operation couldn’t be completed. (co.pointfree error -1.)"
69+
title: .init("The operation couldn’t be completed. (co.pointfree error -1.)")
7070
)
7171
},
7272

0 commit comments

Comments
 (0)