class="lines-num lines-num-new"> 27
override func start() -> Observable<Void> {
photoDetailViewController.viewModel.delegate = self
- return .never()
+ return didCancel
}
}
@@ -14,11 +14,19 @@ public class BaseCoordinator<ResultType> {
|
||
| 14 | 14 |
typealias CoordinationResult = ResultType |
| 15 | 15 |
|
| 16 | 16 |
let disposeBag = DisposeBag() |
| 17 |
- let didCancel = PublishSubject<Void>() |
|
| 17 |
+ var didCancel = PublishSubject<Void>() |
|
| 18 |
+ var navigationController: UINavigationController |
|
| 19 |
+ var viewController: UIViewController |
|
| 18 | 20 |
|
| 19 | 21 |
private let identifier = UUID() |
| 20 | 22 |
private var childCoordinators = [UUID: Any]() |
| 21 | 23 |
|
| 24 |
+ init(navigationController: UINavigationController, viewController: UIViewController) {
|
|
| 25 |
+ self.viewController = viewController |
|
| 26 |
+ self.navigationController = navigationController |
|
| 27 |
+ listenDeallocate() |
|
| 28 |
+ } |
|
| 29 |
+ |
|
| 22 | 30 |
private func store<T>(coordinator: BaseCoordinator<T>) {
|
| 23 | 31 |
childCoordinators[coordinator.identifier] = coordinator |
| 24 | 32 |
} |
@@ -38,4 +46,13 @@ public class BaseCoordinator<ResultType> {
|
||
| 38 | 46 |
func start() -> Observable<ResultType> {
|
| 39 | 47 |
fatalError("Start method should be implemented.")
|
| 40 | 48 |
} |
| 49 |
+ |
|
| 50 |
+ func listenDeallocate() {
|
|
| 51 |
+ navigationController.rx.willShow.subscribe(onNext: {[weak self] (_, _) in
|
|
| 52 |
+ guard let `self` = self else { return }
|
|
| 53 |
+ if !self.navigationController.viewControllers.contains(self.viewController) {
|
|
| 54 |
+ self.didCancel.onNext(()) |
|
| 55 |
+ } |
|
| 56 |
+ }).disposed(by: disposeBag) |
|
| 57 |
+ } |
|
| 41 | 58 |
} |
@@ -0,0 +1,40 @@ |
||
| 1 |
+// |
|
| 2 |
+// UIViewController+Rx.swift |
|
| 3 |
+// Paiai_iOS |
|
| 4 |
+// |
|
| 5 |
+// Created by ffib on 2019/5/6. |
|
| 6 |
+// Copyright © 2019 FFIB. All rights reserved. |
|
| 7 |
+// |
|
| 8 |
+ |
|
| 9 |
+import UIKit |
|
| 10 |
+import RxCocoa |
|
| 11 |
+import RxSwift |
|
| 12 |
+ |
|
| 13 |
+public extension Reactive where Base: UIViewController {
|
|
| 14 |
+ var viewDidLoad: ControlEvent<Void> {
|
|
| 15 |
+ let source = self.methodInvoked(#selector(Base.viewDidLoad)).map { _ in }
|
|
| 16 |
+ return ControlEvent(events: source) |
|
| 17 |
+ } |
|
| 18 |
+ |
|
| 19 |
+ var viewWillAppear: ControlEvent<Bool> {
|
|
| 20 |
+ let source = self.methodInvoked(#selector(Base.viewWillAppear)) |
|
| 21 |
+ .map { $0.first as? Bool ?? false }
|
|
| 22 |
+ return ControlEvent(events: source) |
|
| 23 |
+ } |
|
| 24 |
+ var viewDidAppear: ControlEvent<Bool> {
|
|
| 25 |
+ let source = self.methodInvoked(#selector(Base.viewDidAppear)) |
|
| 26 |
+ .map { $0.first as? Bool ?? false }
|
|
| 27 |
+ return ControlEvent(events: source) |
|
| 28 |
+ } |
|
| 29 |
+ |
|
| 30 |
+ var viewWillDisappear: ControlEvent<Bool> {
|
|
| 31 |
+ let source = self.methodInvoked(#selector(Base.viewWillDisappear)) |
|
| 32 |
+ .map { $0.first as? Bool ?? false }
|
|
| 33 |
+ return ControlEvent(events: source) |
|
| 34 |
+ } |
|
| 35 |
+ var viewDidDisappear: ControlEvent<Bool> {
|
|
| 36 |
+ let source = self.methodInvoked(#selector(Base.viewDidDisappear)) |
|
| 37 |
+ .map { $0.first as? Bool ?? false }
|
|
| 38 |
+ return ControlEvent(events: source) |
|
| 39 |
+ } |
|
| 40 |
+} |