Skip to content

Commit 6bd8a8a

Browse files
committed
Updated completions way.
1 parent fa54349 commit 6bd8a8a

File tree

6 files changed

+41
-31
lines changed

6 files changed

+41
-31
lines changed

SPAlert.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Pod::Spec.new do |s|
22

33
s.name = 'SPAlert'
4-
s.version = '5.1.5'
4+
s.version = '5.1.6'
55
s.summary = 'Native alert from Apple Music & Feedback. Contains Done, Heart & Message and other presets. Support SwiftUI.'
66
s.homepage = 'https://github.com/sparrowcode/AlertKit'
77
s.source = { :git => 'https://github.com/sparrowcode/AlertKit.git', :tag => s.version }

Sources/AlertKit/AlertKitAPI.swift

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,31 @@ public enum AlertKitAPI {
2626
}
2727
}
2828

29-
public static func dismissAllAlerts() {
29+
public static func dismissAllAlerts(completion: (() -> Void)? = nil) {
30+
31+
var alertViews: [AlertViewProtocol] = []
32+
3033
for window in UIApplication.shared.windows {
3134
for view in window.subviews {
3235
if let view = view as? AlertViewProtocol {
33-
view.dismiss()
36+
alertViews.append(view)
3437
}
3538
}
3639
}
40+
41+
if alertViews.isEmpty {
42+
completion?()
43+
} else {
44+
for (index, view) in alertViews.enumerated() {
45+
if index == .zero {
46+
view.dismiss(completion: completion)
47+
} else {
48+
view.dismiss(completion: nil)
49+
}
50+
}
51+
alertViews.first?.dismiss {
52+
completion?()
53+
}
54+
}
3755
}
3856
}

Sources/AlertKit/Extensions/SwiftUIExtension.swift

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,14 @@ import SwiftUI
33
@available(iOS 13.0, *)
44
extension View {
55

6-
public func alert(isPresent: Binding<Bool>, view: AlertViewProtocol) -> some View {
6+
public func alert(isPresent: Binding<Bool>, view: AlertViewProtocol, completion: (()->Void)? = nil) -> some View {
77
if isPresent.wrappedValue {
8-
let alertCompletion = view.completion
9-
let completion = {
8+
let wrapperCompletion = {
109
isPresent.wrappedValue = false
11-
alertCompletion?()
10+
completion?()
1211
}
1312
if let window = UIApplication.shared.windows.filter({ $0.isKeyWindow }).first {
14-
view.present(on: window, completion: completion)
13+
view.present(on: window, completion: wrapperCompletion)
1514
}
1615
}
1716
return self

Sources/AlertKit/Views/AlertAppleMusic16View.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ public class AlertAppleMusic16View: UIView, AlertViewProtocol {
2323
fileprivate var presentDismissDuration: TimeInterval = 0.2
2424
fileprivate var presentDismissScale: CGFloat = 0.8
2525

26-
open var completion: (() -> Void)? = nil
27-
2826
private lazy var backgroundView: UIVisualEffectView = {
2927
let view: UIVisualEffectView = {
3028
#if !os(tvOS)
@@ -124,8 +122,7 @@ public class AlertAppleMusic16View: UIView, AlertViewProtocol {
124122
fatalError("init(coder:) has not been implemented")
125123
}
126124

127-
open func present(on view: UIView, completion: @escaping ()->Void = {}) {
128-
self.completion = completion
125+
open func present(on view: UIView, completion: (()->Void)? = nil) {
129126
self.viewForPresent = view
130127
viewForPresent?.addSubview(self)
131128
guard let viewForPresent = viewForPresent else { return }
@@ -156,19 +153,22 @@ public class AlertAppleMusic16View: UIView, AlertViewProtocol {
156153

157154
if self.dismissInTime {
158155
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + self.duration) {
159-
self.dismiss()
156+
// If dismiss manually no need call original completion.
157+
if self.alpha != 0 {
158+
self.dismiss(completion: completion)
159+
}
160160
}
161161
}
162162
})
163163
}
164164

165-
@objc open func dismiss() {
165+
@objc open func dismiss(completion: (()->Void)? = nil) {
166166
UIView.animate(withDuration: presentDismissDuration, animations: {
167167
self.alpha = 0
168168
self.transform = self.transform.scaledBy(x: self.presentDismissScale, y: self.presentDismissScale)
169169
}, completion: { [weak self] finished in
170170
self?.removeFromSuperview()
171-
self?.completion?()
171+
completion?()
172172
})
173173
}
174174

Sources/AlertKit/Views/AlertAppleMusic17View.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@ public class AlertAppleMusic17View: UIView, AlertViewProtocol {
2828
fileprivate var presentDismissDuration: TimeInterval = 0.2
2929
fileprivate var presentDismissScale: CGFloat = 0.8
3030

31-
open var completion: (() -> Void)? = nil
32-
3331
private lazy var backgroundView: UIView = {
3432
#if os(visionOS)
3533
let swiftUIView = VisionGlassBackgroundView(cornerRadius: 12)
@@ -126,8 +124,7 @@ public class AlertAppleMusic17View: UIView, AlertViewProtocol {
126124
fatalError("init(coder:) has not been implemented")
127125
}
128126

129-
open func present(on view: UIView, completion: @escaping ()->Void = {}) {
130-
self.completion = completion
127+
open func present(on view: UIView, completion: (()->Void)? = nil) {
131128
self.viewForPresent = view
132129
viewForPresent?.addSubview(self)
133130
guard let viewForPresent = viewForPresent else { return }
@@ -164,19 +161,22 @@ public class AlertAppleMusic17View: UIView, AlertViewProtocol {
164161

165162
if self.dismissInTime {
166163
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + self.duration) {
167-
self.dismiss()
164+
// If dismiss manually no need call original completion.
165+
if self.alpha != 0 {
166+
self.dismiss(completion: completion)
167+
}
168168
}
169169
}
170170
})
171171
}
172172

173-
@objc open func dismiss() {
173+
@objc open func dismiss(completion: (()->Void)? = nil) {
174174
UIView.animate(withDuration: presentDismissDuration, animations: {
175175
self.alpha = 0
176176
self.transform = self.transform.scaledBy(x: self.presentDismissScale, y: self.presentDismissScale)
177177
}, completion: { [weak self] finished in
178178
self?.removeFromSuperview()
179-
self?.completion?()
179+
completion?()
180180
})
181181
}
182182

Sources/AlertKit/Views/AlertViewProtocol.swift

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,6 @@ import UIKit
22

33
public protocol AlertViewProtocol {
44

5-
func present(on view: UIView, completion: @escaping ()->Void)
6-
func dismiss()
7-
8-
var completion: (() -> Void)? { get set }
9-
}
10-
11-
extension AlertViewProtocol where Self: UIView {
12-
13-
func present(on view: UIView, completion: @escaping ()->Void = {}) {}
5+
func present(on view: UIView, completion: (()->Void)?)
6+
func dismiss(completion: (()->Void)?)
147
}

0 commit comments

Comments
 (0)