Skip to content

Commit fef4fc0

Browse files
author
wxlpp
committed
feat(All): 路由时支持附加一个对象
1 parent 51b1b2c commit fef4fc0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

72 files changed

+810
-350
lines changed

.jazzy.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ module: UIRouter
22
sdk: iphone
33
swift_build_tool: xcodebuild
44
podspec: UIRouter.podspec
5-
module_version: 1.0
5+
module_version: 0.2.0.alpha
66
clean: true
77
theme: theme/fullwidth

Example/Podfile.lock

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
PODS:
22
- Submodule (0.1.0):
33
- UIRouter
4-
- UIRouter (0.2.0.alpha)
5-
- UIRouter/Web (0.2.0.alpha)
4+
- UIRouter (0.2.0.alpha):
5+
- UIRouter/Core (= 0.2.0.alpha)
6+
- UIRouter/Core (0.2.0.alpha)
7+
- UIRouter/Web (0.2.0.alpha):
8+
- UIRouter/Core
69

710
DEPENDENCIES:
811
- Submodule (from `Submodule/`)
@@ -16,7 +19,7 @@ EXTERNAL SOURCES:
1619

1720
SPEC CHECKSUMS:
1821
Submodule: 4d6c30b294c43d33503985e348909fd7c4c02353
19-
UIRouter: 57c6fd4e66a174927134a6d962a9452c6e17fc12
22+
UIRouter: f0fadcc043f620b846aa1feee97ea8803127f27f
2023

2124
PODFILE CHECKSUM: 5387537adb0879783c29a9e30e3acc9630837f78
2225

Example/Submodule/Source/SubViewController.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ public final class SubViewController: UIViewController {
2323
// MARK: 路由
2424

2525
extension SubViewController: Routable {
26-
public static func route(parameters: RouterParameters, completion: @escaping RouteCompletionHandler<SubViewController>) {
26+
27+
public static func route(parameters: RouterParameters, object: Any?, completion: @escaping RouteCompletionHandler<SubViewController>) {
2728
completion(.success(SubViewController()))
2829
}
2930

Example/Submodule/UserProfileViewController.swift

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

1515
extension UserProfileViewController: Routable {
16+
1617
public static var paths: [String] {
1718
["user/profile/:id"]
1819
}
19-
20-
public static func route(parameters: RouterParameters, completion: @escaping RouteCompletionHandler<UserProfileViewController>) {
20+
21+
public static func route(parameters: RouterParameters, object: Any?, completion: @escaping RouteCompletionHandler<UserProfileViewController>) {
2122
if let userID: String = parameters.get("userID") {
2223
completion(.success(UserProfileViewController()))
2324
} else {

Example/shell.xcodeproj/project.pbxproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
926010144793C5C8C345B1D4 /* Pods_ExampleTests.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_ExampleTests.framework; sourceTree = BUILT_PRODUCTS_DIR; };
2828
A3B75E7330C33D887776A83C /* Pods-ExampleTests.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-ExampleTests.release.xcconfig"; path = "Target Support Files/Pods-ExampleTests/Pods-ExampleTests.release.xcconfig"; sourceTree = "<group>"; };
2929
A4119074813D4DEC57006AEB /* Pods_Example_ExampleUITests.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_Example_ExampleUITests.framework; sourceTree = BUILT_PRODUCTS_DIR; };
30-
AFB4C5D2264003490018229B /* UIRouter.podspec */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; name = UIRouter.podspec; path = ../UIRouter.podspec; sourceTree = "<group>"; };
30+
AFB4C5D2264003490018229B /* UIRouter.podspec */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; name = UIRouter.podspec; path = ../UIRouter.podspec; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.ruby; };
3131
AFF87535263D28E000234826 /* shell.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = shell.app; sourceTree = BUILT_PRODUCTS_DIR; };
3232
AFF87538263D28E000234826 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
3333
AFF8753A263D28E000234826 /* SceneDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SceneDelegate.swift; sourceTree = "<group>"; };

Example/shell/ErrorDetailsViewController.swift

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,43 +9,41 @@ import UIKit
99

1010
class ErrorDetailsViewController: UITableViewController {
1111
let error: Error
12-
let errorDetails: [(key: String,value: String)]
12+
let errorDetails: [(key: String, value: String)]
1313

1414
init(error: Error) {
1515
self.error = error
1616
if let localizedError = error as? LocalizedError {
17-
var list: [(key: String,value: String)] = []
17+
var list: [(key: String, value: String)] = []
1818
if let errorDescription = localizedError.errorDescription {
19-
list.append(("简述",errorDescription))
19+
list.append(("简述", errorDescription))
2020
}
2121
if let failureReason = localizedError.failureReason {
22-
list.append(("原因",failureReason))
23-
22+
list.append(("原因", failureReason))
2423
}
2524
if let helpAnchor = localizedError.helpAnchor {
26-
list.append(("解决",helpAnchor))
27-
25+
list.append(("解决", helpAnchor))
2826
}
2927
if let recoverySuggestion = localizedError.recoverySuggestion {
30-
list.append(("恢复",recoverySuggestion))
31-
28+
list.append(("恢复", recoverySuggestion))
3229
}
33-
errorDetails = list
34-
}else {
35-
errorDetails = [("错误",error.localizedDescription)]
30+
self.errorDetails = list
31+
} else {
32+
self.errorDetails = [("错误", error.localizedDescription)]
3633
}
3734
super.init(style: .insetGrouped)
3835
}
3936

37+
@available(*, unavailable)
4038
required init?(coder: NSCoder) {
4139
fatalError("init(coder:) has not been implemented")
4240
}
43-
41+
4442
override func viewWillAppear(_ animated: Bool) {
4543
super.viewWillAppear(animated)
46-
self.title = "错误信息"
44+
title = "错误信息"
4745
}
48-
46+
4947
override func viewDidLoad() {
5048
super.viewDidLoad()
5149
tableView.allowsSelection = false

Example/shell/MainViewController.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@
88
import UIKit
99
import UIRouter
1010
class MainViewController: UITableViewController {
11-
let routes = [(name: "SubModule", path: "sub"),
12-
(name: "Baidu", path: "https://www.baidu.com"),
13-
(name: "用户简介", path: "user/profile")]
11+
let routes = [
12+
(name: "SubModule", path: "sub"),
13+
(name: "Baidu", path: "https://www.baidu.com"),
14+
(name: "用户简介", path: "user/profile")
15+
]
1416

1517
override func viewDidLoad() {
1618
super.viewDidLoad()

Example/shell/RouteErrorHandler.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,5 @@ class RouteErrorHandler: RouteErrorHandling {
1717
#endif
1818
}
1919

20-
func handleCustomError(_ error: Error) {
21-
22-
}
20+
func handleCustomError(_ error: Error) {}
2321
}

README.md

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,19 +54,17 @@ public final class UserProfileViewController: UIViewController {}
5454
// MARK: 路由
5555

5656
extension UserProfileViewController: Routable {
57-
public static func route(parameters: RouterParameters, completion: @escaping RouteCompletionHandler<UserProfileViewController>) {
58-
// 对参数进行验证,成功返回 ViewController,失败则返回错误,
59-
// 这里返回的是参数验证错误,也可以返回业务自定义错误。
60-
if let userID: String = parameters.get("name") {
57+
public static var paths: [String] {
58+
["user/profile/:id"]
59+
}
60+
61+
public static func route(parameters: RouterParameters, object: Any?, completion: @escaping RouteCompletionHandler<UserProfileViewController>) {
62+
if let userID: String = parameters.get("userID") {
6163
completion(.success(UserProfileViewController()))
6264
} else {
63-
completion(.failure(RouteError.parameterValidationFailed(url: path, name: "name")))
65+
completion(.failure(RouteError.parameterValidationFailed(vcType: Self.self, name: "userID")))
6466
}
6567
}
66-
67-
public static var path: String {
68-
"user/profile/:id"
69-
}
7068
}
7169
```
7270
### 页面路由

Source/Core/Routable.swift

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import UIKit
1010

1111
public protocol RouteBase: UIViewController {
1212
static var paths: [String] { get }
13-
static func routeVC(parameters: RouterParameters, completion: @escaping RouteCompletionHandler<UIViewController>)
13+
static func routeVC(parameters: RouterParameters, object: Any?, completion: @escaping RouteCompletionHandler<UIViewController>)
1414
}
1515

1616
/// 需要通过路由解耦的 `UIViewController` 实现此协议后,路由中心将会自动完成组件的注册
@@ -19,13 +19,14 @@ public protocol Routable: RouteBase {
1919
/// 路由中心通过路由链接到指定 `UIViewController` 后由此回调进行页面构造
2020
/// - Parameters:
2121
/// - parameters: 由路由 `URL` 解析后传递的参数
22+
/// - object: 业务对象
2223
/// - completion: 验证参数后构造页面通过回调返回结果
23-
static func route(parameters: RouterParameters, completion: @escaping RouteCompletionHandler<Self>)
24+
static func route(parameters: RouterParameters, object: Any?, completion: @escaping RouteCompletionHandler<Self>)
2425
}
2526

2627
public extension Routable {
27-
static func routeVC(parameters: RouterParameters, completion: @escaping RouteCompletionHandler<UIViewController>) {
28-
route(parameters: parameters) { result in
28+
static func routeVC(parameters: RouterParameters, object: Any?, completion: @escaping RouteCompletionHandler<UIViewController>) {
29+
route(parameters: parameters, object: object) { result in
2930
completion(result.map { $0 as UIViewController })
3031
}
3132
}

0 commit comments

Comments
 (0)