From 70c15afe0e0237de05c36565f7e7e3d8b7e08d86 Mon Sep 17 00:00:00 2001 From: Abel Stuker Date: Thu, 11 Sep 2025 00:56:22 +0200 Subject: [PATCH] fix: crashing after first slideovercard dismissal --- Sources/SlideOverCard/SOCManager.swift | 43 ++++++++++---------------- 1 file changed, 17 insertions(+), 26 deletions(-) diff --git a/Sources/SlideOverCard/SOCManager.swift b/Sources/SlideOverCard/SOCManager.swift index 9e58f8b..ff5135d 100644 --- a/Sources/SlideOverCard/SOCManager.swift +++ b/Sources/SlideOverCard/SOCManager.swift @@ -37,43 +37,35 @@ internal class SOCManager: ObservableObject { cardController = UIHostingController(rootView: rootCard) cardController?.view.backgroundColor = .clear cardController?.modalPresentationStyle = .overFullScreen - - model.$showCard - .removeDuplicates() - .sink { [weak self] value in - if !value { - self?.dismiss() - } - } - .store(in: &cancellables) + cardController?.modalTransitionStyle = .crossDissolve } /// Presents a `SlideOverCard` @available(iOSApplicationExtension, unavailable) func present() { - if let cardController, !self.model.showCard { + + guard !self.model.showCard else { return } + + if let cardController { var topViewController = window?.topViewController() - - // Fallback - if topViewController == nil { + + if let topViewController { + if topViewController.presentedViewController == nil { + topViewController.present(cardController, animated: false) { + self.model.showCard = true + } + } + } else { let windowScene = UIApplication.shared .connectedScenes - .filter { $0.activationState == .foregroundActive } .compactMap { $0 as? UIWindowScene } - .first + .first(where: { $0.activationState == .foregroundActive }) topViewController = windowScene? .windows - .filter { $0.isKeyWindow } - .first? + .first(where: { $0.isKeyWindow })? .rootViewController } - - if let topViewController { - topViewController.present(cardController, animated: false) { - self.model.showCard = true - } - } } } @@ -81,9 +73,8 @@ internal class SOCManager: ObservableObject { @available(iOSApplicationExtension, unavailable) func dismiss() { onDismiss?() - self.model.showCard = false - DispatchQueue.main.asyncAfter(deadline: .now()+0.5) { [weak self] in - self?.cardController?.dismiss(animated: false) + cardController?.dismiss(animated: true) { + self.model.showCard = false } }