// // Navigator.swift // Lookfit // // Created by lemo. on 2020/3/7. // Copyright © 2020 Sheldon. All rights reserved. // import Foundation import UIKit import RxSwift import RxCocoa protocol Navigatable { var navigator: Navigator! { get set } } class Navigator { static var `default` = Navigator() // MARK: - segues list, all app scenes enum Scene { case tabs(viewModel: HomeTabBarViewModel) case guideUserInfo case binding(viewModel: BindingViewModels) case notice(viewModel: NoticeViewModels) case heartRateCheck(viewModel: HeartRateCheckViewModels) case notDisturb(viewModel: NotDisturbViewModels) case drinkRemind(viewModel: DrinkRemindViewModels) case repeatSet(viewModel: RepeatViewModels) case sedentary(viewModel: SedentaryViewModels) case alarmList(viewModel: NewAlarmListViewModel) case addAlarm(viewModel: NewAddAlarmViewModel) case about case target(viewModel: TargetVM) case unitSet case userInfo(viewModel: UserInfoViewModel) case step(viewModel: StepViewModels) case sleep(viewModel: SleepViewModels) case heartRate(viewModel: HRViewModels) case sportDetail(viewModel: SportDetailViewModels) case firmwareUpdate(viewModel: FirmwareUpdateViewModels) case dialPush(viewModel: DialPushViewModels) case temperatureCheck(viewModel: TemperarureCheckViewModels) case temperature(viewModel: TemperatureViewModels) case wechatSport(viewModel: WeChatSportViewModelss) case bloodPressure(viewModel: BloodPressureViewModels) case bloodOxygen(viewModel: BloodOxygenViewModels) case telephonebook(ViewModel: TelephoneBookViewModels) case businesscard(ViewModel: businessCardViewModels) case theWallet(ViewModel: PaymentCodeViewModels) case upDataQRCodeImg(ViewModel:UpdataQRCodeImgViewModels) } enum Transition { case root(in: UIWindow) case navigation case modal } // MARK: - get a single VC func get(segue: Scene) -> UIViewController? { switch segue { case .tabs(let viewModel): let rootVC = HomeTabBarController(viewModel: viewModel, navigator: self) return rootVC case .guideUserInfo: let guideVC = GuideUserInfoViewController(viewModel: nil, navigator: self) guideVC.modalPresentationStyle = .fullScreen return guideVC case .binding(let viewModel): let bindingVC = BindingViewControllers(viewModel: viewModel, navigator: self) bindingVC.navigationTitle = MultiLanguageKey.binding.localized return bindingVC case .notice(let viewModel): let noticeVC = NoticeViewConrtollers(viewModel: viewModel, navigator: self) noticeVC.navigationTitle = MultiLanguageKey.notice.localized return noticeVC case .heartRateCheck(let viewModel): let heartRateCheckVC = HeartRateMonitorViewControllers(viewModel: viewModel, navigator: self) heartRateCheckVC.navigationTitle = MultiLanguageKey.heartCheck.localized return heartRateCheckVC case .notDisturb(let viewModel): let notDisturbVC = NotDisturbViewControllers(viewModel: viewModel, navigator: self) notDisturbVC.navigationTitle = MultiLanguageKey.notDisturb.localized return notDisturbVC case .drinkRemind(let viewModel): let drinkRemindVC = DrinkRemindViewControllers(viewModel: viewModel, navigator: self) drinkRemindVC.navigationTitle = MultiLanguageKey.drinkRemind.localized return drinkRemindVC case .repeatSet(let viewModel): let repeatTimeVC = RepeatTimeViewControllers(viewModel: viewModel, navigator: self) repeatTimeVC.navigationTitle = MultiLanguageKey.repeatCycle.localized return repeatTimeVC case .sedentary(let viewModel): let sedentaryVC = SedentaryViewControllers(viewModel: viewModel, navigator: self) sedentaryVC.navigationTitle = MultiLanguageKey.sedentary.localized return sedentaryVC case .alarmList(let viewModel): let newAlarmVC = NewAlarmViewController(viewModel: viewModel, navigator: self) newAlarmVC.navigationTitle = MultiLanguageKey.alarmClock.localized return newAlarmVC case .addAlarm(let viewModel): let newAddAlarmVC = NewAddAlarmVC(viewModel: viewModel, navigator: self) newAddAlarmVC.navigationTitle = viewModel.isAdd ? MultiLanguageKey.addAlarm.localized : MultiLanguageKey.editAlarm.localized return newAddAlarmVC case .about: let vc = AboutVC(viewModel: nil, navigator: self) vc.navigationTitle = MultiLanguageKey.about.localized return vc case .target(let viewModel): let vc = TargetVC(viewModel: viewModel, navigator: self) vc.navigationTitle = MultiLanguageKey.targetSet.localized return vc case .unitSet: let vc = UnitSetVC(viewModel: nil, navigator: self) vc.navigationTitle = MultiLanguageKey.unitSet.localized return vc case .userInfo(let viewModel): let vc = UserInfoViewController(viewModel: viewModel, navigator: self) vc.navigationTitle = MultiLanguageKey.mineInfo.localized return vc case .step(let viewModel): let vc = StepViewControllers(viewModel: viewModel, navigator: self) vc.navigationTitle = MultiLanguageKey.step.localized return vc case .sleep(let viewModel): let vc = SleepViewControllers(viewModel: viewModel, navigator: self) vc.navigationTitle = MultiLanguageKey.sleep.localized return vc case .heartRate(let viewModel): let vc = HRViewControllers(viewModel: viewModel, navigator: self) vc.navigationTitle = MultiLanguageKey.heartRate.localized return vc case .sportDetail(let viewModel): let vc = SportDetailViewControllers(viewModel: viewModel, navigator: self) return vc case .firmwareUpdate(let viewModel): let vc = FirmwareUpdateViewControllers(viewModel: viewModel, navigator: self) vc.navigationTitle = MultiLanguageKey.firmwareUpgrade.localized return vc case .dialPush(let viewModel): let vc = DialPushViewControllers(viewModel: viewModel, navigator: self) vc.navigationTitle = MultiLanguageKey.dialPush.localized return vc case .temperatureCheck(let viewModel): let vc = TemperarureCheckViewControllers(viewModel: viewModel, navigator: self) vc.navigationTitle = MultiLanguageKey.temperatureCheck.localized return vc case .temperature(let viewModel): let vc = TemperatureViewControllers(viewModel: viewModel, navigator: self) vc.navigationTitle = MultiLanguageKey.temperature.localized return vc case .wechatSport(let viewModel): let vc = WeChatSportViewControllerss(viewModel: viewModel, navigator: self) vc.navigationTitle = MultiLanguageKey.wechatSport.localized return vc case .bloodPressure(let viewModel): let vc = BloodPressureViewControllers(viewModel: viewModel, navigator: self) vc.navigationTitle = MultiLanguageKey.bloodPressure.localized return vc case .bloodOxygen(let viewModel): let vc = BloodOxygenViewControllers(viewModel: viewModel, navigator: self) vc.navigationTitle = MultiLanguageKey.bloodOxygen.localized return vc case .telephonebook(let viewModel): let vc = TelephoneBookViewControllers(viewModel: viewModel, navigator: self) vc.navigationTitle = MultiLanguageKey.telephoneBook.localized return vc case .theWallet(let viewModel): let vc = PaymentCodeViewControllers(viewModel: viewModel, navigator: self) vc.navigationTitle = MultiLanguageKey.theWallet.localized return vc case .businesscard(let viewModel): let vc = businessCardViewControllers(viewModel: viewModel, navigator: self) vc.navigationTitle = MultiLanguageKey.businessCard.localized return vc case .upDataQRCodeImg(let viewModel): let vc = UpdataQRCodeImgViewControllers(viewModel: viewModel, navigator: self) vc.navigationTitle = viewModel.titleName return vc } } func pop(sender: UIViewController?, toRoot: Bool = false) { if toRoot { sender?.navigationController?.popToRootViewController(animated: true) } else { sender?.navigationController?.popViewController(animated: true) } } func dismiss(sender: UIViewController?) { sender?.navigationController?.dismiss(animated: true, completion: nil) } // MARK: - invoke a single segue func show(segue: Scene, sender: UIViewController?, transition: Transition = .navigation) { if let target = get(segue: segue) { show(target: target, sender: sender, transition: transition) } } private func show(target: UIViewController, sender: UIViewController?, transition: Transition) { switch transition { case .root(in: let window): window.rootViewController = target window.makeKeyAndVisible() return default: break } guard let sender = sender else { fatalError("You need to pass in a sender for .navigation or .modal transitions") } if let nav = sender as? UINavigationController { //push root controller on navigation stack nav.pushViewController(target, animated: false) return } switch transition { case .navigation: if let nav = sender.navigationController { // push controller to navigation stack nav.pushViewController(target, animated: true) } case .modal: // present modally DispatchQueue.main.async { let nav = NavigationController(rootViewController: target) nav.modalPresentationStyle = target.modalPresentationStyle sender.present(nav, animated: true, completion: nil) } default: break } } }