|
//
// Route.swift
// PaiAi
//
// Created by FFIB on 2017/9/1.
// Copyright © 2017年 yb. All rights reserved.
//
import UIKit
enum NavigationMode {
case push
case present
}
struct RouteModel {
var ctl: UIViewController.Type
var storyboard: (name: String, identifier: String)?
}
struct Route {
var share: Route {
return Route()
}
init() { }
var routeScheme = [String: RouteModel]()
mutating func register(url: String, model: RouteModel) {
routeScheme[url] = model
}
mutating func remove(url: String) {
routeScheme.removeValue(forKey: url)
}
mutating func removeAll() {
routeScheme.removeAll()
}
func canOpen(url: String) -> Bool {
return routeScheme[url] != nil
}
func open(url: String, mode: NavigationMode) {
guard let model = routeScheme[url], let visibleController = UIApplication.shared.keyWindow?.rootViewController else {
return
}
var ctl: UIViewController {
if let storyboard = model.storyboard {
return UIStoryboard(name: storyboard.name, bundle: nil).instantiateViewController(withIdentifier: storyboard.identifier)
} else {
return model.ctl.init()
}
}
switch mode {
case .present:
visibleController.present(ctl, animated: true, completion: nil)
case .push:
visibleController.show(ctl, sender: nil)
}
}
}
|