@@ -14,7 +14,7 @@ public class InAppModalAlertIssuer: AlertIssuer {
1414 private weak var alertPresenter : AlertPresenter ?
1515 private weak var alertManagerResponder : AlertManagerResponder ?
1616
17- private var alertsShowing : [ Alert . Identifier : ( UIAlertController , Alert ) ] = [ : ]
17+ private var alertsPresented : [ Alert . Identifier : ( UIAlertController , Alert ) ] = [ : ]
1818 private var alertsPending : [ Alert . Identifier : ( Timer , Alert ) ] = [ : ]
1919
2020 typealias ActionFactoryFunction = ( String ? , UIAlertAction . Style , ( ( UIAlertAction ) -> Void ) ? ) -> UIAlertAction
@@ -29,7 +29,8 @@ public class InAppModalAlertIssuer: AlertIssuer {
2929 alertManagerResponder: AlertManagerResponder ,
3030 soundPlayer: AlertSoundPlayer = DeviceAVSoundPlayer ( ) ,
3131 newActionFunc: @escaping ActionFactoryFunction = UIAlertAction . init,
32- newTimerFunc: TimerFactoryFunction ? = nil ) {
32+ newTimerFunc: TimerFactoryFunction ? = nil )
33+ {
3334 self . alertPresenter = alertPresenter
3435 self . alertManagerResponder = alertManagerResponder
3536 self . soundPlayer = soundPlayer
@@ -38,7 +39,7 @@ public class InAppModalAlertIssuer: AlertIssuer {
3839 return Timer . scheduledTimer ( withTimeInterval: timeInterval, repeats: repeats) { _ in block ? ( ) }
3940 }
4041 }
41-
42+
4243 public func issueAlert( _ alert: Alert ) {
4344 switch alert. trigger {
4445 case . immediate:
@@ -52,21 +53,30 @@ public class InAppModalAlertIssuer: AlertIssuer {
5253
5354 public func retractAlert( identifier: Alert . Identifier ) {
5455 DispatchQueue . main. async {
55- self . alertsPending [ identifier] ? . 0 . invalidate ( )
56- self . clearPendingAlert ( identifier: identifier)
57- self . removeDeliveredAlert ( identifier: identifier, completion: nil )
56+ self . removePendingAlert ( identifier: identifier)
57+ self . removePresentedAlert ( identifier: identifier)
5858 }
5959 }
60-
61- func removeDeliveredAlert( identifier: Alert . Identifier , completion: ( ( ) -> Void ) ? ) {
62- self . alertsShowing [ identifier] ? . 0 . dismiss ( animated: true , completion: completion)
63- self . clearDeliveredAlert ( identifier: identifier)
60+
61+ func removePresentedAlert( identifier: Alert . Identifier , completion: ( ( ) -> Void ) ? = nil ) {
62+ guard let alertPresented = alertsPresented [ identifier] else {
63+ completion ? ( )
64+ return
65+ }
66+ alertPresenter? . dismissAlert ( alertPresented. 0 , animated: true , completion: completion)
67+ clearPresentedAlert ( identifier: identifier)
68+ }
69+
70+ func removePendingAlert( identifier: Alert . Identifier ) {
71+ guard let alertPending = alertsPending [ identifier] else { return }
72+ alertPending. 0 . invalidate ( )
73+ clearPendingAlert ( identifier: identifier)
6474 }
6575}
6676
6777/// Private functions
6878extension InAppModalAlertIssuer {
69-
79+
7080 private func schedule( alert: Alert , interval: TimeInterval , repeats: Bool ) {
7181 guard alert. foregroundContent != nil else {
7282 return
@@ -90,59 +100,60 @@ extension InAppModalAlertIssuer {
90100 return
91101 }
92102 DispatchQueue . main. async {
93- if self . isAlertShowing ( identifier: alert. identifier) {
103+ if self . isAlertPresented ( identifier: alert. identifier) {
94104 return
95105 }
96106 let alertController = self . constructAlert ( title: content. title,
97107 message: content. body,
98108 action: content. acknowledgeActionButtonLabel,
99109 isCritical: content. isCritical) { [ weak self] in
100- self ? . clearDeliveredAlert ( identifier: alert. identifier)
101- self ? . alertManagerResponder? . acknowledgeAlert ( identifier: alert. identifier)
110+ // the completion is called after the alert is acknowledged
111+ self ? . clearPresentedAlert ( identifier: alert. identifier)
112+ self ? . alertManagerResponder? . acknowledgeAlert ( identifier: alert. identifier)
102113 }
103114 self . alertPresenter? . present ( alertController, animated: true ) { [ weak self] in
104- // the completion is called after the alert is displayed
115+ // the completion is called after the alert is presented
105116 self ? . playSound ( for: alert)
106- self ? . addDeliveredAlert ( alert: alert, controller: alertController)
117+ self ? . addPresentedAlert ( alert: alert, controller: alertController)
107118 }
108119 }
109120 }
110121
111122 private func addPendingAlert( alert: Alert , timer: Timer ) {
112123 dispatchPrecondition ( condition: . onQueue( . main) )
113- self . alertsPending [ alert. identifier] = ( timer, alert)
124+ alertsPending [ alert. identifier] = ( timer, alert)
114125 }
115126
116- private func addDeliveredAlert ( alert: Alert , controller: UIAlertController ) {
127+ private func addPresentedAlert ( alert: Alert , controller: UIAlertController ) {
117128 dispatchPrecondition ( condition: . onQueue( . main) )
118- self . alertsShowing [ alert. identifier] = ( controller, alert)
129+ alertsPresented [ alert. identifier] = ( controller, alert)
119130 }
120131
121132 private func clearPendingAlert( identifier: Alert . Identifier ) {
122133 dispatchPrecondition ( condition: . onQueue( . main) )
123134 alertsPending [ identifier] = nil
124135 }
125136
126- private func clearDeliveredAlert ( identifier: Alert . Identifier ) {
137+ private func clearPresentedAlert ( identifier: Alert . Identifier ) {
127138 dispatchPrecondition ( condition: . onQueue( . main) )
128- alertsShowing [ identifier] = nil
139+ alertsPresented [ identifier] = nil
129140 }
130-
141+
131142 private func isAlertPending( identifier: Alert . Identifier ) -> Bool {
132143 dispatchPrecondition ( condition: . onQueue( . main) )
133144 return alertsPending. index ( forKey: identifier) != nil
134145 }
135146
136- private func isAlertShowing ( identifier: Alert . Identifier ) -> Bool {
147+ private func isAlertPresented ( identifier: Alert . Identifier ) -> Bool {
137148 dispatchPrecondition ( condition: . onQueue( . main) )
138- return alertsShowing . index ( forKey: identifier) != nil
149+ return alertsPresented . index ( forKey: identifier) != nil
139150 }
140151
141- private func constructAlert( title: String , message: String , action: String , isCritical: Bool , completion : @escaping ( ) -> Void ) -> UIAlertController {
152+ private func constructAlert( title: String , message: String , action: String , isCritical: Bool , acknowledgeCompletion : @escaping ( ) -> Void ) -> UIAlertController {
142153 dispatchPrecondition ( condition: . onQueue( . main) )
143154 // For now, this is a simple alert with an "OK" button
144155 let alertController = UIAlertController ( title: title, message: message, preferredStyle: . alert)
145- alertController. addAction ( newActionFunc ( action, . default, { _ in completion ( ) } ) )
156+ alertController. addAction ( newActionFunc ( action, . default, { _ in acknowledgeCompletion ( ) } ) )
146157 return alertController
147158 }
148159
0 commit comments