-
-
Save susieyy/f8e840e573996e0bda4ff0255f1f70e7 to your computer and use it in GitHub Desktop.
#swtws
This file contains 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
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