Skip to content

Instantly share code, notes, and snippets.

@susieyy
Created January 14, 2017 02:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save susieyy/f8e840e573996e0bda4ff0255f1f70e7 to your computer and use it in GitHub Desktop.
Save susieyy/f8e840e573996e0bda4ff0255f1f70e7 to your computer and use it in GitHub Desktop.
#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