Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 17 additions & 26 deletions Sources/SlideOverCard/SOCManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,53 +37,44 @@ internal class SOCManager<Content: View, Style: ShapeStyle>: 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
}
}
}
}

/// Dismisses a `SlideOverCard`
@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
}
}

Expand Down