Skip to content

Commit

Permalink
Updated completions way.
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanvorobei committed Nov 1, 2023
1 parent fa54349 commit 6bd8a8a
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 31 deletions.
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.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 }
Expand Down
22 changes: 20 additions & 2 deletions Sources/AlertKit/AlertKitAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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?()
}
}
}
}
9 changes: 4 additions & 5 deletions Sources/AlertKit/Extensions/SwiftUIExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@ import SwiftUI
@available(iOS 13.0, *)
extension View {

public func alert(isPresent: Binding<Bool>, view: AlertViewProtocol) -> some View {
public func alert(isPresent: Binding<Bool>, 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
Expand Down
14 changes: 7 additions & 7 deletions Sources/AlertKit/Views/AlertAppleMusic16View.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 }
Expand Down Expand Up @@ -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?()
})
}

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

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

0 comments on commit 6bd8a8a

Please sign in to comment.