Skip to content

Commit 9e9ced9

Browse files
committed
Updated confirm alert wrapper.
1 parent d63133b commit 9e9ced9

File tree

1 file changed

+28
-21
lines changed

1 file changed

+28
-21
lines changed

Sources/SparrowKit/UIKit/Extensions/UIAlertControllerExtension.swift

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -88,16 +88,16 @@ public extension UIAlertController {
8888
text: String? = nil,
8989
placeholder: String? = nil,
9090
editingChangedTarget: Any?,
91-
editingChangedSelector: Selector?) {
92-
93-
addTextField { textField in
94-
textField.text = text
95-
textField.placeholder = placeholder
96-
if let target = editingChangedTarget, let selector = editingChangedSelector {
97-
textField.addTarget(target, action: selector, for: .editingChanged)
98-
}
91+
editingChangedSelector: Selector?
92+
) {
93+
addTextField { textField in
94+
textField.text = text
95+
textField.placeholder = placeholder
96+
if let target = editingChangedTarget, let selector = editingChangedSelector {
97+
textField.addTarget(target, action: selector, for: .editingChanged)
9998
}
10099
}
100+
}
101101

102102
/**
103103
SparrowKit: Add Text Field to Alert Controller.
@@ -110,29 +110,36 @@ public extension UIAlertController {
110110
func addTextField(
111111
text: String? = nil,
112112
placeholder: String? = nil,
113-
action: UIAction?) {
114-
addTextField { textField in
115-
textField.text = text
116-
textField.placeholder = placeholder
117-
if let action = action {
118-
textField.addAction(action, for: .editingChanged)
119-
}
113+
action: UIAction?
114+
) {
115+
addTextField { textField in
116+
textField.text = text
117+
textField.placeholder = placeholder
118+
if let action = action {
119+
textField.addAction(action, for: .editingChanged)
120120
}
121121
}
122+
}
122123

123-
static func confirm(title: String, description: String, actionTitle: String, cancelTitle: String, desctructive: Bool, action: @escaping ()->Void, sourceView: UIView, on controller: UIViewController) {
124+
static func confirm(title: String, description: String, actionTitle: String, cancelTitle: String, desctructive: Bool, completion: @escaping (_ confirmed: Bool)->Void, sourceView: UIView, on controller: UIViewController) {
124125
let alertController = UIAlertController.init(title: title, message: description, preferredStyle: .actionSheet)
125126
alertController.popoverPresentationController?.sourceView = sourceView
126127
alertController.addAction(title: actionTitle, style: desctructive ? .destructive : .default) { [] _ in
127-
action()
128+
completion(true)
128129
}
129-
alertController.addAction(title: cancelTitle, style: .cancel, handler: nil)
130+
alertController.addAction(title: cancelTitle, style: .cancel, handler: { _ in
131+
completion(false)
132+
})
130133
controller.present(alertController)
131134
}
132135

133-
static func confirmDouble(title: String, description: String, actionTitle: String, cancelTitle: String, desctructive: Bool, action: @escaping ()->Void, sourceView: UIView, on controller: UIViewController) {
134-
confirm(title: title, description: description, actionTitle: actionTitle, cancelTitle: cancelTitle, desctructive: desctructive, action: {
135-
confirm(title: title, description: description, actionTitle: actionTitle, cancelTitle: cancelTitle, desctructive: desctructive, action: action, sourceView: sourceView, on: controller)
136+
static func confirmDouble(title: String, description: String, actionTitle: String, cancelTitle: String, desctructive: Bool, completion: @escaping (_ confirmed: Bool)->Void, sourceView: UIView, on controller: UIViewController) {
137+
confirm(title: title, description: description, actionTitle: actionTitle, cancelTitle: cancelTitle, desctructive: desctructive, completion: { confirmed in
138+
if confirmed {
139+
confirm(title: title, description: description, actionTitle: actionTitle, cancelTitle: cancelTitle, desctructive: desctructive, completion: completion, sourceView: sourceView, on: controller)
140+
} else {
141+
completion(false)
142+
}
136143
}, sourceView: sourceView, on: controller)
137144
}
138145
}

0 commit comments

Comments
 (0)