2019-01-21 23:10:25 +00:00
|
|
|
/*
|
2019-01-22 16:20:28 +00:00
|
|
|
Copyright 2019 New Vector Ltd
|
2019-01-21 23:10:25 +00:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import UIKit
|
|
|
|
|
|
|
|
/// `NavigationRouter` is a concrete implementation of NavigationRouterType.
|
|
|
|
final class NavigationRouter: NSObject, NavigationRouterType {
|
|
|
|
|
|
|
|
// MARK: - Properties
|
|
|
|
|
|
|
|
// MARK: Private
|
|
|
|
|
|
|
|
private var completions: [UIViewController : () -> Void]
|
2020-01-31 14:02:02 +00:00
|
|
|
private let navigationController: UINavigationController
|
2019-01-21 23:10:25 +00:00
|
|
|
|
|
|
|
// MARK: Public
|
|
|
|
|
2020-01-31 14:02:02 +00:00
|
|
|
var modules: [Presentable] {
|
|
|
|
return navigationController.viewControllers
|
|
|
|
}
|
2019-01-21 23:10:25 +00:00
|
|
|
|
|
|
|
// MARK: - Setup
|
|
|
|
|
2020-04-08 15:02:00 +00:00
|
|
|
init(navigationController: UINavigationController = RiotNavigationController()) {
|
2019-01-21 23:10:25 +00:00
|
|
|
self.navigationController = navigationController
|
|
|
|
self.completions = [:]
|
|
|
|
super.init()
|
|
|
|
self.navigationController.delegate = self
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - Public
|
|
|
|
|
|
|
|
func present(_ module: Presentable, animated: Bool = true) {
|
2019-12-10 10:51:17 +00:00
|
|
|
NSLog("[NavigationRouter] Present \(module)")
|
2019-01-21 23:10:25 +00:00
|
|
|
navigationController.present(module.toPresentable(), animated: animated, completion: nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func dismissModule(animated: Bool = true, completion: (() -> Void)? = nil) {
|
2019-12-10 10:51:17 +00:00
|
|
|
NSLog("[NavigationRouter] Dismiss presented module")
|
2019-01-21 23:10:25 +00:00
|
|
|
navigationController.dismiss(animated: animated, completion: completion)
|
|
|
|
}
|
|
|
|
|
2019-10-23 14:28:30 +00:00
|
|
|
func setRootModule(_ module: Presentable, hideNavigationBar: Bool = false, animated: Bool = false, popCompletion: (() -> Void)? = nil) {
|
2019-12-10 10:51:17 +00:00
|
|
|
NSLog("[NavigationRouter] Set root module \(module)")
|
2019-10-23 14:28:30 +00:00
|
|
|
|
|
|
|
let controller = module.toPresentable()
|
|
|
|
|
|
|
|
// Avoid setting a UINavigationController onto stack
|
|
|
|
guard controller is UINavigationController == false else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-01-21 23:10:25 +00:00
|
|
|
// Call all completions so all coordinators can be deallocated
|
2019-12-10 10:51:17 +00:00
|
|
|
for presentable in completions.keys {
|
|
|
|
runCompletion(for: presentable)
|
|
|
|
}
|
2019-10-23 14:28:30 +00:00
|
|
|
|
|
|
|
if let popCompletion = popCompletion {
|
|
|
|
completions[controller] = popCompletion
|
|
|
|
}
|
|
|
|
|
|
|
|
navigationController.setViewControllers([controller], animated: animated)
|
2019-01-21 23:10:25 +00:00
|
|
|
navigationController.isNavigationBarHidden = hideNavigationBar
|
|
|
|
}
|
|
|
|
|
|
|
|
func popToRootModule(animated: Bool) {
|
2019-12-10 10:51:17 +00:00
|
|
|
NSLog("[NavigationRouter] Pop to root module")
|
|
|
|
|
2019-01-21 23:10:25 +00:00
|
|
|
if let controllers = navigationController.popToRootViewController(animated: animated) {
|
|
|
|
controllers.forEach { runCompletion(for: $0) }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func popToModule(_ module: Presentable, animated: Bool) {
|
2019-12-10 10:51:17 +00:00
|
|
|
NSLog("[NavigationRouter] Pop to module \(module)")
|
|
|
|
|
2019-01-21 23:10:25 +00:00
|
|
|
if let controllers = navigationController.popToViewController(module.toPresentable(), animated: animated) {
|
|
|
|
controllers.forEach { runCompletion(for: $0) }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func push(_ module: Presentable, animated: Bool = true, popCompletion: (() -> Void)? = nil) {
|
2019-12-10 10:51:17 +00:00
|
|
|
NSLog("[NavigationRouter] Push module \(module)")
|
2019-01-21 23:10:25 +00:00
|
|
|
|
|
|
|
let controller = module.toPresentable()
|
|
|
|
|
|
|
|
// Avoid pushing UINavigationController onto stack
|
|
|
|
guard controller is UINavigationController == false else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if let completion = popCompletion {
|
|
|
|
completions[controller] = completion
|
|
|
|
}
|
|
|
|
|
|
|
|
navigationController.pushViewController(controller, animated: animated)
|
|
|
|
}
|
|
|
|
|
|
|
|
func popModule(animated: Bool = true) {
|
2019-12-10 10:51:17 +00:00
|
|
|
NSLog("[NavigationRouter] Pop module")
|
|
|
|
|
2019-01-21 23:10:25 +00:00
|
|
|
if let controller = navigationController.popViewController(animated: animated) {
|
|
|
|
runCompletion(for: controller)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: Presentable
|
|
|
|
|
|
|
|
func toPresentable() -> UIViewController {
|
|
|
|
return navigationController
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - Private
|
|
|
|
|
|
|
|
private func runCompletion(for controller: UIViewController) {
|
2019-12-10 10:51:17 +00:00
|
|
|
guard let completion = completions[controller] else {
|
|
|
|
return
|
|
|
|
}
|
2019-01-21 23:10:25 +00:00
|
|
|
completion()
|
|
|
|
completions.removeValue(forKey: controller)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - UINavigationControllerDelegate
|
|
|
|
extension NavigationRouter: UINavigationControllerDelegate {
|
|
|
|
|
|
|
|
func navigationController(_ navigationController: UINavigationController, didShow viewController: UIViewController, animated: Bool) {
|
|
|
|
|
|
|
|
// Ensure the view controller is popping
|
|
|
|
guard let poppedViewController = navigationController.transitionCoordinator?.viewController(forKey: .from),
|
|
|
|
!navigationController.viewControllers.contains(poppedViewController) else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-12-10 10:51:17 +00:00
|
|
|
NSLog("[NavigationRouter] Poppped module: \(poppedViewController)")
|
|
|
|
|
2019-01-21 23:10:25 +00:00
|
|
|
runCompletion(for: poppedViewController)
|
|
|
|
}
|
|
|
|
}
|