/
QueuedAlertPresenter.swift
166 lines (138 loc) · 6.8 KB
/
QueuedAlertPresenter.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
//
// QueuedAlertPresenter.swift
// RandomReminders
//
// Created by Antonio Nunes on 29/02/16.
// Copyright © 2016 SintraWorks. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is furnished
// to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
import UIKit
public typealias AlertActionHandler = () -> ()
/**
An AlertAction instance encapsulates all the required information to create an alert action.
*/
public struct AlertAction {
var title: String
var style: UIAlertAction.Style
var handler: AlertActionHandler?
var enabled: Bool
var isPreferredAction: Bool
/**
Initializes an AlertAction instance with the passed in values. All parameters have default values, so pass only those you need.
- title: Optional title for the action. Defaults to an empty string.
- style: Alert style (UIAlertActionStyle). Defaults to .Default
- isPreferredAction: Whether the action is the preferred action of the alert. Defaults to false.
- handler: Optional closure containing code to execute when the action is clicked.
*/
public init(title: String = "", style: UIAlertAction.Style = .default, enabled: Bool = true, isPreferredAction: Bool = false, handler: AlertActionHandler? = nil) {
self.title = title
self.style = style
self.enabled = enabled
self.isPreferredAction = isPreferredAction
self.handler = handler
}
}
/**
An AlertInfo instance encapsulates all the required information to create an Alert, including its actions.
*/
public struct AlertInfo {
var title: String?
var message: String?
var style: UIAlertController.Style
var actions: [AlertAction]?
/**
Initializes an AlertInfo instance with the passed in values. All parameteres have default values, so pass only those you need.
- title: Optional title for the alert. Defaults to an empty string.
- message: Optional message body for the alert. Defaults to an empty string
- style: Alert style (UIAlertControllerStyle). Defaults to .Alert
- actions: Optional array of AlertActions. Defaults to nil.
*/
public init(title: String? = "", message: String? = "", style: UIAlertController.Style = .alert, actions: [AlertAction]? = nil) {
self.title = title
self.message = message
self.style = style
self.actions = actions
}
}
/**
A QueuedAlertPresenter serializes the presentation of alerts. Use it when your code needs to present alerts in rapid succession, where the user may not be able to keep up with the alerts.
Using QueuedAlertPresenter instead of UIAlertController directly, ensures your user gets to see all the alerts your code creates.
To present an alert create an instance of AlertInfo then add it to the sharedAlertPresenter with addAlert(alertInfo).
AlertInfo holds the information for an individual alert you want to present.
*/
open class QueuedAlertPresenter {
public static let sharedAlertPresenter = QueuedAlertPresenter()
fileprivate var queuedAlerts = [AlertInfo]()
fileprivate var presentedAlert: AlertInfo?
/**
Adds an alert to the alert queue, and triggers presentation of the queued alerts.
*/
open func addAlert(_ alertInfo: AlertInfo) {
objc_sync_enter(self)
defer { objc_sync_exit(self) }
self.queuedAlerts.append(alertInfo)
self.presentAlerts()
}
/**
Presents queued alerts in succession.
- Returns: true if an alert was presented, false if no alert was presented.
*/
@discardableResult fileprivate func presentAlerts() -> Bool {
if self.presentedAlert == nil,
let alertToPresent = self.queuedAlerts.first {
self.presentedAlert = alertToPresent
self.presentAlert(alertToPresent)
return true
}
return false
}
fileprivate func presentAlert(_ alertInfo :AlertInfo) {
let alertController = UIAlertController(title: alertInfo.title, message:alertInfo.message, preferredStyle: alertInfo.style)
if let actions = alertInfo.actions {
for infoAction in actions {
let alertAction = UIAlertAction(title: infoAction.title, style: infoAction.style) { action -> Void in
infoAction.handler?()
self.presentedAlert = nil
self.queuedAlerts.removeFirst()
self.presentAlerts()
}
alertAction.isEnabled = infoAction.enabled
alertController.addAction(alertAction)
if #available(iOS 9.0, *) {
if infoAction.isPreferredAction {
alertController.preferredAction = alertAction
}
}
}
}
self.validViewControllerForPresentation.presentViewControllerOnMainThread(alertController, animated: true, completion: nil)
}
// Ensure we present on a view controller that is neither being dismissed, nor presenting another view controller.
fileprivate var validViewControllerForPresentation: UIViewController {
guard
let keyWindow = UIApplication.shared.windows.first(where: { $0.isKeyWindow }),
let rootViewController = keyWindow.rootViewController else { fatalError("There is no root view controller on the key window") }
var validController = UIViewController.topViewControllerForViewController(rootViewController)
while validController.isBeingDismissed {
guard let ancestor = validController.presentingViewController else { fatalError("Exhausted view controller hierarchy, while looking for a controller that is not being dismissed") }
validController = ancestor
}
return validController
}
}