From 6bd8a8aab893afacf28396a0fa12c598ab400742 Mon Sep 17 00:00:00 2001 From: Ivan Vorobei Date: Wed, 1 Nov 2023 14:29:07 +0300 Subject: [PATCH] Updated completions way. --- SPAlert.podspec | 2 +- Sources/AlertKit/AlertKitAPI.swift | 22 +++++++++++++++++-- .../Extensions/SwiftUIExtension.swift | 9 ++++---- .../Views/AlertAppleMusic16View.swift | 14 ++++++------ .../Views/AlertAppleMusic17View.swift | 14 ++++++------ .../AlertKit/Views/AlertViewProtocol.swift | 11 ++-------- 6 files changed, 41 insertions(+), 31 deletions(-) diff --git a/SPAlert.podspec b/SPAlert.podspec index 4dbe57f..a452884 100644 --- a/SPAlert.podspec +++ b/SPAlert.podspec @@ -1,7 +1,7 @@ Pod::Spec.new do |s| s.name = 'SPAlert' - s.version = '5.1.5' + s.version = '5.1.6' s.summary = 'Native alert from Apple Music & Feedback. Contains Done, Heart & Message and other presets. Support SwiftUI.' s.homepage = 'https://github.com/sparrowcode/AlertKit' s.source = { :git => 'https://github.com/sparrowcode/AlertKit.git', :tag => s.version } diff --git a/Sources/AlertKit/AlertKitAPI.swift b/Sources/AlertKit/AlertKitAPI.swift index 022f54f..e6218aa 100644 --- a/Sources/AlertKit/AlertKitAPI.swift +++ b/Sources/AlertKit/AlertKitAPI.swift @@ -26,13 +26,31 @@ public enum AlertKitAPI { } } - public static func dismissAllAlerts() { + public static func dismissAllAlerts(completion: (() -> Void)? = nil) { + + var alertViews: [AlertViewProtocol] = [] + for window in UIApplication.shared.windows { for view in window.subviews { if let view = view as? AlertViewProtocol { - view.dismiss() + alertViews.append(view) } } } + + if alertViews.isEmpty { + completion?() + } else { + for (index, view) in alertViews.enumerated() { + if index == .zero { + view.dismiss(completion: completion) + } else { + view.dismiss(completion: nil) + } + } + alertViews.first?.dismiss { + completion?() + } + } } } diff --git a/Sources/AlertKit/Extensions/SwiftUIExtension.swift b/Sources/AlertKit/Extensions/SwiftUIExtension.swift index c1a2bb5..4f16b58 100644 --- a/Sources/AlertKit/Extensions/SwiftUIExtension.swift +++ b/Sources/AlertKit/Extensions/SwiftUIExtension.swift @@ -3,15 +3,14 @@ import SwiftUI @available(iOS 13.0, *) extension View { - public func alert(isPresent: Binding, view: AlertViewProtocol) -> some View { + public func alert(isPresent: Binding, view: AlertViewProtocol, completion: (()->Void)? = nil) -> some View { if isPresent.wrappedValue { - let alertCompletion = view.completion - let completion = { + let wrapperCompletion = { isPresent.wrappedValue = false - alertCompletion?() + completion?() } if let window = UIApplication.shared.windows.filter({ $0.isKeyWindow }).first { - view.present(on: window, completion: completion) + view.present(on: window, completion: wrapperCompletion) } } return self diff --git a/Sources/AlertKit/Views/AlertAppleMusic16View.swift b/Sources/AlertKit/Views/AlertAppleMusic16View.swift index df83b68..c5df8be 100644 --- a/Sources/AlertKit/Views/AlertAppleMusic16View.swift +++ b/Sources/AlertKit/Views/AlertAppleMusic16View.swift @@ -23,8 +23,6 @@ public class AlertAppleMusic16View: UIView, AlertViewProtocol { fileprivate var presentDismissDuration: TimeInterval = 0.2 fileprivate var presentDismissScale: CGFloat = 0.8 - open var completion: (() -> Void)? = nil - private lazy var backgroundView: UIVisualEffectView = { let view: UIVisualEffectView = { #if !os(tvOS) @@ -124,8 +122,7 @@ public class AlertAppleMusic16View: UIView, AlertViewProtocol { fatalError("init(coder:) has not been implemented") } - open func present(on view: UIView, completion: @escaping ()->Void = {}) { - self.completion = completion + open func present(on view: UIView, completion: (()->Void)? = nil) { self.viewForPresent = view viewForPresent?.addSubview(self) guard let viewForPresent = viewForPresent else { return } @@ -156,19 +153,22 @@ public class AlertAppleMusic16View: UIView, AlertViewProtocol { if self.dismissInTime { DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + self.duration) { - self.dismiss() + // If dismiss manually no need call original completion. + if self.alpha != 0 { + self.dismiss(completion: completion) + } } } }) } - @objc open func dismiss() { + @objc open func dismiss(completion: (()->Void)? = nil) { UIView.animate(withDuration: presentDismissDuration, animations: { self.alpha = 0 self.transform = self.transform.scaledBy(x: self.presentDismissScale, y: self.presentDismissScale) }, completion: { [weak self] finished in self?.removeFromSuperview() - self?.completion?() + completion?() }) } diff --git a/Sources/AlertKit/Views/AlertAppleMusic17View.swift b/Sources/AlertKit/Views/AlertAppleMusic17View.swift index e6f10a3..968052b 100644 --- a/Sources/AlertKit/Views/AlertAppleMusic17View.swift +++ b/Sources/AlertKit/Views/AlertAppleMusic17View.swift @@ -28,8 +28,6 @@ public class AlertAppleMusic17View: UIView, AlertViewProtocol { fileprivate var presentDismissDuration: TimeInterval = 0.2 fileprivate var presentDismissScale: CGFloat = 0.8 - open var completion: (() -> Void)? = nil - private lazy var backgroundView: UIView = { #if os(visionOS) let swiftUIView = VisionGlassBackgroundView(cornerRadius: 12) @@ -126,8 +124,7 @@ public class AlertAppleMusic17View: UIView, AlertViewProtocol { fatalError("init(coder:) has not been implemented") } - open func present(on view: UIView, completion: @escaping ()->Void = {}) { - self.completion = completion + open func present(on view: UIView, completion: (()->Void)? = nil) { self.viewForPresent = view viewForPresent?.addSubview(self) guard let viewForPresent = viewForPresent else { return } @@ -164,19 +161,22 @@ public class AlertAppleMusic17View: UIView, AlertViewProtocol { if self.dismissInTime { DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + self.duration) { - self.dismiss() + // If dismiss manually no need call original completion. + if self.alpha != 0 { + self.dismiss(completion: completion) + } } } }) } - @objc open func dismiss() { + @objc open func dismiss(completion: (()->Void)? = nil) { UIView.animate(withDuration: presentDismissDuration, animations: { self.alpha = 0 self.transform = self.transform.scaledBy(x: self.presentDismissScale, y: self.presentDismissScale) }, completion: { [weak self] finished in self?.removeFromSuperview() - self?.completion?() + completion?() }) } diff --git a/Sources/AlertKit/Views/AlertViewProtocol.swift b/Sources/AlertKit/Views/AlertViewProtocol.swift index e4cbecb..19d1e6c 100644 --- a/Sources/AlertKit/Views/AlertViewProtocol.swift +++ b/Sources/AlertKit/Views/AlertViewProtocol.swift @@ -2,13 +2,6 @@ import UIKit public protocol AlertViewProtocol { - func present(on view: UIView, completion: @escaping ()->Void) - func dismiss() - - var completion: (() -> Void)? { get set } -} - -extension AlertViewProtocol where Self: UIView { - - func present(on view: UIView, completion: @escaping ()->Void = {}) {} + func present(on view: UIView, completion: (()->Void)?) + func dismiss(completion: (()->Void)?) }