-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearchView.swift
More file actions
387 lines (334 loc) · 12.8 KB
/
SearchView.swift
File metadata and controls
387 lines (334 loc) · 12.8 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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
// Copyright © 2025 Booket. All rights reserved
import BKDesign
import BKDomain
import Combine
import SnapKit
import UIKit
final class SearchView: BaseView {
enum CollectionLayoutMode {
case beforeSearch
case afterSearch
}
enum SearchSection: Hashable {
case recent
case result
}
let eventPublisher = PassthroughSubject<SearchViewEvent, Never>()
private let searchBar = BKSearchTextField(
placeholder: "도서 검색 후 내 서재에 담아보세요.",
type: .normal
)
private let divider = BKDivider(type: .medium)
private let header = SearchSectionHeaderView()
private let emptyView = EmptyStateView()
private lazy var collectionView: UICollectionView = {
return setupCollectionView()
}()
private lazy var dataSource: UICollectionViewDiffableDataSource<SearchSection, SearchItem> = {
return setupDataSource()
}()
private var layoutMode = CollectionLayoutMode.beforeSearch
override func setupView() {
addSubviews(searchBar, divider, header, collectionView, emptyView)
}
override func configure() {
searchBar.setOnReturn { [weak self] text in
self?.eventPublisher.send(.search(text))
}
emptyView.onTapActionButton = { [weak self] in
let currentMode = AccessModeCenter.shared.mode.value
if currentMode == .guest {
self?.eventPublisher.send(.goToLogin)
} else {
self?.eventPublisher.send(.goToRequestPage)
}
}
}
override func setupLayout() {
searchBar.snp.makeConstraints {
$0.top.equalTo(safeAreaLayoutGuide.snp.top)
.inset(LayoutConstants.searchBarInset)
$0.leading.trailing.equalToSuperview()
.inset(LayoutConstants.horizontalInset)
}
divider.snp.makeConstraints {
$0.top.equalTo(searchBar.snp.bottom)
.offset(LayoutConstants.searchBarInset + LayoutConstants.dividerOffset)
$0.trailing.leading.equalToSuperview()
}
header.snp.makeConstraints {
$0.top.equalTo(divider.snp.bottom)
.offset(LayoutConstants.dividerOffset)
$0.leading.trailing.equalToSuperview()
}
collectionView.snp.makeConstraints {
$0.top.equalTo(header.snp.bottom)
.offset(LayoutConstants.headerOffset)
$0.leading.trailing.bottom.equalToSuperview()
}
emptyView.snp.makeConstraints {
$0.top.equalTo(header.snp.bottom)
.offset(LayoutConstants.headerOffset)
$0.leading.trailing.bottom.equalToSuperview()
}
}
func setSearchBarPlaceholder(with placeholder: String) {
searchBar.placeholder = placeholder
}
func setSearchBarText(with text: String) {
searchBar.text = text
}
func applySnapshot(
with state: SearchViewModel.SearchState,
count: Int = 0
) {
var snapshot = NSDiffableDataSourceSnapshot<SearchSection, SearchItem>()
switch state {
case .recent(let state):
emptyView.isHidden = true
collectionView.isHidden = false
if state.queries.isEmpty {
collectionView.backgroundView = makeEmptyLabel(state.placeholder)
} else {
collectionView.backgroundView = nil
header.setTitle(.recent)
snapshot.appendSections([.recent])
snapshot.appendItems(state.queries.map { .query($0) }, toSection: .recent)
}
case .result(let state):
let isEmpty = state.books.isEmpty && state.bookInfos.isEmpty
if isEmpty {
updateEmptyState(
isHidden: false,
title: "아직 등록된 책이 없어요",
description: "카카오톡 채널로 문의를 남겨주세요",
buttontitle: "문의하기"
)
searchBar.setClearButtonMode(.whileEditing)
} else {
collectionView.isHidden = false
collectionView.backgroundView = nil
snapshot.appendSections([.result])
// Book과 BookInfo 모두 처리
snapshot.appendItems(state.books.map { .result($0) }, toSection: .result)
snapshot.appendItems(state.bookInfos.map { .libraryResult($0) }, toSection: .result)
searchBar.setClearButtonMode(.always)
}
header.setTitle(.result(count: count))
layoutMode = .afterSearch
}
dataSource.apply(snapshot, animatingDifferences: true)
}
}
private extension SearchView {
func setupCollectionView() -> UICollectionView {
let collectionView = UICollectionView(frame: .zero, collectionViewLayout: createLayout())
collectionView.backgroundColor = .bkBaseColor(.primary)
collectionView.delegate = self
collectionView.alwaysBounceVertical = false
collectionView.contentInset = .zero
collectionView.scrollIndicatorInsets = .zero
collectionView.register(RecentKeywordCell.self, forCellWithReuseIdentifier: RecentKeywordCell.identifier)
collectionView.register(SearchResultCell.self, forCellWithReuseIdentifier: SearchResultCell.identifier)
return collectionView
}
func setupDataSource() -> UICollectionViewDiffableDataSource<SearchSection, SearchItem> {
let dataSource = UICollectionViewDiffableDataSource<SearchSection, SearchItem>(
collectionView: collectionView
) { collectionView, indexPath, item in
switch item {
case .query(let keyword):
return self.makeRecentKeywordCell(in: collectionView, at: indexPath, keyword: keyword)
case .result(let result):
return self.makeSearchResultCell(in: collectionView, at: indexPath, book: result)
case .libraryResult(let bookInfo):
return self.makeLibraryResultCell(in: collectionView, at: indexPath, bookInfo: bookInfo)
}
}
return dataSource
}
func makeRecentKeywordCell(
in collectionView: UICollectionView,
at indexPath: IndexPath,
keyword: String
) -> UICollectionViewCell {
guard let cell = collectionView.dequeueReusableCell(
withReuseIdentifier: RecentKeywordCell.identifier,
for: indexPath
) as? RecentKeywordCell else {
return UICollectionViewCell()
}
cell.configure(labelText: keyword)
cell.onDeleteTapped = { [weak self] in
self?.eventPublisher.send(.deleteRecentQuery(keyword))
}
cell.onQueryLabelTapped = { [weak self] in
self?.eventPublisher.send(.search(keyword))
self?.setSearchBarText(with: keyword)
}
return cell
}
func makeSearchResultCell(
in collectionView: UICollectionView,
at indexPath: IndexPath,
book: Book
) -> UICollectionViewCell {
guard let cell = collectionView.dequeueReusableCell(
withReuseIdentifier: SearchResultCell.identifier,
for: indexPath
) as? SearchResultCell else {
return UICollectionViewCell()
}
switch book.userBookStatus {
case .beforeRegistration, nil:
cell.configure(
title: book.title,
description: .init(
author: book.author,
publisher: book.publisher
),
image: book.thumbnail,
canSelect: true,
recordCount: book.recordCount
)
default:
cell.configure(
title: book.title,
description: .init(
author: book.author,
publisher: book.publisher
),
image: book.thumbnail,
canSelect: false,
recordCount: book.recordCount
)
}
return cell
}
func makeLibraryResultCell(
in collectionView: UICollectionView,
at indexPath: IndexPath,
bookInfo: BookInfo
) -> UICollectionViewCell {
guard let cell = collectionView.dequeueReusableCell(
withReuseIdentifier: SearchResultCell.identifier,
for: indexPath
) as? SearchResultCell else {
return UICollectionViewCell()
}
// BookInfo는 내 서재 도서 - 터치 가능하고 recordCount가 있으면 .record 스타일
cell.configure(
title: bookInfo.title,
description: .init(
author: bookInfo.author,
publisher: bookInfo.publisher
),
image: bookInfo.imageUrl,
canSelect: true,
recordCount: bookInfo.recordCount,
isLibraryBook: true
)
return cell
}
func createLayout() -> UICollectionViewLayout {
let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .vertical
layout.minimumLineSpacing = .zero
layout.minimumInteritemSpacing = .zero
layout.sectionInset = .zero
layout.estimatedItemSize = .zero // 정확한 사이즈 계산을 위해 추가
return layout
}
func makeEmptyLabel(
_ text: String,
verticalOffset: CGFloat = 0
) -> UIView {
let container = UIView()
container.backgroundColor = .bkBaseColor(.primary)
let label = UILabel()
label.text = text
label.textColor = .bkContentColor(.secondary)
label.textAlignment = .center
container.addSubview(label)
label.snp.makeConstraints {
$0.centerX.equalToSuperview()
$0.centerY.equalToSuperview().offset(verticalOffset)
}
return container
}
private func updateEmptyState(isHidden: Bool, title: String = "", description: String = "", buttontitle: String = "") {
emptyView.isHidden = isHidden
collectionView.isHidden = !isHidden
if !isHidden {
emptyView.setContent(title: title, description: description, actionTitle: buttontitle)
bringSubviewToFront(emptyView)
}
}
@objc
func searchButtonTapped() {
guard let text = searchBar.text, !text.isEmpty else { return }
eventPublisher.send(.search(text))
}
}
extension SearchView: UICollectionViewDelegateFlowLayout {
func collectionView(
_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
sizeForItemAt indexPath: IndexPath
) -> CGSize {
switch layoutMode {
case .beforeSearch:
return CGSize(
width: collectionView.bounds.width,
height: LayoutConstants.recentItemHeight
)
case .afterSearch:
return CGSize(
width: collectionView.bounds.width,
height: LayoutConstants.resultItemHeight
)
}
}
}
extension SearchView: UICollectionViewDelegate {
func collectionView(
_ collectionView: UICollectionView,
willDisplay cell: UICollectionViewCell,
forItemAt indexPath: IndexPath
) {
guard layoutMode == .afterSearch else { return }
let section = indexPath.section
let totalItems = collectionView.numberOfItems(inSection: section)
if indexPath.item == totalItems - 1 {
eventPublisher.send(.loadNextPage)
}
}
func collectionView(
_ collectionView: UICollectionView,
didSelectItemAt indexPath: IndexPath
) {
guard let item = dataSource.itemIdentifier(for: indexPath) else { return }
switch item {
case .result(let book):
switch book.userBookStatus {
case .beforeRegistration, nil:
eventPublisher.send(.upsertBook(book.isbn))
default:
break
}
case .libraryResult(let bookInfo):
eventPublisher.send(.goToBookDetail(isbn: bookInfo.isbn, userBookId: bookInfo.bookId))
case .query: break
}
}
}
private extension SearchView {
enum LayoutConstants {
static let searchBarInset = BKInset.inset3
static let dividerOffset = BKInset.inset2
static let horizontalInset = BKInset.inset5
static let headerOffset = BKInset.inset1
static let recentItemHeight: CGFloat = 56
static let resultItemHeight: CGFloat = 132
}
}