Fixed crash when tap for alert.

This commit is contained in:
Ivan Vorobei
2023-11-08 18:16:43 +03:00
parent 3c7acb2845
commit 3d35de60ad
7 changed files with 45 additions and 21 deletions
+7 -5
View File
@@ -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()
```
+1 -1
View File
@@ -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 }
+9 -7
View File
@@ -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?()
}
}
}
}
@@ -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?()
})
}
@@ -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?()
})
}
@@ -0,0 +1,6 @@
import UIKit
protocol AlertViewInternalDismissProtocol {
func dismiss(customCompletion: (()->Void)?)
}
@@ -3,5 +3,5 @@ import UIKit
public protocol AlertViewProtocol {
func present(on view: UIView, completion: (()->Void)?)
func dismiss(completion: (()->Void)?)
func dismiss()
}