-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBookDetailViewCell.swift
More file actions
190 lines (162 loc) · 5.96 KB
/
Copy pathBookDetailViewCell.swift
File metadata and controls
190 lines (162 loc) · 5.96 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
// Copyright © 2025 Booket. All rights reserved
import BKDesign
import SnapKit
import UIKit
final class BookDetailViewCell: UICollectionViewCell {
static let reuseIdentifier: String = "BookDetailViewCell"
private let noteLabel = BKLabel(
fontStyle: .body2(weight: .medium),
color: .bkContentColor(.secondary)
)
private let lowerStack: UIStackView = {
let stackView = UIStackView()
stackView.axis = .horizontal
stackView.alignment = .center
stackView.distribution = .equalSpacing
return stackView
}()
private let emotionIcon = UIImageView()
private let emotionLabel = BKLabel2(
fontStyle: .body1(weight: .semiBold),
color: .bkContentColor(.brand)
)
private let creationLabel = BKLabel2(
fontStyle: .label1(weight: .medium),
color: .bkContentColor(.tertiary)
)
private let pageLabel = BKLabel2(
fontStyle: .italic,
color: .bkContentColor(.tertiary)
)
private let moreButton: UIImageView = {
let imageView = UIImageView(
image: BKImage.Icon.moreVertical
.withRenderingMode(.alwaysTemplate)
)
imageView.tintColor = .bkContentColor(.tertiary)
imageView.isUserInteractionEnabled = true
return imageView
}()
private var moreButtonAction: (() -> Void)?
private let upperStack: UIStackView = {
let stackView = UIStackView()
stackView.axis = .horizontal
stackView.alignment = .center
stackView.distribution = .equalSpacing
return stackView
}()
private let emotionStack: UIStackView = {
let stackView = UIStackView()
stackView.axis = .horizontal
stackView.spacing = LayoutConstants.emotionStackSpacing
stackView.alignment = .center
return stackView
}()
override init(frame: CGRect) {
super.init(frame: frame)
setupViews()
configure()
setupLayout()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func prepareForReuse() {
super.prepareForReuse()
noteLabel.setText(text: "")
emotionIcon.image = nil
emotionLabel.setText(text: "")
creationLabel.setText(text: "")
pageLabel.setText(text: "")
}
func configure(
with item: BookDetailItem
) {
let emotion = item.emotion ?? .joy
let displayedNote = "\"\(item.note)\""
noteLabel.setText(text: displayedNote)
emotionIcon.image = emotion.circleImage
emotionIcon.contentMode = .scaleAspectFit
emotionLabel.setText(text: "#\(emotion.rawValue)")
creationLabel.setText(text: item.createdAt.toKoreanDotDateString())
pageLabel.setText(text: "\(item.page)p")
}
func applyMoreButtonGesture(
action: @escaping () -> Void
) {
moreButtonAction = action
let tap = UITapGestureRecognizer(
target: self,
action: #selector(handleMoreButtonTapped)
)
moreButton.addGestureRecognizer(tap)
}
}
private extension BookDetailViewCell {
func setupViews() {
contentView.addSubviews(upperStack, noteLabel, lowerStack)
[emotionStack, moreButton].forEach(upperStack.addArrangedSubview)
[creationLabel, pageLabel].forEach(lowerStack.addArrangedSubview)
[emotionIcon, emotionLabel].forEach(emotionStack.addArrangedSubview)
}
func configure() {
noteLabel.numberOfLines = Constants.noteMaxNumberOfLines
noteLabel.lineBreakMode = .byTruncatingTail
backgroundColor = .bkBaseColor(.secondary)
layer.cornerRadius = LayoutConstants.cornerRadius
clipsToBounds = true
}
func setupLayout() {
contentView.snp.makeConstraints {
$0.edges.equalToSuperview()
$0.width.equalTo(UIScreen.main.bounds.width - LayoutConstants.horizontalInset * 2)
}
upperStack.snp.makeConstraints {
$0.top.equalToSuperview()
.inset(LayoutConstants.topInset)
$0.horizontalEdges.equalToSuperview()
.inset(LayoutConstants.horizontalInset)
}
noteLabel.snp.makeConstraints {
$0.top.equalTo(upperStack.snp.bottom)
.offset(LayoutConstants.noteLabelTopInset)
$0.horizontalEdges.equalToSuperview()
.inset(LayoutConstants.horizontalInset)
}
lowerStack.snp.makeConstraints {
$0.top.equalTo(noteLabel.snp.bottom)
.offset(LayoutConstants.lowerLabelTopInset)
$0.horizontalEdges.equalToSuperview()
.inset(LayoutConstants.horizontalInset)
$0.bottom.equalToSuperview()
.inset(LayoutConstants.bottomInset)
}
moreButton.snp.makeConstraints {
$0.size.equalTo(LayoutConstants.moreButtonSize)
}
emotionIcon.snp.makeConstraints {
$0.size.equalTo(LayoutConstants.emotionImageSize)
}
}
@objc func handleMoreButtonTapped() {
moreButtonAction?()
}
}
private extension BookDetailViewCell {
enum LayoutConstants {
static let emotionStackSpacing = BKSpacing.spacing2
static let noteLabelTopInset = BKInset.inset3
static let lowerLabelTopInset = BKInset.inset2
static let horizontalInset = BKInset.inset5
static let contentSpacing = BKSpacing.spacing4
static let topInset = BKInset.inset5
static let bottomInset = BKInset.inset4
static let cornerRadius = BKRadius.medium
static let emotionImageSize: CGSize = CGSize(width: 32, height: 32)
static let moreButtonSize: CGSize = CGSize(width: 20, height: 20)
static let imageCornerRadius: CGFloat = 20
}
enum Constants {
static let noteMaxNumberOfLines: Int = 4
}
}