-
Notifications
You must be signed in to change notification settings - Fork 5
/
AlertController.swift
129 lines (98 loc) · 4.59 KB
/
AlertController.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
//
// AlertController.swift
// goCrypto
//
// Created by shobhit tyagi on 16/07/18.
// Copyright © 2018 Rajat. All rights reserved.
//
import Foundation
import UIKit
// @Shobhit Tyagi, 09-july-2018
//Alert Controller
open class AlertController {
// MARK: - Singleton
static let instance = AlertController()
// MARK: - Private Functions
fileprivate func topMostController() -> UIViewController? {
var presentedVC = UIApplication.shared.keyWindow?.rootViewController
while let pVC = presentedVC?.presentedViewController {
presentedVC = pVC
}
if presentedVC == nil {
//print("AlertController Error: You don't have any views set. You may be calling in viewdidload. Try viewdidappear.")
}
return presentedVC
}
// MARK: - Class Functions
open class func alert(title: String) {
return alert(title: title, message: "")
}
open class func alert(message: String) {
return alert(title: "", message: message)
}
open class func alert(title: String, message: String) {
return alert(title: title, message: message, acceptMessage: "OK") { () -> () in
// Do nothing
}
}
open class func alert(title: String, message: String, acceptMessage: String, acceptBlock: @escaping () -> ()) {
DispatchQueue.main.async(execute: {
let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.alert)
let acceptButton = UIAlertAction(title: acceptMessage, style: .default, handler: { (action: UIAlertAction) in
acceptBlock()
})
alert.addAction(acceptButton)
instance.topMostController()?.present(alert, animated: true, completion: nil)
//return alert
})
}
open class func alert(title: String, message: String, buttons:[String], tapBlock:((UIAlertAction,Int) -> Void)?) {
DispatchQueue.main.async(execute: {
let alert = UIAlertController(title: title, message: message, preferredStyle: .alert, buttons: buttons, tapBlock: tapBlock)
instance.topMostController()?.present(alert, animated: true, completion: nil)
//return alert
})
}
open class func actionSheet(title: String, message: String, sourceView: UIView, actions: [UIAlertAction]) {
DispatchQueue.main.async(execute: {
let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.actionSheet)
for action in actions {
alert.addAction(action)
}
alert.popoverPresentationController?.sourceView = sourceView
alert.popoverPresentationController?.sourceRect = sourceView.bounds
instance.topMostController()?.present(alert, animated: true, completion: nil)
//return alert
})
}
open class func actionSheet(title: String, message: String, sourceView: UIView, buttons:[String], tapBlock:((UIAlertAction,Int) -> Void)?) {
DispatchQueue.main.async(execute: {
let alert = UIAlertController(title: title, message: message, preferredStyle: .actionSheet, buttons: buttons, tapBlock: tapBlock)
alert.popoverPresentationController?.sourceView = sourceView
alert.popoverPresentationController?.sourceRect = sourceView.bounds
instance.topMostController()?.present(alert, animated: true, completion: nil)
//return alert
})
}
}
private extension UIAlertController {
convenience init(title: String?, message: String?, preferredStyle: UIAlertControllerStyle, buttons:[String], tapBlock:((UIAlertAction,Int) -> Void)?) {
self.init(title: title, message: message, preferredStyle:preferredStyle)
var buttonIndex = 0
for buttonTitle in buttons {
let action = UIAlertAction(title: buttonTitle, preferredStyle: .default, buttonIndex: buttonIndex, tapBlock: tapBlock)
buttonIndex += 1
self.addAction(action)
}
}
}
private extension UIAlertAction {
convenience init(title: String?, preferredStyle: UIAlertActionStyle, buttonIndex:Int, tapBlock:((UIAlertAction,Int) -> Void)?) {
self.init(title: title, style: preferredStyle) {
(action:UIAlertAction) in
if let block = tapBlock {
block(action,buttonIndex)
}
}
}
}