Skip to content

Instantly share code, notes, and snippets.

@susieyy susieyy/Routing.swift Secret
Created Jan 14, 2017

Embed
What would you like to do?
#swtws
enum RouteType {
case notMatch
case profile(userId: Int)
case settings
case http(url: NSURL)
init(URL url: NSURL) {
guard let scheme = url.scheme else { self = .notMatch; return }
switch scheme {
case "http", "https":
self = .http(url: url)
case Global.appScheme:
self = self.dynamicType.appScheme(url)
default:
self = .notMatch
}
}
private static func appScheme(url: NSURL) -> RouteType {
guard let components = NSURLComponents(URL: url, resolvingAgainstBaseURL: false)
, let host = components.host
, let paths = url.pathComponents else {
return .notMatch
}
switch host {
case "profile":
guard paths.count >= 1 else { return .notMatch }
return .profile(userId: paths[1])
case "settings":
return .settings
default:
return .notMatch
}
}
}
struct Router {
// 遷移処理はRxSwiftのObservableを返すことで非同期でも行えるようにしています
func execute(routeType: RouteType) -> Observable<Void> {
switch routeType {
case .notMatch: break
case .profile(let userId):
return presentProfile(userId)
case .settings:
return presentSettings()
case .http(let url):
return presentWeb(url)
}
return Observable.empty()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.