Skip to content

Commit 4d27fd3

Browse files
committed
commit: update
1 parent 004df77 commit 4d27fd3

20 files changed

+458
-284
lines changed

Example/ModuleRouteExample/AppDelegate.swift

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,13 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
2020

2121
private func setupRoute() {
2222

23-
navigator.register(module: DetailModule())
2423
navigator.register(dependencyFactory: {
25-
ServiceA()
26-
}, forType: ServiceAInterface.self)
24+
DetailModule()
25+
}, forType: DetailInterface.self)
26+
navigator.register(dependencyFactory: {
27+
ChatModule()
28+
}, forType: ChatInterface.self)
29+
2730
}
2831

2932

Example/ModuleRouteExample/AppRouteFacotry.swift

Lines changed: 0 additions & 25 deletions
This file was deleted.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//
2+
// ChatModule.swift
3+
// ModuleRouteExample
4+
//
5+
// Created by GIKI on 2025/2/15.
6+
//
7+
8+
import UIKit
9+
import ModuleRoute
10+
11+
protocol ChatInterface: MRModuleInterface {
12+
13+
}
14+
15+
class ChatModule: MRModule {
16+
static var supportedRoutes: [MRRoute.Type] = [
17+
ChatRoute.self
18+
]
19+
20+
public init() {}
21+
22+
public func handle(route: MRRoute) -> RouteResult {
23+
// 根据具体路由做出响应
24+
switch route {
25+
case is DetailRoute:
26+
let detail = DetailViewController()
27+
return .navigator(detail)
28+
default:
29+
return .none
30+
}
31+
}
32+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//
2+
// ChatViewController.swift
3+
// ModuleRouteExample
4+
//
5+
// Created by GIKI on 2025/2/15.
6+
//
7+
8+
import UIKit
9+
10+
class ChatViewController: UIViewController {
11+
12+
override func viewDidLoad() {
13+
super.viewDidLoad()
14+
15+
// Do any additional setup after loading the view.
16+
}
17+
18+
19+
/*
20+
// MARK: - Navigation
21+
22+
// In a storyboard-based application, you will often want to do a little preparation before navigation
23+
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
24+
// Get the new view controller using segue.destination.
25+
// Pass the selected object to the new view controller.
26+
}
27+
*/
28+
29+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//
2+
// DeaiViewController.swift
3+
// ModuleRouteExample
4+
//
5+
// Created by GIKI on 2025/2/15.
6+
//
7+
8+
import UIKit
9+
10+
// MARK: - Detail View Controller
11+
class DetailViewController: UIViewController {
12+
var item: ItemModel?
13+
14+
override func viewDidLoad() {
15+
super.viewDidLoad()
16+
view.backgroundColor = .white
17+
setupUI()
18+
}
19+
20+
private func setupUI() {
21+
let label = UILabel()
22+
label.text = item?.title
23+
label.textAlignment = .center
24+
label.frame = CGRect(x: 0, y: 0, width: 200, height: 50)
25+
label.center = view.center
26+
view.addSubview(label)
27+
}
28+
}
29+

Example/ModuleRouteExample/DetailModule.swift renamed to Example/ModuleRouteExample/Detail/DetailModule.swift

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,16 @@ import Foundation
99
import ModuleRoute
1010
import UIKit
1111

12-
class DetailModule: MRModule {
12+
protocol DetailInterface: MRModuleInterface {
13+
14+
}
1315

14-
var supportedRoutes: [MRRoute.Type] {
15-
return [
16-
DetailRoute.self
17-
]
18-
}
16+
class DetailModule: MRModule {
1917

18+
static var supportedRoutes: [MRRoute.Type] = [
19+
DetailRoute.self
20+
]
21+
2022

2123
public init() {}
2224

@@ -25,7 +27,7 @@ class DetailModule: MRModule {
2527
switch route {
2628
case is DetailRoute:
2729
let detail = DetailViewController()
28-
return .viewController(detail)
30+
return .navigator(detail)
2931
default:
3032
return .none
3133
}

Example/ModuleRouteExample/DetailRoute.swift

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,18 @@ import Foundation
99
import ModuleRoute
1010

1111
struct DetailRoute: MRRoute {
12-
static var name: String = "detail"
12+
var params: [String : Any] = [:]
1313

14-
public var parameters: [String: Any] = [:]
14+
var callback: ((Any?) -> Void)?
1515

16-
init(parameters: [String: Any]) {
17-
self.parameters = parameters
18-
}
16+
static var name: String = "detail"
17+
}
1918

19+
20+
struct ChatRoute: MRRoute {
21+
var params: [String : Any] = [:]
22+
23+
var callback: ((Any?) -> Void)?
24+
25+
static var name: String = "chat"
2026
}

Example/ModuleRouteExample/ViewController.swift

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ import ModuleRoute
1111
class ViewController: UIViewController {
1212

1313
@MRInject var navigator: MRNavigator
14-
@MRInject var alertService: ServiceAInterface
15-
1614
// MARK: - Properties
1715
private var collectionView: UICollectionView!
1816
private let cellIdentifier = "Cell"
@@ -53,6 +51,7 @@ class ViewController: UIViewController {
5351
collectionView.dataSource = self
5452

5553
view.addSubview(collectionView)
54+
5655
}
5756
}
5857

@@ -75,9 +74,9 @@ extension ViewController: UICollectionViewDelegate {
7574
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
7675
let item = items[indexPath.item]
7776
if indexPath.item == 1 {
78-
alertService.showAlertWithTap()
77+
navigator.navigate(to: DetailRoute(), from: self)
7978
} else {
80-
navigator.navigate(to: DetailRoute(parameters: [:]), from: self, using: PushPresentation())
79+
navigator.navigate(to: ChatRoute(), from: self)
8180
}
8281

8382
//
@@ -126,23 +125,3 @@ struct ItemModel {
126125
let color: UIColor
127126
}
128127

129-
// MARK: - Detail View Controller
130-
class DetailViewController: UIViewController {
131-
var item: ItemModel?
132-
133-
override func viewDidLoad() {
134-
super.viewDidLoad()
135-
view.backgroundColor = .white
136-
setupUI()
137-
}
138-
139-
private func setupUI() {
140-
let label = UILabel()
141-
label.text = item?.title
142-
label.textAlignment = .center
143-
label.frame = CGRect(x: 0, y: 0, width: 200, height: 50)
144-
label.center = view.center
145-
view.addSubview(label)
146-
}
147-
}
148-

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
11
# ModuleRoute
2-
Module Route
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//
2+
// File.swift
3+
// ModuleRoute
4+
//
5+
// Created by GIKI on 2025/2/15.
6+
//
7+
8+
import Foundation
9+
10+
public struct DeepLinkParser {
11+
private var schemeHandlers: [String: (URL) -> MRRoute?] = [:]
12+
13+
public mutating func register(scheme: String, handler: @escaping (URL) -> MRRoute?) {
14+
schemeHandlers[scheme] = handler
15+
}
16+
17+
public func parse(url: URL) -> MRRoute? {
18+
guard let scheme = url.scheme else { return nil }
19+
return schemeHandlers[scheme]?(url)
20+
}
21+
}
22+
23+
extension MRRoute {
24+
static func from(url: URL) -> MRRoute? {
25+
guard let components = URLComponents(url: url, resolvingAgainstBaseURL: true) else {
26+
return nil
27+
}
28+
29+
var params: [String: Any] = [:]
30+
components.queryItems?.forEach { item in
31+
params[item.name] = item.value
32+
}
33+
34+
return BasicRoute(params: params)
35+
}
36+
}

0 commit comments

Comments
 (0)