-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMainViewController.swift
More file actions
214 lines (189 loc) · 7.62 KB
/
MainViewController.swift
File metadata and controls
214 lines (189 loc) · 7.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import SectionUI
import SnapKit
import UIKit
class MainViewController: SKCollectionViewController {
override func viewDidLoad() {
super.viewDidLoad()
title = "SectionKit Example"
view.backgroundColor = .systemBackground
manager.reload([
makeFoundationSection(),
makeLayoutSection(),
makeDataSection(),
makeInteractionSection(),
makePageSection(),
])
}
private func makeFoundationSection() -> SKCSectionProtocol {
makeSection(
title: "1. Foundation (基础: 合规 Cell 的构建)",
models: [
.init(
title: "Lesson 1: Standard Code-Based Cell", desc: "StandardCellViewController",
action: { StandardCellViewController() }),
.init(
title: "Lesson 3: The Wrapper Pattern", desc: "WrapperCellViewController",
action: { WrapperCellViewController() }),
.init(
title: "Lesson 4: Auto-Layout & Adaptive Cell",
desc: "AdaptiveCellViewController", action: { AdaptiveCellViewController() }),
.init(
title: "Lesson 5: Minimal Setup (Hello World)",
desc: "HelloWorldViewController", action: { HelloWorldViewController() }),
.init(
title: "Single Type Section", desc: "SingleTypeSectionViewController",
action: { SingleTypeSectionViewController() }),
])
}
private func makeLayoutSection() -> SKCSectionProtocol {
makeSection(
title: "2. Layout (布局与样式)",
models: [
.init(
title: "Grid Layout", desc: "GridColorViewController",
action: { GridColorViewController() }),
.init(
title: "Waterfall Layout", desc: "WaterfallViewController",
action: { WaterfallViewController() }),
.init(
title: "Header & Footer", desc: "FooterAndHeaderViewController",
action: { FooterAndHeaderViewController() }),
.init(
title: "Decoration Views", desc: "DecorationViewController",
action: { DecorationViewController() }),
.init(
title: "Index Titles", desc: "IndexTitlesViewController",
action: { IndexTitlesViewController() }),
.init(
title: "Pin Index", desc: "PinIndexViewController",
action: { PinIndexViewController() }),
])
}
private func makeDataSection() -> SKCSectionProtocol {
makeSection(
title: "3. Data (数据与事件)",
models: [
.init(
title: "Multiple Sections", desc: "MultipleSectionViewController",
action: { MultipleSectionViewController() }),
.init(
title: "Load & Pull", desc: "LoadAndPullViewController",
action: { LoadAndPullViewController() }),
.init(
title: "Combine Subscription", desc: "SubscribeDataWithCombineViewController",
action: { SubscribeDataWithCombineViewController() }),
.init(
title: "Select Text", desc: "SelectTextViewController",
action: { SelectTextViewController() }),
.init(
title: "Reactive Data (@SKPublished)", desc: "ReactiveDataViewController",
action: { ReactiveDataViewController() }),
])
}
private func makeInteractionSection() -> SKCSectionProtocol {
makeSection(
title: "4. Interaction (高级交互)",
models: [
.init(
title: "Parallax", desc: "ParallaxViewController",
action: { ParallaxViewController() }),
.init(
title: "Scroll Observer", desc: "ScrollObserverViewController",
action: { ScrollObserverViewController() }),
.init(
title: "Gallery Performance", desc: "GalleryViewController",
action: { GalleryViewController() }),
])
}
private func makePageSection() -> SKCSectionProtocol {
makeSection(
title: "5. Page (页面与嵌套)",
models: [
.init(
title: "Standard Page Controller", desc: "PageViewController",
action: { PageViewController() }),
.init(
title: "Nested Scroll", desc: "NestedScrollViewController",
action: { NestedScrollViewController() }),
])
}
private func makeSection(title: String, models: [MenuCell.Model]) -> SKCSingleTypeSection<
MenuCell
> {
return MenuCell.wrapperToSingleTypeSection(models)
.onCellAction(.selected) { [weak self] context in
let vc = context.model.action()
self?.navigationController?.pushViewController(vc, animated: true)
}
.setHeader(HeaderLabel.self, model: title) { view in
view.backgroundColor = .secondarySystemBackground
}
}
}
// MARK: - Views
class HeaderLabel: UICollectionReusableView, SKLoadViewProtocol, SKConfigurableView {
typealias Model = String
lazy var label: UILabel = {
let label = UILabel()
label.font = .systemFont(ofSize: 14, weight: .bold)
label.textColor = .secondaryLabel
return label
}()
override init(frame: CGRect) {
super.init(frame: frame)
addSubview(label)
label.snp.makeConstraints { make in
make.edges.equalToSuperview().inset(
UIEdgeInsets(top: 0, left: 16, bottom: 0, right: 16))
}
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func config(_ model: String) {
label.text = model
}
static func preferredSize(limit size: CGSize, model: String?) -> CGSize {
return .init(width: size.width, height: 40)
}
}
class MenuCell: UICollectionViewCell, SKLoadViewProtocol, SKConfigurableView {
struct Model {
let title: String
let desc: String
let action: () -> UIViewController
}
private lazy var titleLabel: UILabel = {
let label = UILabel()
label.font = .systemFont(ofSize: 16, weight: .medium)
return label
}()
private lazy var descLabel: UILabel = {
let label = UILabel()
label.font = .systemFont(ofSize: 12)
label.textColor = .secondaryLabel
return label
}()
override init(frame: CGRect) {
super.init(frame: frame)
contentView.addSubview(titleLabel)
contentView.addSubview(descLabel)
titleLabel.snp.makeConstraints { make in
make.top.leading.trailing.equalToSuperview().inset(16)
}
descLabel.snp.makeConstraints { make in
make.top.equalTo(titleLabel.snp.bottom).offset(4)
make.leading.trailing.bottom.equalToSuperview().inset(16)
}
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func config(_ model: Model) {
titleLabel.text = model.title
descLabel.text = model.desc
}
static func preferredSize(limit size: CGSize, model: Model?) -> CGSize {
return .init(width: size.width, height: 60)
}
}