-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathDialogManager.swift
executable file
·105 lines (74 loc) · 3.43 KB
/
DialogManager.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
import Foundation
protocol Dialogable {
var message: String { get }
}
class DialogManager {
typealias RememberableDialogSubject = Dialogable & Rememberable
enum DialogType {
case alert(blocking: Bool)
case optionalUpdate(updateURL: URL)
case requiredUpdate(updateURL: URL)
}
func displayAlertDialog(_ alertConfig: RememberableDialogSubject, blocking: Bool) {
let dialog = createAlertController(.alert(blocking: blocking), message: alertConfig.message)
displayAlertController(dialog) { () -> Void in
guard !blocking else { return }
Memory.remember(alertConfig)
}
}
func displayRequiredUpdateDialog(_ updateConfig: Dialogable, updateURL: URL) {
let dialog = createAlertController(.requiredUpdate(updateURL: updateURL), message: updateConfig.message)
displayAlertController(dialog, completion: nil)
}
func displayOptionalUpdateDialog(_ updateConfig: RememberableDialogSubject, updateURL: URL) {
let dialog = createAlertController(.optionalUpdate(updateURL: updateURL), message: updateConfig.message)
displayAlertController(dialog) { () -> Void in
Memory.remember(updateConfig)
}
}
// MARK: Custom Alert Controllers
func createAlertController(_ type: DialogType, message: String) -> UIAlertController {
let alertController = UIAlertController(title: nil, message: message, preferredStyle: .alert)
switch type {
case .alert(let blocking):
if !blocking {
alertController.addAction(dismissActon())
}
case .optionalUpdate(let updateURL):
alertController.addAction(dismissActon())
alertController.addAction(updateAction(updateURL))
case .requiredUpdate(let updateURL):
alertController.addAction(updateAction(updateURL))
}
return alertController
}
func displayAlertController(_ alert: UIAlertController, completion: (() -> Void)?) {
DispatchQueue.main.async {
guard let topViewController = self.topViewController() else { return }
topViewController.present(alert, animated: true, completion: completion)
}
}
func topViewController() -> UIViewController? {
guard let rootViewController = UIApplication.shared.keyWindow?.rootViewController else { return nil }
var topViewController: UIViewController = rootViewController
while let presentedViewController = topViewController.presentedViewController {
topViewController = presentedViewController
}
return topViewController
}
// MARK: Custom Alert Actions
private func dismissActon() -> UIAlertAction {
let alertTitle = NSLocalizedString("Dismiss", comment: "Button title for dismissing the update AlertView")
return UIAlertAction(title: alertTitle, style: .default)
}
private func updateAction(_ updateURL: URL) -> UIAlertAction {
let alertTitle = NSLocalizedString("Update", comment: "Button title for accepting the update AlertView")
let updateHandler: (UIAlertAction) -> Void = { _ in
guard UIApplication.shared.canOpenURL(updateURL) else { return }
DispatchQueue.main.async {
UIApplication.shared.open(updateURL)
}
}
return UIAlertAction(title: alertTitle, style: .default, handler: updateHandler)
}
}