Created
September 19, 2017 07:55
-
-
Save oozoofrog/146bf98ea80967d4ccadb2179f9b1714 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| final class RoutingModule { | |
| weak var rootViewController: UIViewController? | |
| var navigationController: UINavigationController? { | |
| if let navigation = self.rootViewController as? UINavigationController { | |
| return navigation | |
| } else { | |
| return self.rootViewController?.navigationController | |
| } | |
| } | |
| var presentDisposeBag: DisposeBag = DisposeBag() | |
| var presents: [PresentContainer] = [] | |
| init(rootViewController: UIViewController?) { | |
| self.rootViewController = rootViewController | |
| } | |
| func push(viewController: UIViewController) { | |
| self.navigationController?.pushViewController(viewController, animated: true) | |
| } | |
| func present(viewController: UIViewController, | |
| wrapWithNavigation: Bool = false, | |
| overContext: Bool = false) { | |
| let presentedViewController = wrapWithNavigation ? NavigationController(rootViewController: viewController) : viewController | |
| let presentedContainer = PresentContainer(viewController: presentedViewController) { (presenting, presented) in | |
| if overContext { | |
| presenting.providesPresentationContextTransitionStyle = true | |
| presenting.definesPresentationContext = true | |
| presented.modalPresentationStyle = .overCurrentContext | |
| } | |
| } | |
| self.presents.append(presentedContainer) | |
| self.present() | |
| } | |
| func alert(title: String?, message: String, actions: [UIAlertAction]) { | |
| let alert = UIAlertController(title: title, message: message, preferredStyle: .alert) | |
| for action in actions { | |
| alert.addAction(action) | |
| } | |
| self.present(viewController: alert) | |
| } | |
| func actionSheet(title: String?, message: String, actions: [UIAlertAction]) { | |
| } | |
| private func popFirstPresent() -> PresentContainer? { | |
| guard self.presents.isEmpty == false else { return nil } | |
| if let first = self.presents.first { | |
| self.presents.removeFirst() | |
| return first | |
| } | |
| return nil | |
| } | |
| private func present() { | |
| guard let root = self.rootViewController, root.presentedViewController == nil else { return } | |
| if let presented = self.popFirstPresent() { | |
| presented.configuration(root, presented.viewController) | |
| root.present(presented.viewController, animated: true, completion: nil) | |
| presented.viewController.rx | |
| .deallocated | |
| .subscribe(onCompleted: { [weak self] in | |
| self?.present() | |
| }) | |
| .disposed(by: self.presentDisposeBag) | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment