Shadowfacts
65d57df949
Allows people to move forward in the navigation stack after popping (making popping a non-destructive action).
77 lines
2.8 KiB
Swift
77 lines
2.8 KiB
Swift
//
|
|
// EnhancedNavigationViewController.swift
|
|
// Tusker
|
|
//
|
|
// Created by Shadowfacts on 2/19/20.
|
|
// Copyright © 2020 Shadowfacts. All rights reserved.
|
|
//
|
|
|
|
import UIKit
|
|
|
|
class EnhancedNavigationViewController: UINavigationController {
|
|
|
|
var poppedViewControllers = [UIViewController]()
|
|
var skipResetPoppedOnNextPush = false
|
|
|
|
private var interactivePushTransition: InteractivePushTransition!
|
|
|
|
override func viewDidLoad() {
|
|
super.viewDidLoad()
|
|
|
|
self.interactivePushTransition = InteractivePushTransition(navigationController: self)
|
|
}
|
|
|
|
override func popViewController(animated: Bool) -> UIViewController? {
|
|
if let popped = super.popViewController(animated: animated) {
|
|
poppedViewControllers.insert(popped, at: 0)
|
|
return popped
|
|
} else {
|
|
return nil
|
|
}
|
|
}
|
|
|
|
override func popToRootViewController(animated: Bool) -> [UIViewController]? {
|
|
if let popped = super.popToRootViewController(animated: animated) {
|
|
poppedViewControllers = popped
|
|
return popped
|
|
} else {
|
|
return nil
|
|
}
|
|
}
|
|
|
|
override func popToViewController(_ viewController: UIViewController, animated: Bool) -> [UIViewController]? {
|
|
if let popped = super.popToViewController(viewController, animated: animated) {
|
|
poppedViewControllers.insert(contentsOf: popped, at: 0)
|
|
return popped
|
|
} else {
|
|
return nil
|
|
}
|
|
}
|
|
|
|
override func pushViewController(_ viewController: UIViewController, animated: Bool) {
|
|
if skipResetPoppedOnNextPush {
|
|
skipResetPoppedOnNextPush = false
|
|
} else {
|
|
self.poppedViewControllers = []
|
|
}
|
|
super.pushViewController(viewController, animated: animated)
|
|
}
|
|
|
|
func onWillShow() {
|
|
self.transitionCoordinator?.notifyWhenInteractionChanges({ (context) in
|
|
if context.isCancelled {
|
|
if self.interactivePushTransition.interactive {
|
|
// when an interactive push gesture is cancelled, make sure to adding the VC that was being pushed back onto the popped stack so it doesn't disappear
|
|
self.poppedViewControllers.insert(self.interactivePushTransition.pushingViewController!, at: 0)
|
|
} else {
|
|
// when an interactive pop gesture is cancelled (i.e. the user lifts their finger before it triggers),
|
|
// the popViewController(animated:) method has already been called so the VC has already been added to the popped stack
|
|
// so we make sure to remove it, otherwise there could be duplicate VCs on the navigation stasck
|
|
self.poppedViewControllers.remove(at: 0)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
|
|
}
|