From 3d35de60ad26aab58395ebecdd63b533546862ed Mon Sep 17 00:00:00 2001 From: Ivan Vorobei Date: Wed, 8 Nov 2023 18:16:43 +0300 Subject: [PATCH] Fixed crash when tap for alert. --- README.md | 12 +++++++----- SPAlert.podspec | 2 +- Sources/AlertKit/AlertKitAPI.swift | 16 +++++++++------- .../AlertKit/Views/AlertAppleMusic16View.swift | 13 ++++++++++--- .../AlertKit/Views/AlertAppleMusic17View.swift | 15 +++++++++++---- .../Views/AlertViewInternalDismissProtocol.swift | 6 ++++++ Sources/AlertKit/Views/AlertViewProtocol.swift | 2 +- 7 files changed, 45 insertions(+), 21 deletions(-) create mode 100644 Sources/AlertKit/Views/AlertViewInternalDismissProtocol.swift diff --git a/README.md b/README.md index 8540c9e..287dedb 100644 --- a/README.md +++ b/README.md @@ -75,7 +75,7 @@ or adding it to the `dependencies` of your `Package.swift`: ```swift dependencies: [ - .package(url: "https://github.com/sparrowcode/AlertKit", .upToNextMajor(from: "5.1.5")) + .package(url: "https://github.com/sparrowcode/AlertKit", .upToNextMajor(from: "5.1.8")) ] ``` @@ -99,7 +99,7 @@ If you prefer not to use any of dependency managers, you can integrate manually. ## SwiftUI -You can use basic way via AlertKitAPI or call via modifier: +You can use basic way via `AlertKitAPI` or call via modifier: ```swift let alertView = AlertAppleMusic17View(title: "Hello", subtitle: nil, icon: .done) @@ -114,9 +114,10 @@ If you need customisation fonts, icon, colors or any other, make view: ```swift let alertView = AlertAppleMusic17View(title: "Added to Library", subtitle: nil, icon: .done) -// Change Font + +// change font alertView.titleLabel.font = UIFont.systemFont(ofSize: 21) -// Change Color +// change color alertView.titleLabel.textColor = .white ``` @@ -126,8 +127,9 @@ You can present and dismiss alerts manually via view. ```swift let alertView = AlertAppleMusic17View(title: "Added to Library", subtitle: nil, icon: .done) -alertView.present(on: self) +// present +alertView.present(on: self) // and dismiss alertView.dismiss() ``` diff --git a/SPAlert.podspec b/SPAlert.podspec index a452884..9378b4e 100644 --- a/SPAlert.podspec +++ b/SPAlert.podspec @@ -1,7 +1,7 @@ Pod::Spec.new do |s| s.name = 'SPAlert' - s.version = '5.1.6' + s.version = '5.1.8' 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 e6218aa..52d9da3 100644 --- a/Sources/AlertKit/AlertKitAPI.swift +++ b/Sources/AlertKit/AlertKitAPI.swift @@ -26,13 +26,16 @@ public enum AlertKitAPI { } } + /** + Call only with this one `completion`. Internal ones is canceled. + */ public static func dismissAllAlerts(completion: (() -> Void)? = nil) { - var alertViews: [AlertViewProtocol] = [] + var alertViews: [AlertViewInternalDismissProtocol] = [] for window in UIApplication.shared.windows { for view in window.subviews { - if let view = view as? AlertViewProtocol { + if let view = view as? AlertViewInternalDismissProtocol { alertViews.append(view) } } @@ -43,14 +46,13 @@ public enum AlertKitAPI { } else { for (index, view) in alertViews.enumerated() { if index == .zero { - view.dismiss(completion: completion) + view.dismiss(customCompletion: { + completion?() + }) } else { - view.dismiss(completion: nil) + view.dismiss(customCompletion: nil) } } - alertViews.first?.dismiss { - completion?() - } } } } diff --git a/Sources/AlertKit/Views/AlertAppleMusic16View.swift b/Sources/AlertKit/Views/AlertAppleMusic16View.swift index 9c04ca3..d447d8b 100644 --- a/Sources/AlertKit/Views/AlertAppleMusic16View.swift +++ b/Sources/AlertKit/Views/AlertAppleMusic16View.swift @@ -23,6 +23,8 @@ public class AlertAppleMusic16View: UIView, AlertViewProtocol { fileprivate var presentDismissDuration: TimeInterval = 0.2 fileprivate var presentDismissScale: CGFloat = 0.8 + fileprivate var completion: (()->Void)? = nil + private lazy var backgroundView: UIVisualEffectView = { let view: UIVisualEffectView = { #if !os(tvOS) @@ -124,6 +126,7 @@ public class AlertAppleMusic16View: UIView, AlertViewProtocol { open func present(on view: UIView, completion: (()->Void)? = nil) { self.viewForPresent = view + self.completion = completion viewForPresent?.addSubview(self) guard let viewForPresent = viewForPresent else { return } @@ -155,20 +158,24 @@ public class AlertAppleMusic16View: UIView, AlertViewProtocol { DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + self.duration) { // If dismiss manually no need call original completion. if self.alpha != 0 { - self.dismiss(completion: completion) + self.dismiss() } } } }) } - @objc open func dismiss(completion: (()->Void)? = nil) { + @objc open func dismiss() { + self.dismiss(customCompletion: self.completion) + } + + func dismiss(customCompletion: (()->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() - completion?() + customCompletion?() }) } diff --git a/Sources/AlertKit/Views/AlertAppleMusic17View.swift b/Sources/AlertKit/Views/AlertAppleMusic17View.swift index 86d4611..f965198 100644 --- a/Sources/AlertKit/Views/AlertAppleMusic17View.swift +++ b/Sources/AlertKit/Views/AlertAppleMusic17View.swift @@ -2,7 +2,7 @@ import UIKit import SwiftUI @available(iOS 13, visionOS 1, *) -public class AlertAppleMusic17View: UIView, AlertViewProtocol { +public class AlertAppleMusic17View: UIView, AlertViewProtocol, AlertViewInternalDismissProtocol { open var dismissByTap: Bool = true open var dismissInTime: Bool = true @@ -28,6 +28,8 @@ public class AlertAppleMusic17View: UIView, AlertViewProtocol { fileprivate var presentDismissDuration: TimeInterval = 0.2 fileprivate var presentDismissScale: CGFloat = 0.8 + fileprivate var completion: (()->Void)? = nil + private lazy var backgroundView: UIView = { #if os(visionOS) let swiftUIView = VisionGlassBackgroundView(cornerRadius: 12) @@ -126,6 +128,7 @@ public class AlertAppleMusic17View: UIView, AlertViewProtocol { open func present(on view: UIView, completion: (()->Void)? = nil) { self.viewForPresent = view + self.completion = completion viewForPresent?.addSubview(self) guard let viewForPresent = viewForPresent else { return } @@ -163,20 +166,24 @@ public class AlertAppleMusic17View: UIView, AlertViewProtocol { DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + self.duration) { // If dismiss manually no need call original completion. if self.alpha != 0 { - self.dismiss(completion: completion) + self.dismiss() } } } }) } - @objc open func dismiss(completion: (()->Void)? = nil) { + @objc open func dismiss() { + self.dismiss(customCompletion: self.completion) + } + + func dismiss(customCompletion: (()->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() - completion?() + customCompletion?() }) } diff --git a/Sources/AlertKit/Views/AlertViewInternalDismissProtocol.swift b/Sources/AlertKit/Views/AlertViewInternalDismissProtocol.swift new file mode 100644 index 0000000..86d5052 --- /dev/null +++ b/Sources/AlertKit/Views/AlertViewInternalDismissProtocol.swift @@ -0,0 +1,6 @@ +import UIKit + +protocol AlertViewInternalDismissProtocol { + + func dismiss(customCompletion: (()->Void)?) +} diff --git a/Sources/AlertKit/Views/AlertViewProtocol.swift b/Sources/AlertKit/Views/AlertViewProtocol.swift index 19d1e6c..83b04cd 100644 --- a/Sources/AlertKit/Views/AlertViewProtocol.swift +++ b/Sources/AlertKit/Views/AlertViewProtocol.swift @@ -3,5 +3,5 @@ import UIKit public protocol AlertViewProtocol { func present(on view: UIView, completion: (()->Void)?) - func dismiss(completion: (()->Void)?) + func dismiss() }