Skip to content

Commit 82e3636

Browse files
committed
commit: update example
1 parent d18fe8f commit 82e3636

File tree

6 files changed

+237
-11
lines changed

6 files changed

+237
-11
lines changed

Example/ModuleRouteExample/AppDelegate.swift

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

88
import UIKit
9+
import ModuleRoute
910

1011
@main
1112
class AppDelegate: UIResponder, UIApplicationDelegate {
12-
13-
14-
13+
14+
private let navigator: MRNavigator = MRNavigator()
15+
1516
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
16-
// Override point for customization after application launch.
17+
setupRoute()
1718
return true
1819
}
1920

@@ -30,6 +31,15 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
3031
// If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
3132
// Use this method to release any resources that were specific to the discarded scenes, as they will not return.
3233
}
34+
35+
private func setupRoute() {
36+
37+
navigator.register(routeHandler: AppRouteFacotry())
38+
navigator.register(dependencyFactory: {
39+
ServiceA()
40+
}, forType: ServiceAInterface.self)
41+
42+
}
3343

3444

3545
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//
2+
// AppRouteFacotry.swift
3+
// ModuleRouteExample
4+
//
5+
// Created by GIKI on 2025/2/14.
6+
//
7+
8+
import Foundation
9+
import ModuleRoute
10+
import UIKit
11+
class AppRouteFacotry:RouteFactory {
12+
var supportedRoutes: [MRRoute.Type] {
13+
return []
14+
}
15+
16+
func destinationModule(for route: MRRoute, from viewController: UIViewController) -> MRModule.Type? {
17+
return nil
18+
}
19+
}
Lines changed: 59 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,78 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="13122.16" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="BYZ-38-t0r">
2+
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="23504" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="6IG-wi-BMn">
3+
<device id="retina6_12" orientation="portrait" appearance="light"/>
34
<dependencies>
4-
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="13104.12"/>
5+
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="23506"/>
56
<capability name="Safe area layout guides" minToolsVersion="9.0"/>
7+
<capability name="System colors in document resources" minToolsVersion="11.0"/>
68
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
79
</dependencies>
810
<scenes>
911
<!--View Controller-->
1012
<scene sceneID="tne-QT-ifu">
1113
<objects>
12-
<viewController id="BYZ-38-t0r" customClass="ViewController" customModuleProvider="target" sceneMemberID="viewController">
14+
<viewController id="BYZ-38-t0r" customClass="ViewController" customModule="ModuleRouteExample" customModuleProvider="target" sceneMemberID="viewController">
1315
<view key="view" contentMode="scaleToFill" id="8bC-Xf-vdC">
14-
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
16+
<rect key="frame" x="0.0" y="0.0" width="393" height="852"/>
1517
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
16-
<color key="backgroundColor" xcode11CocoaTouchSystemColor="systemBackgroundColor" cocoaTouchSystemColor="whiteColor"/>
1718
<viewLayoutGuide key="safeArea" id="6Tk-OE-BBY"/>
19+
<color key="backgroundColor" systemColor="systemBackgroundColor"/>
1820
</view>
21+
<navigationItem key="navigationItem" id="U38-fE-uGK"/>
1922
</viewController>
2023
<placeholder placeholderIdentifier="IBFirstResponder" id="dkx-z0-nzr" sceneMemberID="firstResponder"/>
2124
</objects>
25+
<point key="canvasLocation" x="1013" y="-357"/>
26+
</scene>
27+
<!--Root View Controller-->
28+
<scene sceneID="xZ1-eM-eSw">
29+
<objects>
30+
<tableViewController id="AKd-en-CFy" sceneMemberID="viewController">
31+
<tableView key="view" clipsSubviews="YES" contentMode="scaleToFill" alwaysBounceVertical="YES" dataMode="prototypes" style="plain" separatorStyle="default" rowHeight="-1" estimatedRowHeight="-1" sectionHeaderHeight="-1" estimatedSectionHeaderHeight="-1" sectionFooterHeight="-1" estimatedSectionFooterHeight="-1" id="GfZ-uZ-12S">
32+
<rect key="frame" x="0.0" y="0.0" width="393" height="852"/>
33+
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
34+
<color key="backgroundColor" systemColor="systemBackgroundColor"/>
35+
<prototypes>
36+
<tableViewCell clipsSubviews="YES" contentMode="scaleToFill" preservesSuperviewLayoutMargins="YES" selectionStyle="default" indentationWidth="10" id="Fvi-x5-Azj">
37+
<rect key="frame" x="0.0" y="50" width="393" height="44.666667938232422"/>
38+
<autoresizingMask key="autoresizingMask"/>
39+
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" preservesSuperviewLayoutMargins="YES" insetsLayoutMarginsFromSafeArea="NO" tableViewCell="Fvi-x5-Azj" id="ZDs-9g-lUH">
40+
<rect key="frame" x="0.0" y="0.0" width="393" height="44.666667938232422"/>
41+
<autoresizingMask key="autoresizingMask"/>
42+
</tableViewCellContentView>
43+
</tableViewCell>
44+
</prototypes>
45+
<connections>
46+
<outlet property="dataSource" destination="AKd-en-CFy" id="Hsi-8d-sR8"/>
47+
<outlet property="delegate" destination="AKd-en-CFy" id="eFP-qy-Wef"/>
48+
</connections>
49+
</tableView>
50+
<navigationItem key="navigationItem" title="Root View Controller" id="Jbc-NJ-jMP"/>
51+
</tableViewController>
52+
<placeholder placeholderIdentifier="IBFirstResponder" id="86k-Ws-4MP" userLabel="First Responder" customClass="UIResponder" sceneMemberID="firstResponder"/>
53+
</objects>
54+
<point key="canvasLocation" x="553" y="-338"/>
55+
</scene>
56+
<!--Navigation Controller-->
57+
<scene sceneID="Azw-R4-fG9">
58+
<objects>
59+
<navigationController id="6IG-wi-BMn" sceneMemberID="viewController">
60+
<navigationBar key="navigationBar" contentMode="scaleToFill" id="N43-zo-g9h">
61+
<rect key="frame" x="0.0" y="59" width="393" height="44"/>
62+
<autoresizingMask key="autoresizingMask"/>
63+
</navigationBar>
64+
<connections>
65+
<segue destination="BYZ-38-t0r" kind="relationship" relationship="rootViewController" id="CGd-Wi-XuJ"/>
66+
</connections>
67+
</navigationController>
68+
<placeholder placeholderIdentifier="IBFirstResponder" id="rRz-Z3-gji" userLabel="First Responder" customClass="UIResponder" sceneMemberID="firstResponder"/>
69+
</objects>
70+
<point key="canvasLocation" x="-356" y="-338"/>
2271
</scene>
2372
</scenes>
73+
<resources>
74+
<systemColor name="systemBackgroundColor">
75+
<color white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
76+
</systemColor>
77+
</resources>
2478
</document>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//
2+
// ModuleA.swift
3+
// ModuleRouteExample
4+
//
5+
// Created by GIKI on 2025/2/14.
6+
//
7+
8+
import Foundation
9+
10+
class ServiceA {
11+
12+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//
2+
// ModuleAInterface.swift
3+
// ModuleRouteExample
4+
//
5+
// Created by GIKI on 2025/2/14.
6+
//
7+
8+
import Foundation
9+
10+
protocol ServiceAInterface {
11+
12+
}

Example/ModuleRouteExample/ViewController.swift

Lines changed: 121 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,131 @@
88
import UIKit
99

1010
class ViewController: UIViewController {
11-
11+
12+
// MARK: - Properties
13+
private var collectionView: UICollectionView!
14+
private let cellIdentifier = "Cell"
15+
16+
// 可配置的数据源
17+
private var items: [ItemModel] = [
18+
ItemModel(title: "Item 1", color: .blue),
19+
ItemModel(title: "Item 2", color: .green),
20+
ItemModel(title: "Item 3", color: .orange),
21+
ItemModel(title: "Item 4", color: .purple)
22+
]
23+
24+
// MARK: - Lifecycle
1225
override func viewDidLoad() {
1326
super.viewDidLoad()
14-
// Do any additional setup after loading the view.
27+
setupCollectionView()
28+
}
29+
30+
// MARK: - Setup
31+
private func setupCollectionView() {
32+
// 创建布局
33+
let layout = UICollectionViewFlowLayout()
34+
layout.itemSize = CGSize(width: (view.frame.width - 40) / 2, height: 100)
35+
layout.minimumInteritemSpacing = 10
36+
layout.minimumLineSpacing = 10
37+
layout.sectionInset = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
38+
39+
// 初始化 CollectionView
40+
collectionView = UICollectionView(frame: view.bounds, collectionViewLayout: layout)
41+
collectionView.backgroundColor = .white
42+
collectionView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
43+
44+
// 注册 cell
45+
collectionView.register(CustomCell.self, forCellWithReuseIdentifier: cellIdentifier)
46+
47+
// 设置代理
48+
collectionView.delegate = self
49+
collectionView.dataSource = self
50+
51+
view.addSubview(collectionView)
1552
}
53+
}
1654

55+
// MARK: - UICollectionViewDataSource
56+
extension ViewController: UICollectionViewDataSource {
57+
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
58+
return items.count
59+
}
60+
61+
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
62+
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellIdentifier, for: indexPath) as! CustomCell
63+
let item = items[indexPath.item]
64+
cell.configure(with: item)
65+
return cell
66+
}
67+
}
1768

69+
// MARK: - UICollectionViewDelegate
70+
extension ViewController: UICollectionViewDelegate {
71+
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
72+
let item = items[indexPath.item]
73+
74+
// 创建详情页面并跳转
75+
let detailVC = DetailViewController()
76+
detailVC.item = item
77+
navigationController?.pushViewController(detailVC, animated: true)
78+
}
79+
}
80+
81+
// MARK: - Custom Cell
82+
class CustomCell: UICollectionViewCell {
83+
private let titleLabel: UILabel = {
84+
let label = UILabel()
85+
label.textAlignment = .center
86+
label.textColor = .white
87+
return label
88+
}()
89+
90+
override init(frame: CGRect) {
91+
super.init(frame: frame)
92+
setupUI()
93+
}
94+
95+
required init?(coder: NSCoder) {
96+
fatalError("init(coder:) has not been implemented")
97+
}
98+
99+
private func setupUI() {
100+
contentView.addSubview(titleLabel)
101+
titleLabel.frame = contentView.bounds
102+
titleLabel.autoresizingMask = [.flexibleWidth, .flexibleHeight]
103+
}
104+
105+
func configure(with item: ItemModel) {
106+
titleLabel.text = item.title
107+
contentView.backgroundColor = item.color
108+
layer.cornerRadius = 8
109+
clipsToBounds = true
110+
}
111+
}
112+
113+
// MARK: - Model
114+
struct ItemModel {
115+
let title: String
116+
let color: UIColor
117+
}
118+
119+
// MARK: - Detail View Controller
120+
class DetailViewController: UIViewController {
121+
var item: ItemModel?
122+
123+
override func viewDidLoad() {
124+
super.viewDidLoad()
125+
view.backgroundColor = .white
126+
setupUI()
127+
}
128+
129+
private func setupUI() {
130+
let label = UILabel()
131+
label.text = item?.title
132+
label.textAlignment = .center
133+
label.frame = CGRect(x: 0, y: 0, width: 200, height: 50)
134+
label.center = view.center
135+
view.addSubview(label)
136+
}
18137
}
19138

0 commit comments

Comments
 (0)