diff --git a/Sources/AlertKit/Extensions/SwiftUIExtension.swift b/Sources/AlertKit/Extensions/SwiftUIExtension.swift index 4f16b58..419f60a 100644 --- a/Sources/AlertKit/Extensions/SwiftUIExtension.swift +++ b/Sources/AlertKit/Extensions/SwiftUIExtension.swift @@ -1,16 +1,29 @@ import SwiftUI +import UIKit @available(iOS 13.0, *) extension View { - - public func alert(isPresent: Binding, view: AlertViewProtocol, completion: (()->Void)? = nil) -> some View { - if isPresent.wrappedValue { - let wrapperCompletion = { - isPresent.wrappedValue = false - completion?() - } - if let window = UIApplication.shared.windows.filter({ $0.isKeyWindow }).first { - view.present(on: window, completion: wrapperCompletion) + + public func alert( + isPresent: Binding, + view: AlertViewProtocol, + completion: (() -> Void)? = nil + ) -> some View { + // Perform side effects asynchronously to avoid state changes during view updates + DispatchQueue.main.async { + if isPresent.wrappedValue { + let wrapperCompletion = { + isPresent.wrappedValue = false + completion?() + } + if let window = UIApplication.shared.windows.first(where: { + $0.isKeyWindow + }) { + view.present(on: window, completion: wrapperCompletion) + } + } else { + // Programmatically dismiss any active alerts when Binding flips to false + AlertKitAPI.dismissAllAlerts(completion: completion) } } return self diff --git a/Sources/AlertKit/Views/AlertAppleMusic16View.swift b/Sources/AlertKit/Views/AlertAppleMusic16View.swift index b58062e..92ce7ca 100644 --- a/Sources/AlertKit/Views/AlertAppleMusic16View.swift +++ b/Sources/AlertKit/Views/AlertAppleMusic16View.swift @@ -1,7 +1,7 @@ import UIKit @available(iOS 13, *) -public class AlertAppleMusic16View: UIView, AlertViewProtocol { +public class AlertAppleMusic16View: UIView, AlertViewProtocol, AlertViewInternalDismissProtocol { open var dismissByTap: Bool = true open var dismissInTime: Bool = true