-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Alert.swift
163 lines (139 loc) · 6.96 KB
/
Alert.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
//
// Alert.swift
// Alertift
//
// Created by Suguru Kishimoto on 4/26/17.
// Copyright © 2017 Suguru Kishimoto. All rights reserved.
//
import Foundation
import UIKit
extension Alertift {
public enum AlertImagePosition {
case top // Above title and message
case center // Between title and message
case bottom // Below title and message
}
/// Alert
final public class Alert: AlertType, _AlertType {
public typealias Handler = (UIAlertAction, Int, [UITextField]?) -> Void
/// TextFieldHandler
public typealias TextFieldHandler = ((UITextField, Int) -> Void)
/// ActionWithTextFieldsHandler
public typealias ActionWithTextFieldsHandler = ([UITextField]?) -> Void
var _alertController: InnerAlertController!
public var alertController: UIAlertController {
return _alertController as UIAlertController
}
public static var backgroundColor: UIColor?
public static var buttonTextColor: UIColor?
public static var titleTextColor: UIColor?
public static var messageTextColor: UIColor?
/// Make alert
///
/// - Parameters:
/// - title: The title of the alert. Use this string to get the user’s attention and communicate the reason for the alert.
/// - message: Descriptive text that provides additional details about the reason for the alert.
/// - Returns: Instance of **Alert**
public init(title: String? = nil, message: String? = nil) {
buildAlertControlelr(title: title, message: message, style: .alert)
}
public func action(_ action: Alertift.Action, handler: Handler?) -> Self {
return self.action(action, isPreferred: false, handler: handler)
}
public func action(_ action: Alertift.Action, handler: ShortHandler? = nil) -> Self {
return self.action(action) { _, _, _ in handler?() }
}
/// Add action to Alert
///
/// - Parameters:
/// - action: Alert action.
/// - isPreferred: If you want to change this action to preferredAction, set true. Default is false.
/// - handler: The block to execute after this action performed.
/// - Returns: Myself
public func action(_ action: Alertift.Action, isPreferred: Bool, handler: Handler?) -> Self {
return self.action(action, image: nil, isPreferred: isPreferred, handler: handler)
}
public func action(_ action: Alertift.Action, isPreferred: Bool, handler: ShortHandler? = nil) -> Self {
return self.action(action, image: nil, isPreferred: isPreferred) { _, _, _ in handler?() }
}
/// Add action to Alert
///
/// - Parameters:
/// - action: Alert action.
/// - image: Image of action.
/// - renderMode: Render mode for alert action image. Default is `.automatic`
/// - handler: The block to execute after this action performed.
/// - Returns: Myself
public func action(_ action: Alertift.Action, image: UIImage?, renderingMode: UIImage.RenderingMode = .automatic, handler: Handler?) -> Self {
return self.action(action, image: image, renderingMode: renderingMode, isPreferred: false, handler: handler)
}
public func action(_ action: Alertift.Action, image: UIImage?, renderingMode: UIImage.RenderingMode = .automatic, handler: ShortHandler? = nil) -> Self {
return self.action(action, image: image, renderingMode: renderingMode, isPreferred: false) { _, _, _ in handler?() }
}
/// Add action to Alert
///
/// - Parameters:
/// - action: Alert action.
/// - image: Image of action
/// - renderMode: Render mode for alert action image. Default is `.automatic`
/// - isPreferred: If you want to change this action to preferredAction, set true. Default is false.
/// - handler: The block to execute after this action performed.
/// - Returns: Myself
public func action(_ action: Alertift.Action, image: UIImage?, renderingMode: UIImage.RenderingMode = .automatic, isPreferred: Bool, handler: Handler?) -> Self {
let alertAction = buildAlertAction(
action,
handler: merge(_alertController.actionWithTextFieldsHandler, handler ?? { (_, _, _)in })
)
if let image = image {
alertAction.setValue(image.withRenderingMode(renderingMode), forKey: "image")
}
addActionToAlertController(alertAction, isPreferred: isPreferred)
return self
}
public func action(_ action: Alertift.Action, image: UIImage?, renderingMode: UIImage.RenderingMode = .automatic, isPreferred: Bool, handler: ShortHandler? = nil) -> Self {
return self.action(action, image: image, renderingMode: renderingMode, isPreferred: isPreferred) { _, _, _ in handler?() }
}
/// Add text field to alertController
///
/// - Parameter handler: Define handler if you want to customize UITextField. Default is nil.
/// - Returns: Myself
public func textField(configurationHandler handler: ((UITextField) -> Void)? = nil) -> Self {
_alertController.addTextField { [weak self] textField in
handler?(textField)
self?._alertController.registerTextFieldObserver(textField)
}
return self
}
/// Add textFieldHandler to alertController.
///
/// If text field's text is changed, execute textFieldHandler with text field and index.
///
/// - Parameter textFieldTextDidChangeHandler: TextFieldHandler (UITextField, Int) -> Void
/// - Returns: Myself
public func handleTextFieldTextDidChange(textFieldTextDidChangeHandler: TextFieldHandler?) -> Self {
_alertController.textFieldTextDidChangeHandler = textFieldTextDidChangeHandler
return self
}
/// Add alertAction to alertController
///
/// - Parameters:
/// - alertAction: UIAlertAction
/// - isPreferred: If isPreferred is true, alertAction becomes preferredAction.
private func addActionToAlertController(_ alertAction: UIAlertAction, isPreferred: Bool) {
_alertController.addAction(alertAction)
if isPreferred {
_alertController.preferredAction = alertAction
}
}
func convertFinallyHandler(_ handler: Any) -> InnerAlertController.FinallyHandler {
return { (handler as? Handler)?($0, $1, $2) }
}
public func image(_ image: UIImage?, imageTopMargin: Alertift.ImageTopMargin = .none) -> Self {
_alertController.setImage(image, imageTopMargin: imageTopMargin)
return self
}
deinit {
Debug.log()
}
}
}