Skip to content

Commit 8e404f3

Browse files
committed
commit: update module route
1 parent 82e3636 commit 8e404f3

13 files changed

+201
-198
lines changed

Example/ModuleRouteExample/AppDelegate.swift

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,23 @@ import ModuleRoute
1010

1111
@main
1212
class AppDelegate: UIResponder, UIApplicationDelegate {
13-
14-
private let navigator: MRNavigator = MRNavigator()
13+
14+
private var navigator: MRNavigator = MRNavigator()
1515

1616
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
1717
setupRoute()
1818
return true
1919
}
2020

21+
private func setupRoute() {
22+
23+
navigator.register(module: DetailModule())
24+
navigator.register(dependencyFactory: {
25+
ServiceA()
26+
}, forType: ServiceAInterface.self)
27+
}
28+
29+
2130
// MARK: UISceneSession Lifecycle
2231

2332
func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
@@ -32,15 +41,6 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
3241
// Use this method to release any resources that were specific to the discarded scenes, as they will not return.
3342
}
3443

35-
private func setupRoute() {
36-
37-
navigator.register(routeHandler: AppRouteFacotry())
38-
navigator.register(dependencyFactory: {
39-
ServiceA()
40-
}, forType: ServiceAInterface.self)
41-
42-
}
43-
44-
44+
4545
}
4646

Example/ModuleRouteExample/AppRouteFacotry.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,17 @@ import Foundation
99
import ModuleRoute
1010
import UIKit
1111
class AppRouteFacotry:RouteFactory {
12+
1213
var supportedRoutes: [MRRoute.Type] {
13-
return []
14+
return [
15+
DetailRoute.self
16+
]
1417
}
1518

1619
func destinationModule(for route: MRRoute, from viewController: UIViewController) -> MRModule.Type? {
20+
if route is DetailRoute {
21+
return DetailModule.self
22+
}
1723
return nil
1824
}
1925
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//
2+
// DetailModule.swift
3+
// ModuleRouteExample
4+
//
5+
// Created by GIKI on 2025/2/14.
6+
//
7+
8+
import Foundation
9+
import ModuleRoute
10+
import UIKit
11+
12+
class DetailModule: MRModule {
13+
14+
var supportedRoutes: [MRRoute.Type] {
15+
return [
16+
DetailRoute.self
17+
]
18+
}
19+
20+
21+
public init() {}
22+
23+
public func handle(route: MRRoute) -> RouteResult {
24+
// 根据具体路由做出响应
25+
switch route {
26+
case is DetailRoute:
27+
let detail = DetailViewController()
28+
return .viewController(detail)
29+
default:
30+
return .none
31+
}
32+
}
33+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//
2+
// DetailRoute.swift
3+
// ModuleRouteExample
4+
//
5+
// Created by GIKI on 2025/2/14.
6+
//
7+
8+
import Foundation
9+
import ModuleRoute
10+
11+
struct DetailRoute: MRRoute {
12+
static var name: String = "detail"
13+
14+
public var parameters: [String: Any] = [:]
15+
16+
init(parameters: [String: Any]) {
17+
self.parameters = parameters
18+
}
19+
20+
}

Example/ModuleRouteExample/ServiceA.swift

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,50 @@
66
//
77

88
import Foundation
9+
import UIKit
910

10-
class ServiceA {
11+
class ServiceA: ServiceAInterface {
1112

13+
func showAlertWithTap() {
14+
// 创建 UIAlertController
15+
let alertController = UIAlertController(
16+
title: "点击了我了",
17+
message: nil,
18+
preferredStyle: .alert
19+
)
20+
21+
// 创建确认按钮
22+
let okAction = UIAlertAction(
23+
title: "知道了",
24+
style: .default,
25+
handler: nil
26+
)
27+
28+
// 添加按钮到 alertController
29+
alertController.addAction(okAction)
30+
31+
// 获取当前最顶层的 ViewController 并显示 alert
32+
if let topViewController = UIApplication.shared.keyWindow?.rootViewController?.topMostViewController() {
33+
topViewController.present(alertController, animated: true, completion: nil)
34+
}
35+
}
36+
}
37+
38+
// 辅助扩展,用于获取最顶层的 ViewController
39+
extension UIViewController {
40+
func topMostViewController() -> UIViewController {
41+
if let presented = presentedViewController {
42+
return presented.topMostViewController()
43+
}
44+
45+
if let navigation = self as? UINavigationController {
46+
return navigation.visibleViewController?.topMostViewController() ?? navigation
47+
}
48+
49+
if let tab = self as? UITabBarController {
50+
return tab.selectedViewController?.topMostViewController() ?? tab
51+
}
52+
53+
return self
54+
}
1255
}

Example/ModuleRouteExample/ServiceAInterface.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@
77

88
import Foundation
99

10-
protocol ServiceAInterface {
11-
10+
public protocol ServiceAInterface {
11+
func showAlertWithTap()
1212
}

Example/ModuleRouteExample/ViewController.swift

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,13 @@
66
//
77

88
import UIKit
9+
import ModuleRoute
910

1011
class ViewController: UIViewController {
1112

13+
@MRInject var navigator: MRNavigator
14+
@MRInject var alertService: ServiceAInterface
15+
1216
// MARK: - Properties
1317
private var collectionView: UICollectionView!
1418
private let cellIdentifier = "Cell"
@@ -70,11 +74,17 @@ extension ViewController: UICollectionViewDataSource {
7074
extension ViewController: UICollectionViewDelegate {
7175
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
7276
let item = items[indexPath.item]
77+
if indexPath.item == 1 {
78+
alertService.showAlertWithTap()
79+
} else {
80+
navigator.navigate(to: DetailRoute(parameters: [:]), from: self, using: PushPresentation())
81+
}
7382

74-
// 创建详情页面并跳转
75-
let detailVC = DetailViewController()
76-
detailVC.item = item
77-
navigationController?.pushViewController(detailVC, animated: true)
83+
//
84+
// // 创建详情页面并跳转
85+
// let detailVC = DetailViewController()
86+
// detailVC.item = item
87+
// navigationController?.pushViewController(detailVC, animated: true)
7888
}
7989
}
8090

Sources/ModuleRoute/DependencyContainer.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ public protocol DependencyContainer {
1515
}
1616

1717
public class DefaultDependencyContainer: DependencyContainer {
18+
19+
public static let shared = DefaultDependencyContainer()
1820

1921
private var factories = [String: DependencyFactory]()
2022
private var instances = [String: Any]()
@@ -32,7 +34,6 @@ public class DefaultDependencyContainer: DependencyContainer {
3234
if let instance = instances[key] as? T {
3335
return instance
3436
}
35-
3637
// 使用工厂创建新实例
3738
if let factory = factories[key] {
3839
let instance = factory()

Sources/ModuleRoute/MRInject.swift

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,22 @@
88
import Foundation
99

1010
@propertyWrapper
11-
public class MRInject<T>: Resolvable {
11+
public class MRInject<T> {
1212
private var dependency: T?
13+
1314
public var wrappedValue: T {
14-
guard let dependency = dependency else {
15-
fatalError("Dependency \(T.self) not resolved")
15+
get {
16+
if dependency == nil {
17+
dependency = DefaultDependencyContainer.shared.resolve(T.self)
18+
if dependency == nil {
19+
fatalError("Dependency \(T.self) not resolved")
20+
}
21+
}
22+
return dependency!
1623
}
17-
return dependency
1824
}
1925

2026
public init() {}
21-
22-
public func resolve(using container: DependencyContainer) {
23-
dependency = container.resolve(T.self)
24-
}
2527
}
2628

2729

28-
public protocol Resolvable {
29-
func resolve(using container: DependencyContainer)
30-
}
31-

Sources/ModuleRoute/MRModule.swift

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,30 @@
77

88
import UIKit
99

10+
// 路由处理结果枚举
11+
public enum RouteResult {
12+
case viewController(UIViewController) // 返回控制器
13+
case handler(() -> Void) // 执行闭包
14+
case service(Any) // 返回服务实例
15+
case value(Any) // 返回值
16+
case none // 无返回
17+
}
18+
1019
public protocol MRModule {
11-
func build(from route: MRRoute?) -> UIViewController
12-
func resolveDependencies(using container: DependencyContainer)
13-
func isEnabled(route: MRRoute?) -> Bool
14-
func fallbackModule(for route: MRRoute?) -> MRModule.Type?
15-
init()
20+
21+
var supportedRoutes: [MRRoute.Type] { get }
22+
// 处理路由请求
23+
func handle(route: MRRoute) -> RouteResult
1624
}
1725

1826
extension MRModule {
19-
20-
public func resolveDependencies(using container: DependencyContainer) {
21-
let mirror = Mirror(reflecting: self)
22-
for child in mirror.children {
23-
if let resolvable = child.value as? Resolvable {
24-
resolvable.resolve(using: container)
25-
}
27+
// 如果路由处理结果为 .viewController,则返回构造的目标控制器,否则返回 nil
28+
func build(from route: MRRoute) -> UIViewController? {
29+
switch handle(route: route) {
30+
case .viewController(let vc):
31+
return vc
32+
default:
33+
return nil
2634
}
2735
}
28-
29-
public func isEnabled(route: MRRoute?) -> Bool { return true }
30-
public func fallbackModule(for route: MRRoute?) -> MRModule.Type? { return nil }
3136
}
32-

0 commit comments

Comments
 (0)