|
| 1 | +/* |
| 2 | + * MIT License |
| 3 | + * |
| 4 | + * Copyright (c) 2022 Ethan Wong |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in all |
| 14 | + * copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | + * SOFTWARE. |
| 23 | + */ |
| 24 | + |
| 25 | +import UIKit |
| 26 | + |
| 27 | +import RxSwift |
| 28 | + |
| 29 | +public enum RoutingAction<Element: RouteElement> { |
| 30 | + case push(element: Element) |
| 31 | + case pop |
| 32 | + case replaceRoot(element: Element) |
| 33 | +} |
| 34 | + |
| 35 | +open class RoutableNavigationController<Element: RouteElement>: UINavigationController { |
| 36 | + |
| 37 | + private var disposeBag = DisposeBag() |
| 38 | + |
| 39 | + public let coordinator: NavigationRouteCoordinator<Element> |
| 40 | + |
| 41 | + public init(coordinator: NavigationRouteCoordinator<Element>) { |
| 42 | + self.coordinator = coordinator |
| 43 | + super.init(nibName: nil, bundle: nil) |
| 44 | + } |
| 45 | + |
| 46 | + @available(*, unavailable) |
| 47 | + public required init?(coder aDecoder: NSCoder) { |
| 48 | + fatalError("\(#function) has not been implemented") |
| 49 | + } |
| 50 | + |
| 51 | + // swiftlint:disable:next unavailable_function |
| 52 | + open func viewController(forRouteElement element: Element) -> UIViewController? { |
| 53 | + fatalError("viewController(forRouteElement:) is meant be implemented by subclass") |
| 54 | + } |
| 55 | + |
| 56 | + open override func viewDidLoad() { |
| 57 | + super.viewDidLoad() |
| 58 | + |
| 59 | + rx.willShow.map { $0.viewController } |
| 60 | + .withLatestFrom(coordinator.currentRoute) { |
| 61 | + // (topViewController, currentHashes) |
| 62 | + return ($0, $1.elements.map { $0.routeHash }) |
| 63 | + } |
| 64 | + .subscribe(with: self) { owner, val in |
| 65 | + let willShowController = val.0 |
| 66 | + let currentHashes = val.1 |
| 67 | + |
| 68 | + let controllerHashes = owner.viewControllers.map { $0.routeHash } |
| 69 | + print("[RoutableNav]: Did received controller change, State hashes: \(currentHashes), Controller hashes: \(controllerHashes)") |
| 70 | + |
| 71 | + if !(controllerHashes == currentHashes) { |
| 72 | + print("[RoutableNav]: Route not match, perfoming adjustment") |
| 73 | + if |
| 74 | + Array(currentHashes[0..<(currentHashes.endIndex - 1)]) == controllerHashes, |
| 75 | + willShowController.routeHash == controllerHashes.last |
| 76 | + { |
| 77 | + // 第一类校正: 用户 pop 了最上方的 controller |
| 78 | + print("[RoutableNav]: Topmost controller poped by user, syncing state change") |
| 79 | + owner.coordinator.pop(performsSideEffect: false) |
| 80 | + } else { |
| 81 | + // 其他无法处理的场景 |
| 82 | + assertionFailure("Cannot perform route adjustment, route state can be corruped") |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + .disposed(by: disposeBag) |
| 87 | + |
| 88 | + coordinator.routingActions |
| 89 | + .emit(with: self) { owner, actions in |
| 90 | + let destViewControllers = actions.reduce(owner.viewControllers) { viewControllers, action in |
| 91 | + var viewControllers = viewControllers |
| 92 | + switch action { |
| 93 | + case .pop: |
| 94 | + viewControllers.removeLast() |
| 95 | + case .push(let element): |
| 96 | + if let destViewController = owner.viewController(forRouteElement: element) { |
| 97 | + assert(destViewController.routeHash != nil) |
| 98 | + viewControllers.append(destViewController) |
| 99 | + } |
| 100 | + case .replaceRoot(let element): |
| 101 | + if let destViewController = owner.viewController(forRouteElement: element) { |
| 102 | + assert(destViewController.routeHash != nil) |
| 103 | + viewControllers = [destViewController] |
| 104 | + } |
| 105 | + } |
| 106 | + return viewControllers |
| 107 | + } |
| 108 | + // Disable animation to prevent random ordered controllers on delegate calls |
| 109 | + owner.setViewControllers(destViewControllers, animated: false) |
| 110 | + } |
| 111 | + .disposed(by: disposeBag) |
| 112 | + } |
| 113 | + |
| 114 | +} |
0 commit comments