Skip to content

Commit 0f925c2

Browse files
author
wxlpp
committed
feat(All): 支持单页面多路由
1 parent 985b3d9 commit 0f925c2

File tree

8 files changed

+54
-42
lines changed

8 files changed

+54
-42
lines changed

Example/Podfile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
platform :ios, '9.0'
22
inhibit_all_warnings!
3-
install! 'cocoapods', :generate_multiple_pod_projects => true, :incremental_installation => true
3+
install! 'cocoapods',
4+
:generate_multiple_pod_projects => true,
5+
:incremental_installation => true,
6+
:warn_for_unused_master_specs_repo => false
47

58
target 'shell' do
69
use_frameworks!

Example/Podfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@ SPEC CHECKSUMS:
1717
Submodule: 4d6c30b294c43d33503985e348909fd7c4c02353
1818
UIRouter: cc1486c13af569df95ed9a6c0dfb0b8e5532ec65
1919

20-
PODFILE CHECKSUM: 6cbdf15ab32343a1b20c502fe96b51208e44cc80
20+
PODFILE CHECKSUM: ba24c8ebfe1e7f5e3aea87b3cd76d9bb52a9c99e
2121

2222
COCOAPODS: 1.10.1

Example/Submodule/Source/SubViewController.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ extension SubViewController: Routable {
2727
completion(.success(SubViewController()))
2828
}
2929

30-
public static var path: String {
31-
"sub"
30+
public static var paths: [String] {
31+
["sub"]
3232
}
3333
}

Example/Submodule/UserProfileViewController.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@ public final class UserProfileViewController: UIViewController {}
1313
// MARK: 路由
1414

1515
extension UserProfileViewController: Routable {
16+
public static var paths: [String] {
17+
["user/profile/:id"]
18+
}
19+
1620
public static func route(parameters: RouterParameters, completion: @escaping RouteCompletionHandler<UserProfileViewController>) {
1721
if let userID: String = parameters.get("userID") {
1822
completion(.success(UserProfileViewController()))
1923
} else {
20-
completion(.failure(RouteError.parameterValidationFailed(url: path, name: "userID")))
24+
completion(.failure(RouteError.parameterValidationFailed(vcType: Self.self, name: "userID")))
2125
}
2226
}
23-
24-
public static var path: String {
25-
"user/profile/:id"
26-
}
2727
}

Example/shell/MainViewController.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,5 @@ class MainViewController: UITableViewController {
3232
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
3333
tableView.deselectRow(at: indexPath, animated: true)
3434
UIApplication.shared.route(url: routes[indexPath.row].path).push()
35-
UIApplication.shared.route(url: routes[indexPath.row].path).presentWithNavigationController(UINavigationController.self)
3635
}
3736
}

Routable.swift

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//
2+
// Routable.swift
3+
// UIRouter
4+
//
5+
// Created by wxlpp on 2021/5/3.
6+
// Copyright © 2021 Wang Xiaolong. All rights reserved.
7+
//
8+
9+
import UIKit
10+
11+
public protocol RouteBase: UIViewController {
12+
static var paths: [String] { get }
13+
static func routeVC(parameters: RouterParameters, completion: @escaping RouteCompletionHandler<UIViewController>)
14+
}
15+
16+
/// 需要通过路由解耦的 `UIViewController` 实现此协议后,路由中心将会自动完成组件的注册
17+
public protocol Routable: RouteBase {
18+
19+
/// 路由中心通过路由链接到指定 `UIViewController` 后由此回调进行页面构造
20+
/// - Parameters:
21+
/// - parameters: 由路由 `URL` 解析后传递的参数
22+
/// - completion: 验证参数后构造页面通过回调返回结果
23+
static func route(parameters: RouterParameters, completion: @escaping RouteCompletionHandler<Self>)
24+
}
25+
26+
public extension Routable {
27+
static func routeVC(parameters: RouterParameters, completion: @escaping RouteCompletionHandler<UIViewController>) {
28+
route(parameters: parameters) { result in
29+
completion(result.map { $0 as UIViewController })
30+
}
31+
}
32+
}

Source/RouteError.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
// Created by wxlpp on 2021/5/1.
66
//
77

8-
import Foundation
8+
import UIKit
99

1010
/// 路由错误
1111
public enum RouteError {
@@ -16,7 +16,7 @@ public enum RouteError {
1616
/// 路由终点页面未注册
1717
case routeDoesNotExist(url: String)
1818
/// 参数验证失败
19-
case parameterValidationFailed(url: String, name: String)
19+
case parameterValidationFailed(vcType: UIViewController.Type, name: String)
2020
}
2121

2222
// MARK: - LocalizedError
@@ -32,8 +32,8 @@ extension RouteError: LocalizedError {
3232
return "路由失败 路径[\(url)]"
3333
case .routeDoesNotExist(url: let url):
3434
return "路由失败 路径[\(url)]"
35-
case .parameterValidationFailed(url: let url, name: let name):
36-
return "路由参数验证失败 路径[\(url)] 参数[\(name)]"
35+
case .parameterValidationFailed(vcType: let type, name: let name):
36+
return "路由参数验证失败 页面[\(type)] 参数[\(name)]"
3737
}
3838
}
3939

@@ -46,7 +46,7 @@ extension RouteError: LocalizedError {
4646
return "尝试注册路由但地址已存在"
4747
case .routeDoesNotExist:
4848
return "路由终点页面未注册"
49-
case .parameterValidationFailed(url: _, name: let name):
49+
case .parameterValidationFailed(vcType: _, name: let name):
5050
return "未能获取参数[\(name)]"
5151
}
5252
}
Lines changed: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,12 @@
11
//
2-
// Router.swift
2+
// UIViewControllerRouter.swift
33
//
44
// Created by wxlpp on 2021/5/1.
55
// Copyright © 2021 Wang Xiaolong. All rights reserved.
66
//
77

88
import UIKit
99

10-
public protocol RouteBase: UIViewController {
11-
static var path: String { get }
12-
static func routeVC(parameters: RouterParameters, completion: @escaping RouteCompletionHandler<UIViewController>)
13-
}
14-
15-
/// 需要通过路由解耦的 `UIViewController` 实现此协议后,路由中心将会自动完成组件的注册
16-
public protocol Routable: RouteBase {
17-
18-
/// 路由中心通过路由链接到指定 `UIViewController` 后由此回调进行页面构造
19-
/// - Parameters:
20-
/// - parameters: 由路由 `URL` 解析后传递的参数
21-
/// - completion: 验证参数后构造页面通过回调返回结果
22-
static func route(parameters: RouterParameters, completion: @escaping RouteCompletionHandler<Self>)
23-
}
24-
25-
public extension Routable {
26-
static func routeVC(parameters: RouterParameters, completion: @escaping RouteCompletionHandler<UIViewController>) {
27-
route(parameters: parameters) { result in
28-
completion(result.map { $0 as UIViewController })
29-
}
30-
}
31-
}
32-
33-
3410
/// 一个路由器可以通过解析链接获得一个指定的`UIViewController`实例
3511
public class UIViewControllerRouter {
3612
private var isInitialized = false
@@ -91,8 +67,10 @@ public class UIViewControllerRouter {
9167
for i in 0 ..< numberOfClasses {
9268
let item: AnyClass = classesPtr[i]
9369
if let vcType = item as? RouteBase.Type {
94-
try? urlRouter.register(route: vcType.path) { parameters, completion in
95-
vcType.routeVC(parameters: parameters, completion: completion)
70+
for path in vcType.paths {
71+
try? urlRouter.register(route: path) { parameters, completion in
72+
vcType.routeVC(parameters: parameters, completion: completion)
73+
}
9674
}
9775
}
9876
}

0 commit comments

Comments
 (0)