diff --git a/TICoordinatorKit/Classes/Routable/Stack/StackRoutable.swift b/TICoordinatorKit/Classes/Routable/Stack/StackRoutable.swift index 7f75923..f728e49 100644 --- a/TICoordinatorKit/Classes/Routable/Stack/StackRoutable.swift +++ b/TICoordinatorKit/Classes/Routable/Stack/StackRoutable.swift @@ -31,10 +31,15 @@ public protocol StackRoutable: ModalRoutable { var topModule: Presentable? { get } func push(_ module: Presentable?) + func push(_ module: Presentable?, + completion: (() -> ())?) func push(_ module: Presentable?, animated: Bool) func push(_ module: Presentable?, animated: Bool, configurationClosure: ConfigurationClosure?) + func push(_ module: Presentable?, + animated: Bool, + completion: (() -> ())?) func push(_ module: Presentable?, animated: Bool, diff --git a/TICoordinatorKit/Classes/Routable/Stack/StackRouter.swift b/TICoordinatorKit/Classes/Routable/Stack/StackRouter.swift index 742e057..4fc45ae 100644 --- a/TICoordinatorKit/Classes/Routable/Stack/StackRouter.swift +++ b/TICoordinatorKit/Classes/Routable/Stack/StackRouter.swift @@ -22,7 +22,7 @@ import UIKit -open class StackRouter: StackRoutable { +open class StackRouter: NSObject, StackRoutable, UINavigationControllerDelegate { private var completions: [UIViewController: () -> ()] @@ -45,6 +45,10 @@ open class StackRouter: StackRoutable { self.rootController = rootController self.headModule = rootController.topViewController self.completions = [:] + + super.init() + + self.rootController?.delegate = self } // MARK: - StackRoutable @@ -53,6 +57,12 @@ open class StackRouter: StackRoutable { push(module, animated: true) } + public func push(_ module: Presentable?, + completion: (() -> ())?) { + + push(module, animated: true, completion: completion) + } + public func push(_ module: Presentable?, animated: Bool) { push(module, animated: animated, configurationClosure: nil, completion: nil) } @@ -64,7 +74,12 @@ open class StackRouter: StackRoutable { push(module, animated: animated, configurationClosure: configurationClosure, completion: nil) } - + public func push(_ module: Presentable?, + animated: Bool, + completion: (() -> ())?) { + + push(module, animated: animated, configurationClosure: nil, completion: completion) + } public func push(_ module: Presentable?, animated: Bool, @@ -200,10 +215,19 @@ open class StackRouter: StackRoutable { completions[controller]?() completions.removeValue(forKey: controller) } - + public func runCompletionsChain(of controllers: [UIViewController]) { controllers.forEach { [weak self] controller in self?.runCompletion(for: controller) } } + + public func navigationController(_ navigationController: UINavigationController, didShow viewController: UIViewController, animated: Bool) { + guard let poppedViewController = navigationController.transitionCoordinator?.viewController(forKey: .from), + !navigationController.viewControllers.contains(poppedViewController) else { + return + } + + runCompletion(for: poppedViewController) + } }