Skip to content

Commit

Permalink
Fixed crash when tap for alert.
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanvorobei committed Nov 8, 2023
1 parent 3c7acb2 commit 3d35de6
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 21 deletions.
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
]
```

Expand All @@ -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)
Expand All @@ -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
```

Expand All @@ -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()
```
Expand Down
2 changes: 1 addition & 1 deletion SPAlert.podspec
Original file line number Diff line number Diff line change
@@ -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 }
Expand Down
16 changes: 9 additions & 7 deletions Sources/AlertKit/AlertKitAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand All @@ -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?()
}
}
}
}
13 changes: 10 additions & 3 deletions Sources/AlertKit/Views/AlertAppleMusic16View.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 }

Expand Down Expand Up @@ -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?()
})
}

Expand Down
15 changes: 11 additions & 4 deletions Sources/AlertKit/Views/AlertAppleMusic17View.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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 }

Expand Down Expand Up @@ -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?()
})
}

Expand Down
6 changes: 6 additions & 0 deletions Sources/AlertKit/Views/AlertViewInternalDismissProtocol.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import UIKit

protocol AlertViewInternalDismissProtocol {

func dismiss(customCompletion: (()->Void)?)
}
2 changes: 1 addition & 1 deletion Sources/AlertKit/Views/AlertViewProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ import UIKit
public protocol AlertViewProtocol {

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

0 comments on commit 3d35de6

Please sign in to comment.