Skip to content

Commit a01ad73

Browse files
author
GIKI
committed
commit: navigator add servicelocator
1 parent 7ce3971 commit a01ad73

File tree

8 files changed

+83
-91
lines changed

8 files changed

+83
-91
lines changed

Example/ModuleRouteExample/AppDelegate.swift

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ import ModuleRoute
1111
@main
1212
class AppDelegate: UIResponder, UIApplicationDelegate {
1313

14-
public lazy var myServiceLocator = startServiceLocator {
15-
AppModule()
16-
}
14+
public lazy var myServiceLocator = startServiceLocator()
15+
// {
16+
//// AppModule()
17+
// }
1718
private var navigator: MRNavigator!
1819

1920

@@ -29,8 +30,16 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
2930

3031
private func setupRoute() {
3132
navigator = MRNavigator(serviceLocator:myServiceLocator)
32-
navigator.register(moduleType: DetailModule.self)
33-
navigator.register(moduleType: ChatModule.self)
33+
navigator.register(DetailInterface.self, routes: DetailModule.supportedRoutes) {
34+
DetailModule()
35+
}
36+
navigator.register(ChatInterface.self, routes: ChatModule.supportedRoutes) {
37+
ChatModule()
38+
}
39+
40+
// navigator.register(interfaceType: DetailInterface.self, moduleType: DetailModule.self)
41+
// navigator.register(moduleType: DetailModule.self)
42+
// navigator.register(moduleType: ChatModule.self)
3443
}
3544

3645

@@ -58,9 +67,9 @@ class AppModule: ServiceLocatorModule {
5867
single(DetailInterface.self) {
5968
DetailModule()
6069
}
61-
single {
62-
ChatModule()
63-
}
70+
// single {
71+
// ChatModule()
72+
// }
6473
single(ChatInterface.self) {
6574
ChatModule()
6675
}

Example/ModuleRouteExample/Chat/ChatModule.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ protocol ChatInterface: MRModule {
1313
}
1414

1515
class ChatModule: ChatInterface {
16+
1617
static var supportedRoutes: [MRRoute.Type] = [
1718
ChatRoute.self
1819
]

Example/ModuleRouteExample/Detail/DetailModule.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@ protocol DetailInterface: MRModule {
1515

1616
class DetailModule: DetailInterface {
1717

18+
@MRInject var navigator: MRNavigator
19+
1820
static var supportedRoutes: [MRRoute.Type] = [
1921
DetailRoute.self
2022
]
2123
public init() {
22-
24+
2325
}
2426

2527
public func handle(route: MRRoute) -> RouteResult {

Example/ModuleRouteExample/ViewController.swift

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

1111
class ViewController: UIViewController {
1212

13-
@PluginInject var navigator: MRNavigator
13+
@MRInject var navigator: MRNavigator
1414
// @Inject(DetailInterface) var detail: DetailInterface
15-
@PluginInject var detail: DetailInterface
15+
1616

1717
// MARK: - Properties
1818
private var collectionView: UICollectionView!
@@ -77,18 +77,19 @@ extension ViewController: UICollectionViewDelegate {
7777
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
7878
let item = items[indexPath.item]
7979
if indexPath.item == 1 {
80-
// navigator.navigate(to: DetailRoute(), from: self)
80+
@MRInject var detail: DetailInterface
8181
let result = detail.handle(route: DetailRoute())
82+
switch result {
83+
case .navigator(let vc):
84+
self.navigationController?.pushViewController(vc, animated: true)
85+
default: break
86+
87+
}
8288
print(result)
8389
} else {
8490
navigator.navigate(to: ChatRoute(), from: self)
8591
}
86-
87-
//
88-
// // 创建详情页面并跳转
89-
// let detailVC = DetailViewController()
90-
// detailVC.item = item
91-
// navigationController?.pushViewController(detailVC, animated: true)
92+
9293
}
9394
}
9495

Sources/ModuleRoute/MRDependencyContainer.swift

Lines changed: 0 additions & 64 deletions
This file was deleted.

Sources/ModuleRoute/MRInject.swift

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//
2+
// File.swift
3+
// ModuleRoute
4+
//
5+
// Created by GIKI on 2025/2/17.
6+
//
7+
8+
import Foundation
9+
import UIKit
10+
11+
public final class MRNavigatorLocator {
12+
13+
static let shared = MRNavigatorLocator()
14+
15+
var serviceLocator: ServiceLocator?
16+
17+
}
18+
19+
@propertyWrapper
20+
public final class MRInject<T>: Dependency<T> {
21+
22+
23+
public var wrappedValue: T {
24+
resolvedWrappedValue()
25+
}
26+
27+
public init() {
28+
guard let serviceLocator = MRNavigatorLocator.shared.serviceLocator else {
29+
fatalError("Could not access AppDelegate or cast it to the correct type.")
30+
}
31+
super.init(serviceLocator)
32+
}
33+
}

Sources/ModuleRoute/MRNavigator.swift

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,33 @@ public class MRNavigator {
1919

2020
public init(serviceLocator: ServiceLocator) {
2121
self.serviceLocator = serviceLocator
22+
MRNavigatorLocator.shared.serviceLocator = serviceLocator
2223
serviceLocator.register {
2324
self
2425
}
25-
if #available(iOS 13.0, *) {
26-
Task{
27-
await serviceLocator.build()
28-
}
29-
}
26+
buildServiceLocator()
3027
}
3128

3229
private var routeToModuleTypeMap: [ObjectIdentifier: (ServiceLocator) -> MRModule] = [:]
3330

34-
public func register<T: MRModule>(moduleType: T.Type) {
35-
T.supportedRoutes.forEach { routeType in
31+
public func register<T>(_ type: T.Type = T.self, routes: [MRRoute.Type], _ factory: @escaping () -> T) -> Void {
32+
serviceLocator.single(type, factory)
33+
routes.forEach { routeType in
3634
let key = ObjectIdentifier(routeType)
3735
routeToModuleTypeMap[key] = { locator in
38-
return try! locator.resolve() as T
36+
guard let instance = try? locator.resolve() as T else {
37+
fatalError("Failed to resolve type \(T.self)")
38+
}
39+
return instance as! MRModule
40+
}
41+
}
42+
buildServiceLocator()
43+
}
44+
45+
private func buildServiceLocator() {
46+
if #available(iOS 13.0, *) {
47+
Task{
48+
await serviceLocator.build()
3949
}
4050
}
4151
}

Sources/ModuleRoute/ServiceLocator/ServiceLocator.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public class ServiceLocator {
3838
/// - Parameters:
3939
/// - type: The type of the singleton service to register.
4040
/// - factory: A closure that creates and returns a single instance of the service.
41-
internal func single<T>(_ type: T.Type = T.self, _ factory: @escaping () -> T) {
41+
public func single<T>(_ type: T.Type = T.self, _ factory: @escaping () -> T) {
4242
let key = String(describing: type)
4343
singleLock.lock()
4444
defer { singleLock.unlock() }

0 commit comments

Comments
 (0)