Skip to content

Commit 3d35de6

Browse files
committed
Fixed crash when tap for alert.
1 parent 3c7acb2 commit 3d35de6

7 files changed

+45
-21
lines changed

README.md

+7-5
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ or adding it to the `dependencies` of your `Package.swift`:
7575

7676
```swift
7777
dependencies: [
78-
.package(url: "https://github.com/sparrowcode/AlertKit", .upToNextMajor(from: "5.1.5"))
78+
.package(url: "https://github.com/sparrowcode/AlertKit", .upToNextMajor(from: "5.1.8"))
7979
]
8080
```
8181

@@ -99,7 +99,7 @@ If you prefer not to use any of dependency managers, you can integrate manually.
9999

100100
## SwiftUI
101101

102-
You can use basic way via AlertKitAPI or call via modifier:
102+
You can use basic way via `AlertKitAPI` or call via modifier:
103103

104104
```swift
105105
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:
114114

115115
```swift
116116
let alertView = AlertAppleMusic17View(title: "Added to Library", subtitle: nil, icon: .done)
117-
// Change Font
117+
118+
// change font
118119
alertView.titleLabel.font = UIFont.systemFont(ofSize: 21)
119-
// Change Color
120+
// change color
120121
alertView.titleLabel.textColor = .white
121122
```
122123

@@ -126,8 +127,9 @@ You can present and dismiss alerts manually via view.
126127

127128
```swift
128129
let alertView = AlertAppleMusic17View(title: "Added to Library", subtitle: nil, icon: .done)
129-
alertView.present(on: self)
130130

131+
// present
132+
alertView.present(on: self)
131133
// and dismiss
132134
alertView.dismiss()
133135
```

SPAlert.podspec

+1-1
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.6'
4+
s.version = '5.1.8'
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

+9-7
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,16 @@ public enum AlertKitAPI {
2626
}
2727
}
2828

29+
/**
30+
Call only with this one `completion`. Internal ones is canceled.
31+
*/
2932
public static func dismissAllAlerts(completion: (() -> Void)? = nil) {
3033

31-
var alertViews: [AlertViewProtocol] = []
34+
var alertViews: [AlertViewInternalDismissProtocol] = []
3235

3336
for window in UIApplication.shared.windows {
3437
for view in window.subviews {
35-
if let view = view as? AlertViewProtocol {
38+
if let view = view as? AlertViewInternalDismissProtocol {
3639
alertViews.append(view)
3740
}
3841
}
@@ -43,14 +46,13 @@ public enum AlertKitAPI {
4346
} else {
4447
for (index, view) in alertViews.enumerated() {
4548
if index == .zero {
46-
view.dismiss(completion: completion)
49+
view.dismiss(customCompletion: {
50+
completion?()
51+
})
4752
} else {
48-
view.dismiss(completion: nil)
53+
view.dismiss(customCompletion: nil)
4954
}
5055
}
51-
alertViews.first?.dismiss {
52-
completion?()
53-
}
5456
}
5557
}
5658
}

Sources/AlertKit/Views/AlertAppleMusic16View.swift

+10-3
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ public class AlertAppleMusic16View: UIView, AlertViewProtocol {
2323
fileprivate var presentDismissDuration: TimeInterval = 0.2
2424
fileprivate var presentDismissScale: CGFloat = 0.8
2525

26+
fileprivate var completion: (()->Void)? = nil
27+
2628
private lazy var backgroundView: UIVisualEffectView = {
2729
let view: UIVisualEffectView = {
2830
#if !os(tvOS)
@@ -124,6 +126,7 @@ public class AlertAppleMusic16View: UIView, AlertViewProtocol {
124126

125127
open func present(on view: UIView, completion: (()->Void)? = nil) {
126128
self.viewForPresent = view
129+
self.completion = completion
127130
viewForPresent?.addSubview(self)
128131
guard let viewForPresent = viewForPresent else { return }
129132

@@ -155,20 +158,24 @@ public class AlertAppleMusic16View: UIView, AlertViewProtocol {
155158
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + self.duration) {
156159
// If dismiss manually no need call original completion.
157160
if self.alpha != 0 {
158-
self.dismiss(completion: completion)
161+
self.dismiss()
159162
}
160163
}
161164
}
162165
})
163166
}
164167

165-
@objc open func dismiss(completion: (()->Void)? = nil) {
168+
@objc open func dismiss() {
169+
self.dismiss(customCompletion: self.completion)
170+
}
171+
172+
func dismiss(customCompletion: (()->Void)? = nil) {
166173
UIView.animate(withDuration: presentDismissDuration, animations: {
167174
self.alpha = 0
168175
self.transform = self.transform.scaledBy(x: self.presentDismissScale, y: self.presentDismissScale)
169176
}, completion: { [weak self] finished in
170177
self?.removeFromSuperview()
171-
completion?()
178+
customCompletion?()
172179
})
173180
}
174181

Sources/AlertKit/Views/AlertAppleMusic17View.swift

+11-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import UIKit
22
import SwiftUI
33

44
@available(iOS 13, visionOS 1, *)
5-
public class AlertAppleMusic17View: UIView, AlertViewProtocol {
5+
public class AlertAppleMusic17View: UIView, AlertViewProtocol, AlertViewInternalDismissProtocol {
66

77
open var dismissByTap: Bool = true
88
open var dismissInTime: Bool = true
@@ -28,6 +28,8 @@ public class AlertAppleMusic17View: UIView, AlertViewProtocol {
2828
fileprivate var presentDismissDuration: TimeInterval = 0.2
2929
fileprivate var presentDismissScale: CGFloat = 0.8
3030

31+
fileprivate var completion: (()->Void)? = nil
32+
3133
private lazy var backgroundView: UIView = {
3234
#if os(visionOS)
3335
let swiftUIView = VisionGlassBackgroundView(cornerRadius: 12)
@@ -126,6 +128,7 @@ public class AlertAppleMusic17View: UIView, AlertViewProtocol {
126128

127129
open func present(on view: UIView, completion: (()->Void)? = nil) {
128130
self.viewForPresent = view
131+
self.completion = completion
129132
viewForPresent?.addSubview(self)
130133
guard let viewForPresent = viewForPresent else { return }
131134

@@ -163,20 +166,24 @@ public class AlertAppleMusic17View: UIView, AlertViewProtocol {
163166
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + self.duration) {
164167
// If dismiss manually no need call original completion.
165168
if self.alpha != 0 {
166-
self.dismiss(completion: completion)
169+
self.dismiss()
167170
}
168171
}
169172
}
170173
})
171174
}
172175

173-
@objc open func dismiss(completion: (()->Void)? = nil) {
176+
@objc open func dismiss() {
177+
self.dismiss(customCompletion: self.completion)
178+
}
179+
180+
func dismiss(customCompletion: (()->Void)? = nil) {
174181
UIView.animate(withDuration: presentDismissDuration, animations: {
175182
self.alpha = 0
176183
self.transform = self.transform.scaledBy(x: self.presentDismissScale, y: self.presentDismissScale)
177184
}, completion: { [weak self] finished in
178185
self?.removeFromSuperview()
179-
completion?()
186+
customCompletion?()
180187
})
181188
}
182189

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import UIKit
2+
3+
protocol AlertViewInternalDismissProtocol {
4+
5+
func dismiss(customCompletion: (()->Void)?)
6+
}

Sources/AlertKit/Views/AlertViewProtocol.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ import UIKit
33
public protocol AlertViewProtocol {
44

55
func present(on view: UIView, completion: (()->Void)?)
6-
func dismiss(completion: (()->Void)?)
6+
func dismiss()
77
}

0 commit comments

Comments
 (0)