@@ -4,7 +4,7 @@ import SwiftUI
44/// generic is the type of actions that can be sent from tapping on a button in the alert.
55///
66/// This type can be used in your application's state in order to control the presentation or
7- /// dismissal of alerts. It is preferrable to use this API instead of the default SwiftUI API
7+ /// dismissal of alerts. It is preferable to use this API instead of the default SwiftUI API
88/// for alerts because SwiftUI uses 2-way bindings in order to control the showing and dismissal
99/// of alerts, and that does not play nicely with the Composable Architecture. The library requires
1010/// that all state mutations happen by sending an action so that a reducer can handle that logic,
@@ -92,14 +92,14 @@ import SwiftUI
9292///
9393public struct AlertState < Action> {
9494 public let id = UUID ( )
95- public var message : String ?
95+ public var message : LocalizedStringKey ?
9696 public var primaryButton : Button ?
9797 public var secondaryButton : Button ?
98- public var title : String
98+ public var title : LocalizedStringKey
9999
100100 public init (
101- title: String ,
102- message: String ? = nil ,
101+ title: LocalizedStringKey ,
102+ message: LocalizedStringKey ? = nil ,
103103 dismissButton: Button ? = nil
104104 ) {
105105 self . title = title
@@ -108,8 +108,8 @@ public struct AlertState<Action> {
108108 }
109109
110110 public init (
111- title: String ,
112- message: String ? = nil ,
111+ title: LocalizedStringKey ,
112+ message: LocalizedStringKey ? = nil ,
113113 primaryButton: Button ,
114114 secondaryButton: Button
115115 ) {
@@ -124,7 +124,7 @@ public struct AlertState<Action> {
124124 public var type : `Type`
125125
126126 public static func cancel(
127- _ label: String ,
127+ _ label: LocalizedStringKey ,
128128 send action: Action ? = nil
129129 ) -> Self {
130130 Self ( action: action, type: . cancel( label: label) )
@@ -137,23 +137,23 @@ public struct AlertState<Action> {
137137 }
138138
139139 public static func `default`(
140- _ label: String ,
140+ _ label: LocalizedStringKey ,
141141 send action: Action ? = nil
142142 ) -> Self {
143143 Self ( action: action, type: . default( label: label) )
144144 }
145145
146146 public static func destructive(
147- _ label: String ,
147+ _ label: LocalizedStringKey ,
148148 send action: Action ? = nil
149149 ) -> Self {
150150 Self ( action: action, type: . destructive( label: label) )
151151 }
152152
153- public enum `Type` : Hashable {
154- case cancel( label: String ? )
155- case `default`( label: String )
156- case destructive( label: String )
153+ public enum `Type` {
154+ case cancel( label: LocalizedStringKey ? )
155+ case `default`( label: LocalizedStringKey )
156+ case destructive( label: LocalizedStringKey )
157157 }
158158 }
159159}
@@ -194,24 +194,56 @@ extension AlertState: CustomDebugOutputConvertible {
194194
195195extension AlertState : Equatable where Action: Equatable {
196196 public static func == ( lhs: Self , rhs: Self ) -> Bool {
197- lhs. title == rhs. title
198- && lhs. message == rhs. message
197+ lhs. title. formatted ( ) == rhs. title. formatted ( )
198+ && lhs. message? . formatted ( ) == rhs. message? . formatted ( )
199199 && lhs. primaryButton == rhs. primaryButton
200200 && lhs. secondaryButton == rhs. secondaryButton
201201 }
202202}
203203extension AlertState : Hashable where Action: Hashable {
204204 public func hash( into hasher: inout Hasher ) {
205- hasher. combine ( self . title)
206- hasher. combine ( self . message)
205+ hasher. combine ( self . title. formatted ( ) )
206+ hasher. combine ( self . message? . formatted ( ) )
207207 hasher. combine ( self . primaryButton)
208208 hasher. combine ( self . secondaryButton)
209209 }
210210}
211211extension AlertState : Identifiable { }
212212
213- extension AlertState . Button : Equatable where Action: Equatable { }
214- extension AlertState . Button : Hashable where Action: Hashable { }
213+ extension AlertState . Button . `Type` : Equatable {
214+ public static func == ( lhs: Self , rhs: Self ) -> Bool {
215+ switch ( lhs, rhs) {
216+ case let ( . cancel( lhs) , . cancel( rhs) ) :
217+ return lhs? . formatted ( ) == rhs? . formatted ( )
218+ case let ( . default( lhs) , . default( rhs) ) , let ( . destructive( lhs) , . destructive( rhs) ) :
219+ return lhs. formatted ( ) == rhs. formatted ( )
220+ default :
221+ return false
222+ }
223+ }
224+ }
225+ extension AlertState . Button : Equatable where Action: Equatable {
226+ public static func == ( lhs: Self , rhs: Self ) -> Bool {
227+ return lhs. action == rhs. action && lhs. type == rhs. type
228+ }
229+ }
230+
231+ extension AlertState . Button . `Type` : Hashable {
232+ public func hash( into hasher: inout Hasher ) {
233+ switch self {
234+ case let . cancel( label) :
235+ hasher. combine ( label? . formatted ( ) )
236+ case let . default( label) , let . destructive( label) :
237+ hasher. combine ( label. formatted ( ) )
238+ }
239+ }
240+ }
241+ extension AlertState . Button : Hashable where Action: Hashable {
242+ public func hash( into hasher: inout Hasher ) {
243+ hasher. combine ( self . action)
244+ hasher. combine ( self . type)
245+ }
246+ }
215247
216248extension AlertState . Button {
217249 func toSwiftUI( send: @escaping ( Action ) -> Void ) -> SwiftUI . Alert . Button {
0 commit comments