forked from shadowfacts/Tusker
49 lines
1.5 KiB
Swift
49 lines
1.5 KiB
Swift
//
|
|
// API.swift
|
|
// Duckable
|
|
//
|
|
// Created by Shadowfacts on 11/7/22.
|
|
//
|
|
|
|
import UIKit
|
|
|
|
@MainActor
|
|
public protocol DuckableViewController: UIViewController {
|
|
func duckableViewControllerShouldDuck() -> DuckAttemptAction
|
|
|
|
func duckableViewControllerMayAttemptToDuck()
|
|
|
|
func duckableViewControllerWillAnimateDuck(withDuration duration: CGFloat, afterDelay delay: CGFloat)
|
|
|
|
func duckableViewControllerDidFinishAnimatingDuck()
|
|
}
|
|
|
|
extension DuckableViewController {
|
|
public func duckableViewControllerShouldDuck() -> DuckAttemptAction { .duck }
|
|
public func duckableViewControllerMayAttemptToDuck() {}
|
|
public func duckableViewControllerWillAnimateDuck(withDuration duration: CGFloat, afterDelay delay: CGFloat) {}
|
|
public func duckableViewControllerDidFinishAnimatingDuck() {}
|
|
}
|
|
|
|
public enum DuckAttemptAction {
|
|
case duck
|
|
case dismiss
|
|
case block
|
|
}
|
|
|
|
extension UIViewController {
|
|
@available(iOS 16.0, *)
|
|
public func presentDuckable(_ viewController: DuckableViewController, animated: Bool, isDucked: Bool = false, completion: (() -> Void)? = nil) -> Bool {
|
|
var cur: UIViewController? = self
|
|
while let vc = cur {
|
|
if let container = vc as? DuckableContainerViewController {
|
|
container._presentDuckable(viewController, animated: animated, isDucked: isDucked, completion: completion)
|
|
return true
|
|
} else {
|
|
cur = vc.parent
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
}
|