-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHomeViewController.swift
More file actions
299 lines (254 loc) · 9.48 KB
/
Copy pathHomeViewController.swift
File metadata and controls
299 lines (254 loc) · 9.48 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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
//
// HomeViewController.swift
// HomeFeature
//
// Created by kimnahun on 2026-01-21.
// Copyright © 2026 NDGL-iOS. All rights reserved.
//
import UIKit
import Domain
import DSKit
import RIBs
import RxCocoa
import RxSwift
// MARK: - HomePresentableListener
protocol HomePresentableListener: AnyObject {
func searchBtnTapped()
func settingBtnTapped()
func itemSelected(item: HomeItem)
func moreBtnTapped()
func reloadBtnTapped()
func viewDidLoad()
}
// MARK: - HomeViewController
final class HomeViewController: UIViewController, HomeViewControllable {
// MARK: - Properties
weak var listener: HomePresentableListener?
private let disposeBag = DisposeBag()
let moreButtonTapped = PublishSubject<Void>()
// MARK: - UI Components
private let navigationBar = NDGLNavigationBar(
style: .white,
trailingIcon: DSKitAsset.Assets.icSearch2.image,
trailing2Icon: DSKitAsset.Assets.icSettings1.image
)
private lazy var collectionView = UICollectionView(frame: .zero, collectionViewLayout: createLayout())
private let loadingIndicator = UIActivityIndicatorView(style: .medium)
private let networkErrorView = NDGLErrorView()
private var dataSource: UICollectionViewDiffableDataSource<HomeSectionKind, HomeItem>! = nil
// MARK: - Lifecycle
override func viewDidLoad() {
super.viewDidLoad()
setStyle()
setUI()
setLayout()
setCollectionView()
setDataSource()
bindInteractor()
listener?.viewDidLoad()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
navigationController?.setNavigationBarHidden(true, animated: animated)
}
}
// MARK: - UI Setup
private extension HomeViewController {
func setStyle() {
view.backgroundColor = DSKitAsset.Colors.white.color
collectionView.do {
$0.showsVerticalScrollIndicator = false
$0.showsHorizontalScrollIndicator = false
$0.backgroundColor = .clear
$0.contentInset = .zero
$0.isScrollEnabled = true
}
loadingIndicator.do {
$0.color = DSKitAsset.Colors.green300.color
}
networkErrorView.do {
$0.isHidden = true
}
}
func setUI() {
view.addSubviews(collectionView, navigationBar, loadingIndicator, networkErrorView)
}
func setLayout() {
navigationBar.snp.makeConstraints {
$0.top.equalTo(view.safeAreaLayoutGuide)
$0.directionalHorizontalEdges.equalToSuperview()
}
collectionView.snp.makeConstraints {
$0.top.equalTo(navigationBar.snp.bottom)
$0.bottom.equalToSuperview()
$0.directionalHorizontalEdges.equalToSuperview()
}
loadingIndicator.snp.makeConstraints {
$0.center.equalToSuperview()
}
networkErrorView.snp.makeConstraints {
$0.top.equalTo(navigationBar.snp.bottom)
$0.directionalHorizontalEdges.equalToSuperview()
$0.bottom.equalTo(view.safeAreaLayoutGuide).offset(-68.adjustedH)
}
}
func setCollectionView() {
collectionView.do {
$0.register(
PopularInfoCell.self,
forCellWithReuseIdentifier: PopularInfoCell.cellIdentifier
)
$0.register(
CategoryChipCell.self,
forCellWithReuseIdentifier: CategoryChipCell.cellIdentifier
)
$0.register(
HomeBannerCell.self,
forCellWithReuseIdentifier: HomeBannerCell.cellIdentifier
)
$0.register(
RecommendInfoCell.self,
forCellWithReuseIdentifier: RecommendInfoCell.cellIdentifier
)
$0.register(
HomeHeaderView.self,
forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader,
withReuseIdentifier: HomeHeaderView.reusableViewIdentifier
)
$0.register(
HomeFooterButtonView.self,
forSupplementaryViewOfKind: UICollectionView.elementKindSectionFooter,
withReuseIdentifier: HomeFooterButtonView.reusableViewIdentifier
)
}
}
}
// MARK: - Bind
private extension HomeViewController {
func bindInteractor() {
navigationBar.trailingButtonDidTap
.subscribe(with: self) { owner, _ in
owner.listener?.searchBtnTapped()
}
.disposed(by: disposeBag)
navigationBar.trailing2ButtonDidTap
.subscribe(with: self) { owner, _ in
owner.listener?.settingBtnTapped()
}
.disposed(by: disposeBag)
moreButtonTapped
.subscribe(with: self) { owner, _ in
owner.listener?.moreBtnTapped()
}
.disposed(by: disposeBag)
collectionView.rx.itemSelected
.compactMap { [weak self] indexPath in
self?.dataSource?.itemIdentifier(for: indexPath)
}
.subscribe(with: self) { owner, item in
owner.listener?.itemSelected(item: item)
}
.disposed(by: disposeBag)
networkErrorView.buttonDidTap
.subscribe(with: self) { owner, _ in
owner.listener?.reloadBtnTapped()
}
.disposed(by: disposeBag)
}
func applySnapshot(with sections: [HomeSectionModel]) {
var snapshot = NSDiffableDataSourceSnapshot<HomeSectionKind, HomeItem>()
sections.forEach {
snapshot.appendSections([$0.section])
snapshot.appendItems($0.items, toSection: $0.section)
}
dataSource?.apply(snapshot, animatingDifferences: true)
}
}
private extension HomeViewController {
func setDataSource() {
let bannerRegistration = createBannerCellRegistration()
let categoryRegistration = createCategoryCellRegistration()
let popularTripRegistration = createPopularTripCellRegistration()
let recommendedTripRegistration = createRecommedTripCellRegistration()
dataSource = UICollectionViewDiffableDataSource(collectionView: collectionView) { collectionView, indexPath, item in
switch item {
case .banner(let banner):
return collectionView.dequeueConfiguredReusableCell(
using: bannerRegistration,
for: indexPath,
item: banner
)
case .category(let category):
return collectionView.dequeueConfiguredReusableCell(
using: categoryRegistration,
for: indexPath,
item: category
)
case .popularTrip(let tripList):
return collectionView.dequeueConfiguredReusableCell(
using: popularTripRegistration,
for: indexPath,
item: tripList
)
case .recommendedTrip(let tripList):
return collectionView.dequeueConfiguredReusableCell(
using: recommendedTripRegistration,
for: indexPath,
item: tripList
)
}
}
configureSupplementaryView()
}
func configureSupplementaryView() {
let headerRegistration = createHeaderRegistration()
let popularFooterRegistration = createPopularFooterRegistration()
dataSource.supplementaryViewProvider = { collectionView, kind, indexPath in
guard HomeSectionKind(rawValue: indexPath.section) != nil else {
return UICollectionReusableView()
}
if kind == UICollectionView.elementKindSectionHeader {
return collectionView.dequeueConfiguredReusableSupplementary(
using: headerRegistration,
for: indexPath
)
}
if kind == UICollectionView.elementKindSectionFooter {
return collectionView.dequeueConfiguredReusableSupplementary(
using: popularFooterRegistration,
for: indexPath
)
}
return nil
}
}
}
extension HomeViewController: HomePresentable {
func showErrorView(_ isError: Bool) {
DispatchQueue.main.async { [weak self] in
guard let self else { return }
self.networkErrorView.isHidden = !isError
self.collectionView.isHidden = isError
if isError {
self.loadingIndicator.stopAnimating()
}
}
}
func update(with sections: [HomeSectionModel]) {
DispatchQueue.main.async { [weak self] in
self?.applySnapshot(with: sections)
}
}
func setLoading(_ isLoading: Bool) {
DispatchQueue.main.async { [weak self] in
guard let self else { return }
if isLoading {
self.loadingIndicator.startAnimating()
self.collectionView.alpha = 0.5
} else {
self.loadingIndicator.stopAnimating()
self.collectionView.alpha = 1.0
}
}
}
}