2022-11-09 03:14:40 +00:00
|
|
|
//
|
|
|
|
// API.swift
|
|
|
|
// Duckable
|
|
|
|
//
|
|
|
|
// Created by Shadowfacts on 11/7/22.
|
|
|
|
//
|
|
|
|
|
|
|
|
import UIKit
|
|
|
|
|
|
|
|
public protocol DuckableViewController: UIViewController {
|
2023-05-04 22:40:00 +00:00
|
|
|
func duckableViewControllerShouldDuck() -> DuckAttemptAction
|
2023-04-24 01:55:14 +00:00
|
|
|
|
2022-11-09 03:14:40 +00:00
|
|
|
func duckableViewControllerMayAttemptToDuck()
|
|
|
|
|
|
|
|
func duckableViewControllerWillAnimateDuck(withDuration duration: CGFloat, afterDelay delay: CGFloat)
|
|
|
|
|
|
|
|
func duckableViewControllerDidFinishAnimatingDuck()
|
|
|
|
}
|
|
|
|
|
|
|
|
extension DuckableViewController {
|
2023-05-04 22:40:00 +00:00
|
|
|
public func duckableViewControllerShouldDuck() -> DuckAttemptAction { .duck }
|
2022-11-09 03:14:40 +00:00
|
|
|
public func duckableViewControllerMayAttemptToDuck() {}
|
|
|
|
public func duckableViewControllerWillAnimateDuck(withDuration duration: CGFloat, afterDelay delay: CGFloat) {}
|
|
|
|
public func duckableViewControllerDidFinishAnimatingDuck() {}
|
|
|
|
}
|
|
|
|
|
2023-05-04 22:40:00 +00:00
|
|
|
public enum DuckAttemptAction {
|
|
|
|
case duck
|
|
|
|
case dismiss
|
|
|
|
case block
|
|
|
|
}
|
|
|
|
|
2022-11-09 03:14:40 +00:00
|
|
|
extension UIViewController {
|
|
|
|
@available(iOS 16.0, *)
|
2022-11-28 20:21:05 +00:00
|
|
|
public func presentDuckable(_ viewController: DuckableViewController, animated: Bool, isDucked: Bool = false) -> Bool {
|
2022-11-09 03:14:40 +00:00
|
|
|
var cur: UIViewController? = self
|
|
|
|
while let vc = cur {
|
|
|
|
if let container = vc as? DuckableContainerViewController {
|
2022-11-28 20:21:05 +00:00
|
|
|
container.presentDuckable(viewController, animated: animated, isDucked: isDucked, completion: nil)
|
2022-11-09 03:14:40 +00:00
|
|
|
return true
|
|
|
|
} else {
|
|
|
|
cur = vc.parent
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|