mirror of
https://github.com/ichmagmaus111/ghostgram.git
synced 2026-11-19 13:14:09 +01:00
865 lines
34 KiB
Swift
865 lines
34 KiB
Swift
import Foundation
|
|
import UIKit
|
|
import AsyncDisplayKit
|
|
import SwiftSignalKit
|
|
import UIKitRuntimeUtils
|
|
|
|
public protocol StandalonePresentableController: ViewController {
|
|
}
|
|
|
|
private func findCurrentResponder(_ view: UIView) -> UIResponder? {
|
|
if view.isFirstResponder {
|
|
return view
|
|
} else {
|
|
for subview in view.subviews {
|
|
if let result = findCurrentResponder(subview) {
|
|
return result
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
}
|
|
|
|
func findWindow(_ view: UIView) -> WindowHost? {
|
|
if let view = view as? WindowHost {
|
|
return view
|
|
} else if let superview = view.superview {
|
|
return findWindow(superview)
|
|
} else {
|
|
return nil
|
|
}
|
|
}
|
|
|
|
public enum ViewControllerPresentationAnimation {
|
|
case none
|
|
case modalSheet
|
|
}
|
|
|
|
public struct ViewControllerSupportedOrientations: Equatable {
|
|
public var regularSize: UIInterfaceOrientationMask
|
|
public var compactSize: UIInterfaceOrientationMask
|
|
|
|
public init(regularSize: UIInterfaceOrientationMask, compactSize: UIInterfaceOrientationMask) {
|
|
self.regularSize = regularSize
|
|
self.compactSize = compactSize
|
|
}
|
|
|
|
public func intersection(_ other: ViewControllerSupportedOrientations) -> ViewControllerSupportedOrientations {
|
|
return ViewControllerSupportedOrientations(regularSize: self.regularSize.intersection(other.regularSize), compactSize: self.compactSize.intersection(other.compactSize))
|
|
}
|
|
}
|
|
|
|
open class ViewControllerPresentationArguments {
|
|
public let presentationAnimation: ViewControllerPresentationAnimation
|
|
public let completion: (() -> Void)?
|
|
|
|
public init(presentationAnimation: ViewControllerPresentationAnimation, completion: (() -> Void)? = nil) {
|
|
self.presentationAnimation = presentationAnimation
|
|
self.completion = completion
|
|
}
|
|
}
|
|
|
|
public enum ViewControllerNavigationPresentation {
|
|
case `default`
|
|
case master
|
|
case modal
|
|
case flatModal
|
|
case standaloneModal
|
|
case standaloneFlatModal
|
|
case modalInLargeLayout
|
|
case modalInCompactLayout
|
|
}
|
|
|
|
public enum TabBarItemContextActionType {
|
|
case none
|
|
case always
|
|
case whenActive
|
|
}
|
|
|
|
public protocol CustomViewControllerNavigationData: AnyObject {
|
|
func combine(summary: CustomViewControllerNavigationDataSummary?) -> CustomViewControllerNavigationDataSummary?
|
|
}
|
|
|
|
public protocol CustomViewControllerNavigationDataSummary: AnyObject {
|
|
}
|
|
|
|
@objc open class ViewController: UIViewController, ContainableController {
|
|
public struct NavigationLayout {
|
|
public var navigationFrame: CGRect
|
|
public var defaultContentHeight: CGFloat
|
|
|
|
public init(navigationFrame: CGRect, defaultContentHeight: CGFloat) {
|
|
self.navigationFrame = navigationFrame
|
|
self.defaultContentHeight = defaultContentHeight
|
|
}
|
|
}
|
|
|
|
private var validLayout: ContainerViewLayout?
|
|
public var currentlyAppliedLayout: ContainerViewLayout? {
|
|
return self.validLayout
|
|
}
|
|
|
|
public let presentationContext: PresentationContext
|
|
|
|
public final var supportedOrientations: ViewControllerSupportedOrientations = ViewControllerSupportedOrientations(regularSize: .all, compactSize: .allButUpsideDown) {
|
|
didSet {
|
|
if self.supportedOrientations != oldValue {
|
|
if self.isNodeLoaded {
|
|
self.window?.invalidateSupportedOrientations()
|
|
}
|
|
}
|
|
}
|
|
}
|
|
public final var lockedOrientation: UIInterfaceOrientationMask?
|
|
public final var lockOrientation: Bool = false {
|
|
didSet {
|
|
if self.lockOrientation != oldValue {
|
|
if !self.lockOrientation {
|
|
self.lockedOrientation = nil
|
|
}
|
|
if let window = self.window {
|
|
window.invalidateSupportedOrientations()
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
var blocksInteractionUntilReady: Bool = false
|
|
|
|
public final var isOpaqueWhenInOverlay: Bool = false
|
|
public final var blocksBackgroundWhenInOverlay: Bool = false
|
|
public final var acceptsFocusWhenInOverlay: Bool = false
|
|
public final var automaticallyControlPresentationContextLayout: Bool = true
|
|
public var updateTransitionWhenPresentedAsModal: ((CGFloat, ContainedViewLayoutTransition) -> Void)?
|
|
|
|
public func requestUpdateParameters() {
|
|
self.modalStyleOverlayTransitionFactorUpdated?(.immediate)
|
|
}
|
|
|
|
public func combinedSupportedOrientations(currentOrientationToLock: UIInterfaceOrientationMask) -> ViewControllerSupportedOrientations {
|
|
return self.supportedOrientations
|
|
}
|
|
|
|
public final var deferScreenEdgeGestures: UIRectEdge = [] {
|
|
didSet {
|
|
if self.deferScreenEdgeGestures != oldValue {
|
|
self.window?.invalidateDeferScreenEdgeGestures()
|
|
}
|
|
}
|
|
}
|
|
|
|
public final var prefersOnScreenNavigationHidden: Bool = false {
|
|
didSet {
|
|
if self.prefersOnScreenNavigationHidden != oldValue {
|
|
self.window?.invalidatePrefersOnScreenNavigationHidden()
|
|
}
|
|
}
|
|
}
|
|
|
|
override open var prefersHomeIndicatorAutoHidden: Bool {
|
|
return self.prefersOnScreenNavigationHidden
|
|
}
|
|
|
|
open var previousItem: NavigationPreviousAction?
|
|
|
|
open var navigationPresentation: ViewControllerNavigationPresentation = .default
|
|
open var _presentedInModal: Bool = false
|
|
open var _hasGlassStyle: Bool = false
|
|
open var flatReceivesModalTransition: Bool = false
|
|
/// A full-screen viewer (a web page, a document) presented as a modal fills the screen beside a top-right status
|
|
/// island (iPhone Duo, unfolded portrait) rather than becoming a card below it; its bar then sits beside the island.
|
|
open var fillsScreenBesideStatusIsland: Bool = false
|
|
|
|
public var presentedOverCoveringView: Bool = false
|
|
|
|
public var presentationArguments: Any?
|
|
|
|
public var tabBarItemDebugTapAction: (() -> Void)?
|
|
|
|
public private(set) var modalStyleOverlayTransitionFactor: CGFloat = 0.0
|
|
public var modalStyleOverlayTransitionFactorUpdated: ((ContainedViewLayoutTransition) -> Void)?
|
|
public var customModalStyleOverlayTransitionFactorUpdated: ((ContainedViewLayoutTransition) -> Void)?
|
|
public func updateModalStyleOverlayTransitionFactor(_ value: CGFloat, transition: ContainedViewLayoutTransition) {
|
|
if self.modalStyleOverlayTransitionFactor != value {
|
|
self.modalStyleOverlayTransitionFactor = value
|
|
self.modalStyleOverlayTransitionFactorUpdated?(transition)
|
|
self.customModalStyleOverlayTransitionFactorUpdated?(transition)
|
|
}
|
|
}
|
|
|
|
private var _displayNode: ASDisplayNode?
|
|
public final var displayNode: ASDisplayNode {
|
|
get {
|
|
if let value = self._displayNode {
|
|
return value
|
|
}
|
|
else {
|
|
self.loadDisplayNode()
|
|
if self._displayNode == nil {
|
|
fatalError("displayNode should be initialized after loadDisplayNode()")
|
|
}
|
|
return self._displayNode!
|
|
}
|
|
}
|
|
set(value) {
|
|
self._displayNode = value
|
|
}
|
|
}
|
|
|
|
public final var isNodeLoaded: Bool {
|
|
return self._displayNode != nil
|
|
}
|
|
|
|
public let statusBar: StatusBar
|
|
public let navigationBar: NavigationBar?
|
|
open var transitionNavigationBar: NavigationBar? {
|
|
return self.navigationBar
|
|
}
|
|
/// The bar a navigation push or pop morphs into the other screen's (`NavigationBarTransitionSource`); nil keeps
|
|
/// today's slide. A screen with its own header returns nil.
|
|
open var navigationBarTransitionSource: NavigationBarTransitionSource? {
|
|
guard self.displayNavigationBar, let navigationBar = self.navigationBar, !navigationBar.isHidden else {
|
|
return nil
|
|
}
|
|
return navigationBar
|
|
}
|
|
/// This screen's side when the screen above it expands out of it (a push or pop between the two); nil: it does not
|
|
/// take part.
|
|
open func navigationExpansionSource(above controller: ViewController, operation: NavigationExpansionOperation) -> NavigationExpansionSource? {
|
|
return nil
|
|
}
|
|
|
|
/// This screen's side when it expands out of the screen below it; nil: it does not (the slide).
|
|
open func navigationExpansionDestination(below controller: ViewController, operation: NavigationExpansionOperation) -> NavigationExpansionDestination? {
|
|
return nil
|
|
}
|
|
public private(set) var toolbar: Toolbar?
|
|
|
|
public var displayNavigationBar = true
|
|
open var navigationBarRequiresEntireLayoutUpdate: Bool {
|
|
return true
|
|
}
|
|
|
|
/// iPhone Duo: whether the navigation bar lays out its side column (back or close, icon buttons, overflow) in the side strip (docs/iphone-duo-layout.md). A screen that lays out its own side column returns false.
|
|
open var placesNavigationButtonInSideColumn: Bool {
|
|
return true
|
|
}
|
|
|
|
/// iPhone Duo: where the bar puts its side column for `layout`, or nil while it puts none there.
|
|
private func navigationBarSideColumnPlacement(layout: ContainerViewLayout) -> NavigationBarSideColumnPlacement? {
|
|
guard self.placesNavigationButtonInSideColumn && self.displayNavigationBar else {
|
|
return nil
|
|
}
|
|
return NavigationBarSideColumnPlacement(layout: layout, reservedBelow: (self.parent as? TabBarController)?.verticalTabBarFrame(for: layout))
|
|
}
|
|
|
|
/// iPhone Duo: the side column the bar lays out for `layout` (nil without one), for a screen whose content keeps
|
|
/// clear of it.
|
|
public final func navigationBarSideColumnExtent(layout: ContainerViewLayout) -> NavigationBarSideColumnExtent? {
|
|
guard let navigationBar = self.navigationBar, let placement = self.navigationBarSideColumnPlacement(layout: layout) else {
|
|
return nil
|
|
}
|
|
return navigationBar.sideColumnExtent(placement: placement)
|
|
}
|
|
|
|
public struct TabBarSearchState: Equatable {
|
|
public var isActive: Bool
|
|
|
|
public init(isActive: Bool) {
|
|
self.isActive = isActive
|
|
}
|
|
}
|
|
|
|
public private(set) var tabBarSearchState: TabBarSearchState?
|
|
public var tabBarSearchStateUpdated: ((ContainedViewLayoutTransition) -> Void)?
|
|
public var currentTabBarSearchNode: (() -> ASDisplayNode?)?
|
|
|
|
private weak var activeInputViewCandidate: UIResponder?
|
|
private weak var activeInputView: UIResponder?
|
|
|
|
open var hasActiveInput: Bool = false
|
|
|
|
open var overlayWantsToBeBelowKeyboard: Bool {
|
|
return false
|
|
}
|
|
|
|
var internalOverlayWantsToBeBelowKeyboardUpdated: ((ContainedViewLayoutTransition) -> Void)?
|
|
public func overlayWantsToBeBelowKeyboardUpdated(transition: ContainedViewLayoutTransition) {
|
|
self.internalOverlayWantsToBeBelowKeyboardUpdated?(transition)
|
|
}
|
|
|
|
private var navigationBarOrigin: CGFloat = 0.0
|
|
|
|
open var interactiveNavivationGestureEdgeWidth: InteractiveTransitionGestureRecognizerEdgeWidth? {
|
|
return nil
|
|
}
|
|
|
|
open var navigationEdgeEffectExtension: CGFloat {
|
|
return 0.0
|
|
}
|
|
|
|
public func updateNavigationEdgeEffectExtension(transition: ContainedViewLayoutTransition) {
|
|
if let navigationBar = self.navigationBar {
|
|
navigationBar.updateEdgeEffectExtension(value: max(0.0, self.navigationEdgeEffectExtension - navigationBar.frame.maxY), transition: transition)
|
|
}
|
|
}
|
|
|
|
open func navigationLayout(layout: ContainerViewLayout) -> NavigationLayout {
|
|
// Beside iPhone Duo's status island the bar stays near the top rather than below the island.
|
|
let statusBarHeight: CGFloat = layout.navigationBarStatusBarHeight ?? 0.0
|
|
var defaultNavigationBarHeight: CGFloat
|
|
if self._presentedInModal && self._hasGlassStyle {
|
|
defaultNavigationBarHeight = 68.0
|
|
} else {
|
|
defaultNavigationBarHeight = 60.0
|
|
}
|
|
let navigationBarHeight: CGFloat = statusBarHeight + (self.navigationBar?.contentHeight(defaultHeight: defaultNavigationBarHeight) ?? defaultNavigationBarHeight)
|
|
|
|
var navigationBarFrame = CGRect(origin: CGPoint(x: 0.0, y: 0.0), size: CGSize(width: layout.size.width, height: navigationBarHeight))
|
|
|
|
navigationBarFrame.size.height += self.additionalNavigationBarHeight
|
|
|
|
if !self.displayNavigationBar {
|
|
navigationBarFrame.origin.y = -navigationBarFrame.size.height
|
|
}
|
|
|
|
self.navigationBarOrigin = navigationBarFrame.origin.y
|
|
|
|
return NavigationLayout(navigationFrame: navigationBarFrame, defaultContentHeight: defaultNavigationBarHeight)
|
|
}
|
|
|
|
open var cleanNavigationHeight: CGFloat {
|
|
if let navigationBar = self.navigationBar {
|
|
var height = navigationBar.frame.maxY
|
|
if let contentNode = navigationBar.contentNode, case .expansion = contentNode.mode {
|
|
height += contentNode.nominalHeight - contentNode.height
|
|
}
|
|
return height
|
|
} else {
|
|
return 0.0
|
|
}
|
|
}
|
|
|
|
open var additionalNavigationBarHeight: CGFloat {
|
|
return 0.0
|
|
}
|
|
|
|
public var additionalSideInsets: UIEdgeInsets = UIEdgeInsets()
|
|
|
|
private let _ready = Promise<Bool>(true)
|
|
open var ready: Promise<Bool> {
|
|
return self._ready
|
|
}
|
|
|
|
public var scrollToTop: (() -> Void)?
|
|
public var scrollToTopWithTabBar: (() -> Void)?
|
|
public var longTapWithTabBar: (() -> Void)?
|
|
|
|
public var customPresentPreviewingController: ((ViewController, ASDisplayNode) -> ViewController?)?
|
|
|
|
open func updateNavigationCustomData(_ data: Any?, progress: CGFloat, transition: ContainedViewLayoutTransition) {
|
|
|
|
}
|
|
|
|
open var customData: Any? {
|
|
get {
|
|
return nil
|
|
}
|
|
}
|
|
|
|
open var customNavigationData: CustomViewControllerNavigationData? {
|
|
get {
|
|
return nil
|
|
}
|
|
}
|
|
open var customNavigationDataSummary: CustomViewControllerNavigationDataSummary?
|
|
|
|
public internal(set) var isInFocus: Bool = false {
|
|
didSet {
|
|
if self.isInFocus != oldValue {
|
|
self.inFocusUpdated(isInFocus: self.isInFocus)
|
|
}
|
|
}
|
|
}
|
|
open func inFocusUpdated(isInFocus: Bool) {
|
|
}
|
|
|
|
public var attemptNavigation: (@escaping () -> Void) -> Bool = { _ in
|
|
return true
|
|
}
|
|
|
|
open func preferredContentSizeForLayout(_ layout: ContainerViewLayout) -> CGSize? {
|
|
return nil
|
|
}
|
|
|
|
open func didAppearInContextPreview() {
|
|
}
|
|
|
|
public var titleSignal: Signal<String?, NoError> {
|
|
return Signal { [weak self] subscriber in
|
|
guard let self else {
|
|
return EmptyDisposable
|
|
}
|
|
subscriber.putNext(self.navigationItem.title)
|
|
let listenerIndex = self.navigationItem.addSetTitleListener { title, _ in
|
|
subscriber.putNext(title)
|
|
}
|
|
return ActionDisposable { [weak self] in
|
|
if let self {
|
|
self.navigationItem.removeSetTitleListener(listenerIndex)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public init(navigationBarPresentationData: NavigationBarPresentationData?) {
|
|
self.statusBar = StatusBar()
|
|
if let navigationBarPresentationData = navigationBarPresentationData {
|
|
self.navigationBar = defaultNavigationBarImpl!(navigationBarPresentationData)
|
|
} else {
|
|
self.navigationBar = nil
|
|
}
|
|
self.presentationContext = PresentationContext()
|
|
|
|
super.init(nibName: nil, bundle: nil)
|
|
|
|
self.navigationBar?.backPressed = { [weak self] in
|
|
if let strongSelf = self, strongSelf.attemptNavigation({
|
|
guard let strongSelf = self else {
|
|
return
|
|
}
|
|
if let navigationController = strongSelf.navigationController as? NavigationController {
|
|
navigationController.filterController(strongSelf, animated: true)
|
|
} else {
|
|
strongSelf.navigationController?.popViewController(animated: true)
|
|
}
|
|
}) {
|
|
if let navigationController = strongSelf.navigationController as? NavigationController {
|
|
navigationController.filterController(strongSelf, animated: true)
|
|
} else {
|
|
strongSelf.navigationController?.popViewController(animated: true)
|
|
}
|
|
}
|
|
}
|
|
self.navigationBar?.requestContainerLayout = { [weak self] transition in
|
|
if let strongSelf = self, strongSelf.isNodeLoaded, let validLayout = strongSelf.validLayout {
|
|
if strongSelf.navigationBarRequiresEntireLayoutUpdate {
|
|
strongSelf.containerLayoutUpdated(validLayout, transition: transition)
|
|
} else {
|
|
strongSelf.updateNavigationBarLayout(validLayout, transition: transition)
|
|
}
|
|
}
|
|
}
|
|
self.navigationBar?.sideColumnExtentUpdated = { [weak self] transition in
|
|
self?.requestLayout(transition: transition)
|
|
}
|
|
self.navigationBar?.item = self.navigationItem
|
|
//self.automaticallyAdjustsScrollViewInsets = false
|
|
|
|
self.scrollToTopWithTabBar = { [weak self] in
|
|
self?.scrollToTop?()
|
|
}
|
|
}
|
|
|
|
required public init(coder aDecoder: NSCoder) {
|
|
fatalError("init(coder:) has not been implemented")
|
|
}
|
|
|
|
deinit {
|
|
|
|
}
|
|
|
|
open func updateNavigationBarLayout(_ layout: ContainerViewLayout, transition: ContainedViewLayoutTransition) {
|
|
self.applyNavigationBarLayout(layout, navigationLayout: self.navigationLayout(layout: layout), additionalBackgroundHeight: 0.0, transition: transition)
|
|
}
|
|
|
|
public func applyNavigationBarLayout(_ layout: ContainerViewLayout, navigationLayout: NavigationLayout, additionalBackgroundHeight: CGFloat, transition: ContainedViewLayoutTransition) {
|
|
let statusBarHeight: CGFloat = layout.navigationBarStatusBarHeight ?? 0.0
|
|
|
|
var navigationBarFrame = CGRect(origin: CGPoint(x: 0.0, y: 0.0), size: CGSize(width: layout.size.width, height: navigationLayout.navigationFrame.maxY))
|
|
|
|
if !self.displayNavigationBar {
|
|
navigationBarFrame.origin.y = -navigationBarFrame.size.height
|
|
}
|
|
|
|
self.navigationBarOrigin = navigationBarFrame.origin.y
|
|
|
|
if let navigationBar = self.navigationBar {
|
|
if let contentNode = navigationBar.contentNode, case .expansion = contentNode.mode, !self.displayNavigationBar {
|
|
navigationBarFrame.origin.y -= navigationLayout.defaultContentHeight + statusBarHeight
|
|
navigationBarFrame.size.height += contentNode.height + navigationLayout.defaultContentHeight + statusBarHeight * 2.0
|
|
if self._presentedInModal && self._hasGlassStyle {
|
|
navigationBarFrame.size.height += 8.0
|
|
}
|
|
}
|
|
//navigationBar.backgroundColor = .blue
|
|
if let _ = navigationBar.contentNode, let _ = navigationBar.secondaryContentNode, !self.displayNavigationBar {
|
|
navigationBarFrame.size.height += navigationBar.secondaryContentHeight
|
|
}
|
|
|
|
var additionalTopHeight = statusBarHeight
|
|
if !self.displayNavigationBar {
|
|
additionalTopHeight -= statusBarHeight
|
|
if statusBarHeight != 0.0 {
|
|
additionalTopHeight += 6.0
|
|
}
|
|
}
|
|
if self._presentedInModal && self._hasGlassStyle {
|
|
additionalTopHeight += 8.0
|
|
}
|
|
|
|
navigationBar.trailingButtonsInset = layout.navigationBarTrailingButtonsInset
|
|
navigationBar.sideColumnPlacement = self.navigationBarSideColumnPlacement(layout: layout)
|
|
navigationBar.updateLayout(size: navigationBarFrame.size, defaultHeight: navigationLayout.defaultContentHeight, additionalTopHeight: additionalTopHeight, additionalContentHeight: self.additionalNavigationBarHeight, additionalBackgroundHeight: additionalBackgroundHeight, leftInset: layout.safeInsets.left, rightInset: layout.safeInsets.right, appearsHidden: !self.displayNavigationBar, transition: transition)
|
|
if !transition.isAnimated {
|
|
navigationBar.layer.removeAnimation(forKey: "bounds")
|
|
navigationBar.layer.removeAnimation(forKey: "position")
|
|
}
|
|
transition.updateFrame(node: navigationBar, frame: navigationBarFrame)
|
|
navigationBar.setHidden(!self.displayNavigationBar, animated: transition.isAnimated)
|
|
}
|
|
}
|
|
|
|
open func containerLayoutUpdated(_ layout: ContainerViewLayout, transition: ContainedViewLayoutTransition) {
|
|
self.validLayout = layout
|
|
|
|
if !self.isViewLoaded {
|
|
self.loadView()
|
|
}
|
|
if let _ = layout.statusBarHeight {
|
|
self.statusBar.frame = CGRect(origin: CGPoint(), size: CGSize(width: layout.size.width, height: 40.0))
|
|
}
|
|
|
|
self.updateNavigationBarLayout(layout, transition: transition)
|
|
|
|
if self.automaticallyControlPresentationContextLayout {
|
|
self.presentationContext.containerLayoutUpdated(layout, transition: transition)
|
|
}
|
|
}
|
|
|
|
open func navigationStackConfigurationUpdated(next: [ViewController]) {
|
|
}
|
|
|
|
open override func loadView() {
|
|
self.view = self.displayNode.view
|
|
if let navigationBar = self.navigationBar {
|
|
if navigationBar.supernode == nil {
|
|
self.displayNode.addSubnode(navigationBar)
|
|
}
|
|
}
|
|
self.view.autoresizingMask = []
|
|
self.view.addSubview(self.statusBar.view)
|
|
self.presentationContext.view = self.view
|
|
}
|
|
|
|
open func loadDisplayNode() {
|
|
self.displayNode = ASDisplayNode()
|
|
self.displayNodeDidLoad()
|
|
}
|
|
|
|
open func displayNodeDidLoad() {
|
|
if let backgroundColor = self.displayNode.backgroundColor, backgroundColor.alpha.isEqual(to: 1.0) {
|
|
self.blocksBackgroundWhenInOverlay = true
|
|
self.isOpaqueWhenInOverlay = true
|
|
}
|
|
}
|
|
|
|
public func requestLayout(transition: ContainedViewLayoutTransition) {
|
|
if self.isViewLoaded, let validLayout = self.validLayout {
|
|
self.containerLayoutUpdated(validLayout, transition: transition)
|
|
}
|
|
}
|
|
|
|
open func updateToInterfaceOrientation(_ orientation: UIInterfaceOrientation) {
|
|
|
|
}
|
|
|
|
public func setDisplayNavigationBar(_ displayNavigationBar: Bool, transition: ContainedViewLayoutTransition = .immediate) {
|
|
if displayNavigationBar != self.displayNavigationBar {
|
|
self.displayNavigationBar = displayNavigationBar
|
|
if let parent = self.parent as? TabBarController {
|
|
if parent.currentController === self {
|
|
parent.displayNavigationBar = displayNavigationBar
|
|
parent.requestLayout(transition: transition)
|
|
}
|
|
} else {
|
|
self.requestLayout(transition: transition)
|
|
}
|
|
}
|
|
}
|
|
|
|
public func setNavigationBarPresentationData(_ presentationData: NavigationBarPresentationData, animated: Bool) {
|
|
if animated, let navigationBar = self.navigationBar {
|
|
UIView.transition(with: navigationBar.view, duration: 0.3, options: [.transitionCrossDissolve], animations: {
|
|
}, completion: nil)
|
|
}
|
|
self.navigationBar?.updatePresentationData(presentationData, transition: .immediate)
|
|
if let parent = self.parent as? TabBarController {
|
|
if parent.currentController === self {
|
|
if animated, let navigationBar = parent.navigationBar {
|
|
UIView.transition(with: navigationBar.view, duration: 0.3, options: [.transitionCrossDissolve], animations: {
|
|
}, completion: nil)
|
|
}
|
|
parent.navigationBar?.updatePresentationData(presentationData, transition: .immediate)
|
|
}
|
|
}
|
|
}
|
|
|
|
public func setStatusBarStyle(_ style: StatusBarStyle, animated: Bool) {
|
|
self.statusBar.updateStatusBarStyle(style, animated: animated)
|
|
if let parent = self.parent as? TabBarController {
|
|
if parent.currentController === self {
|
|
parent.statusBar.updateStatusBarStyle(style, animated: animated)
|
|
}
|
|
}
|
|
}
|
|
|
|
override open func present(_ viewControllerToPresent: UIViewController, animated flag: Bool, completion: (() -> Void)? = nil) {
|
|
self.view.window?.rootViewController?.present(viewControllerToPresent, animated: flag, completion: completion)
|
|
}
|
|
|
|
override open func dismiss(animated flag: Bool, completion: (() -> Void)? = nil) {
|
|
if let navigationController = self.navigationController as? NavigationController {
|
|
var animated = flag
|
|
if case .standaloneModal = self.navigationPresentation {
|
|
animated = false
|
|
}
|
|
navigationController.filterController(self, animated: animated)
|
|
} else {
|
|
self.presentingViewController?.dismiss(animated: flag, completion: nil)
|
|
}
|
|
}
|
|
|
|
public final var window: WindowHost? {
|
|
if let window = self.view.window as? WindowHost {
|
|
return window
|
|
} else if let result = findWindow(self.view) {
|
|
return result
|
|
} else {
|
|
if let parent = self.parent as? ViewController {
|
|
return parent.window
|
|
}
|
|
return nil
|
|
}
|
|
}
|
|
|
|
public func push(_ controller: ViewController) {
|
|
(self.navigationController as? NavigationController)?.pushViewController(controller)
|
|
}
|
|
|
|
open func replace(with controller: ViewController) {
|
|
if let navigationController = self.navigationController as? NavigationController {
|
|
var controllers = navigationController.viewControllers
|
|
controllers.removeAll(where: { $0 === self })
|
|
controllers.append(controller)
|
|
navigationController.setViewControllers(controllers, animated: true)
|
|
}
|
|
}
|
|
|
|
open func present(_ controller: ViewController, in context: PresentationContextType, with arguments: Any? = nil, blockInteraction: Bool = false, completion: @escaping () -> Void = {}) {
|
|
if !(controller is StandalonePresentableController), case .window = context, let arguments = arguments as? ViewControllerPresentationArguments, case .modalSheet = arguments.presentationAnimation, self.navigationController != nil {
|
|
controller.navigationPresentation = .modal
|
|
self.push(controller)
|
|
} else {
|
|
controller.presentationArguments = arguments
|
|
switch context {
|
|
case .current:
|
|
self.presentationContext.present(controller, on: PresentationSurfaceLevel(rawValue: 0), completion: completion)
|
|
case let .window(level):
|
|
self.window?.present(controller, on: level, blockInteraction: blockInteraction, completion: completion)
|
|
}
|
|
}
|
|
}
|
|
|
|
public func forEachController(_ f: (ContainableController) -> Bool) {
|
|
for (controller, _) in self.presentationContext.controllers {
|
|
if !f(controller) {
|
|
break
|
|
}
|
|
}
|
|
}
|
|
|
|
public func presentInGlobalOverlay(_ controller: ViewController, with arguments: Any? = nil) {
|
|
controller.presentationArguments = arguments
|
|
self.window?.presentInGlobalOverlay(controller)
|
|
}
|
|
|
|
public func addGlobalPortalHostView(sourceView: PortalSourceView) {
|
|
self.window?.addGlobalPortalHostView(sourceView: sourceView)
|
|
}
|
|
|
|
open override func viewWillDisappear(_ animated: Bool) {
|
|
self.activeInputViewCandidate = findCurrentResponder(self.view)
|
|
|
|
super.viewWillDisappear(animated)
|
|
}
|
|
|
|
open override func viewDidDisappear(_ animated: Bool) {
|
|
self.activeInputView = self.activeInputViewCandidate
|
|
|
|
super.viewDidDisappear(animated)
|
|
}
|
|
|
|
open func viewWillLeaveNavigation() {
|
|
}
|
|
|
|
open override func viewDidAppear(_ animated: Bool) {
|
|
self.activeInputView = nil
|
|
|
|
super.viewDidAppear(animated)
|
|
}
|
|
|
|
open func dismiss(completion: (() -> Void)? = nil) {
|
|
if let navigationController = self.navigationController as? NavigationController {
|
|
navigationController.filterController(self, animated: true)
|
|
} else {
|
|
self.presentingViewController?.dismiss(animated: true, completion: nil)
|
|
}
|
|
completion?()
|
|
}
|
|
|
|
public final func navigationNextSibling() -> UIViewController? {
|
|
if let navigationController = self.navigationController as? NavigationController {
|
|
if let index = navigationController.viewControllers.firstIndex(where: { $0 === self }) {
|
|
if index != navigationController.viewControllers.count - 1 {
|
|
return navigationController.viewControllers[index + 1]
|
|
}
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
/// - Parameter ignoringScreenDimOverlay: pass `true` only when the question is "is this the
|
|
/// controller the user is currently on", not "can the user see it right now". The proximity dim
|
|
/// overlay blanks the screen while the phone is at the user's ear; a caller that latches
|
|
/// one-shot UI must leave this `false` or it will spend that UI invisibly.
|
|
public func traceVisibility(ignoringScreenDimOverlay: Bool = false) -> Bool {
|
|
if !self.isViewLoaded {
|
|
return false
|
|
}
|
|
return traceViewVisibility(view: self.view, rect: self.view.bounds, ignoringScreenDimOverlay: ignoringScreenDimOverlay)
|
|
}
|
|
|
|
open func setToolbar(_ toolbar: Toolbar?, transition: ContainedViewLayoutTransition) {
|
|
if self.toolbar != toolbar {
|
|
self.toolbar = toolbar
|
|
if let parent = self.parent as? TabBarController {
|
|
if parent.currentController === self {
|
|
parent.requestLayout(transition: transition)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
open func toolbarActionSelected(action: ToolbarActionOption) {
|
|
}
|
|
|
|
open var tabBarItemContextActionType: TabBarItemContextActionType = .none
|
|
|
|
open func tabBarItemContextAction(sourceView: ContextExtractedContentContainingView, gesture: ContextGesture) {
|
|
}
|
|
|
|
open func tabBarItemHasDoubleTapAction() -> Bool {
|
|
return false
|
|
}
|
|
|
|
open func tabBarItemPerformDoubleTapAction() {
|
|
}
|
|
|
|
open func tabBarDisabledAction() {
|
|
}
|
|
|
|
open func tabBarActivateSearch() {
|
|
}
|
|
|
|
open func tabBarDeactivateSearch() {
|
|
}
|
|
|
|
open func tabBarItemSwipeAction(direction: TabBarItemSwipeDirection) {
|
|
}
|
|
|
|
public func updateTabBarSearchState(_ tabBarSearchState: TabBarSearchState?, transition: ContainedViewLayoutTransition) {
|
|
if self.tabBarSearchState != tabBarSearchState {
|
|
self.tabBarSearchState = tabBarSearchState
|
|
self.tabBarSearchStateUpdated?(transition)
|
|
}
|
|
}
|
|
|
|
open func updatePossibleControllerDropContent(content: NavigationControllerDropContent?) {
|
|
}
|
|
|
|
open func acceptPossibleControllerDropContent(content: NavigationControllerDropContent) -> Bool {
|
|
return false
|
|
}
|
|
}
|
|
|
|
/// A layer carrying this name is the proximity dim overlay: the black surface shown while the device
|
|
/// is held to the user's ear, standing in for the screen being switched off.
|
|
///
|
|
/// Callers that ask "is this controller the one the user is currently on" want to look through it -
|
|
/// raise-to-listen must keep working with the phone at the ear. Callers that ask "can the user
|
|
/// actually see this right now" must not, or a one-shot tooltip gets burned while the screen is
|
|
/// blanked. So the exemption is opt-in per query, never global.
|
|
let screenDimOverlayLayerName = "ScreenDimOverlay"
|
|
|
|
func traceIsOpaque(layer: CALayer, rect: CGRect, ignoringScreenDimOverlay: Bool = false) -> Bool {
|
|
if ignoringScreenDimOverlay && layer.name == screenDimOverlayLayerName {
|
|
return false
|
|
}
|
|
if layer.bounds.contains(rect) {
|
|
if layer.isHidden {
|
|
return false
|
|
}
|
|
if layer.opacity < 0.01 {
|
|
return false
|
|
}
|
|
if let backgroundColor = layer.backgroundColor {
|
|
var alpha: CGFloat = 0.0
|
|
UIColor(cgColor: backgroundColor).getRed(nil, green: nil, blue: nil, alpha: &alpha)
|
|
if alpha > 0.95 {
|
|
return true
|
|
}
|
|
}
|
|
if let sublayers = layer.sublayers {
|
|
for sublayer in sublayers {
|
|
let sublayerRect = layer.convert(rect, to: sublayer)
|
|
if traceIsOpaque(layer: sublayer, rect: sublayerRect, ignoringScreenDimOverlay: ignoringScreenDimOverlay) {
|
|
return true
|
|
}
|
|
}
|
|
}
|
|
return false
|
|
} else {
|
|
return false
|
|
}
|
|
}
|
|
|
|
private func traceViewVisibility(view: UIView, rect: CGRect, ignoringScreenDimOverlay: Bool) -> Bool {
|
|
if view.isHidden {
|
|
return false
|
|
}
|
|
if view is UIWindow {
|
|
return true
|
|
} else if let superview = view.superview, let siblings = superview.layer.sublayers {
|
|
if view.window == nil {
|
|
return false
|
|
}
|
|
if let index = siblings.firstIndex(where: { $0 === view.layer }) {
|
|
let viewFrame = view.convert(rect, to: superview)
|
|
for i in (index + 1) ..< siblings.count {
|
|
if siblings[i].frame.contains(viewFrame) {
|
|
let siblingSubframe = view.layer.convert(viewFrame, to: siblings[i])
|
|
if traceIsOpaque(layer: siblings[i], rect: siblingSubframe, ignoringScreenDimOverlay: ignoringScreenDimOverlay) {
|
|
return false
|
|
}
|
|
}
|
|
}
|
|
return traceViewVisibility(view: superview, rect: viewFrame, ignoringScreenDimOverlay: ignoringScreenDimOverlay)
|
|
} else {
|
|
return false
|
|
}
|
|
} else {
|
|
return false
|
|
}
|
|
}
|