-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRoutineCreationCardView.swift
More file actions
299 lines (249 loc) · 10.5 KB
/
RoutineCreationCardView.swift
File metadata and controls
299 lines (249 loc) · 10.5 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
//
// RoutineCreationCardView.swift
// Presentation
//
// Created by 이동현 on 8/11/25.
//
import SnapKit
import UIKit
fileprivate enum Layout {
static let edgeSpacing: CGFloat = 18
static let titleImageSize: CGFloat = 24
static let titleImageTrailingSpacing: CGFloat = 15
static let titleLabelLargeHeight: CGFloat = 24
static let titleLabelSmallHeight: CGFloat = 20
static let titleLabelTrailingSpacing: CGFloat = 5
static let placeHolderLabelHeight: CGFloat = 20
static let subTitleLabelHeight: CGFloat = 24
static let contentLabelStackViewSpacing: CGFloat = 1
static let infoImageSize: CGFloat = 16
static let asteriskImageSize: CGFloat = 12
static let chevronDownImageWidth: CGFloat = 20
static let chevronDownImageHeight: CGFloat = 20
static let chevronDownImageTrailingSpacing: CGFloat = 28
static let divideLineTopSpacing: CGFloat = 14
static let divideLineHeight: CGFloat = 1
static let contentViewHeight: CGFloat = 0
}
final class RoutineCreationCardView<ContentView: UIView & RoutineCreationExpandable>: UIView, UIGestureRecognizerDelegate {
private let titleImageView = UIImageView()
private let labelStackView = UIStackView()
private let titleLabel = UILabel()
private let placeHolderLabel = UILabel()
private let infoImageView = UIImageView()
private let asteriskImageView = UIImageView()
private let chevronImageView = UIImageView()
private let divideLine = UIView()
private let contentView = ContentView()
private var labelStackViewBottomConstraint: Constraint?
private var divideLineTopConstraint: Constraint?
public var onAction: ((ContentView.Action) -> Void)? {
didSet { contentView.action = onAction }
}
init(
title: String,
placeHolder: String,
titleImage: UIImage?,
withInfoImage: Bool,
withAsteriskImage: Bool
) {
super.init(frame: .zero)
configureAttribute()
configureLayout()
titleLabel.text = title
placeHolderLabel.text = placeHolder
titleImageView.image = titleImage
infoImageView.isHidden = !withInfoImage
asteriskImageView.isHidden = !withAsteriskImage
divideLine.isHidden = true
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func configure(
image: UIImage,
title: String,
contents: [String]
) {
titleImageView.image = image
titleLabel.text = title
}
private func configureAttribute() {
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(handleTap(_:)))
tapGesture.delegate = self
addGestureRecognizer(tapGesture)
backgroundColor = BitnagilColor.gray99
layer.cornerRadius = 12
layer.masksToBounds = true
labelStackView.spacing = Layout.contentLabelStackViewSpacing
labelStackView.axis = .vertical
labelStackView.distribution = .fill
labelStackView.alignment = .leading
titleLabel.font = BitnagilFont.init(style: .body1, weight: .semiBold).font
titleLabel.textColor = .black
placeHolderLabel.font = BitnagilFont.init(style: .body2, weight: .medium).font
placeHolderLabel.textColor = BitnagilColor.gray70
infoImageView.image = BitnagilIcon.informationIcon
asteriskImageView.image = BitnagilIcon.asteriskIcon
chevronImageView.image = BitnagilIcon.chevronIcon(direction: .down)?.withRenderingMode(.alwaysTemplate)
chevronImageView.tintColor = BitnagilColor.gray30
divideLine.backgroundColor = BitnagilColor.gray96
contentView.isHidden = true
contentView.action = { [weak self] in
self?.onAction?($0)
}
}
private func configureLayout() {
addSubview(titleImageView)
addSubview(labelStackView)
addSubview(asteriskImageView)
addSubview(infoImageView)
addSubview(contentView)
addSubview(chevronImageView)
addSubview(divideLine)
labelStackView.addArrangedSubview(titleLabel)
labelStackView.addArrangedSubview(placeHolderLabel)
titleImageView.snp.makeConstraints { make in
make.centerY.equalTo(labelStackView)
make.leading.equalToSuperview().offset(Layout.edgeSpacing)
make.size.equalTo(Layout.titleImageSize)
}
labelStackView.snp.makeConstraints { make in
make.top.equalToSuperview().offset(Layout.edgeSpacing)
make.leading.equalTo(titleImageView.snp.trailing).offset(Layout.titleImageTrailingSpacing)
make.trailing.equalToSuperview().offset(-Layout.edgeSpacing)
labelStackViewBottomConstraint = make.bottom.equalToSuperview()
.offset(-Layout.edgeSpacing)
.priority(.medium)
.constraint
}
titleLabel.snp.makeConstraints { make in
make.height.equalTo(Layout.titleLabelLargeHeight)
}
placeHolderLabel.snp.makeConstraints { make in
make.height.equalTo(Layout.placeHolderLabelHeight)
}
infoImageView.snp.makeConstraints { make in
make.leading.equalTo(titleLabel.snp.trailing).offset(Layout.titleLabelTrailingSpacing)
make.centerY.equalTo(titleLabel)
make.size.equalTo(Layout.infoImageSize)
}
asteriskImageView.snp.makeConstraints { make in
make.leading.equalTo(titleLabel.snp.trailing).offset(Layout.titleLabelTrailingSpacing)
make.centerY.equalTo(titleLabel)
make.size.equalTo(Layout.asteriskImageSize)
}
chevronImageView.snp.makeConstraints { make in
make.centerY.equalTo(titleImageView)
make.height.equalTo(Layout.chevronDownImageHeight)
make.width.equalTo(Layout.chevronDownImageWidth)
make.trailing.equalToSuperview().offset(-Layout.chevronDownImageTrailingSpacing)
}
divideLine.snp.makeConstraints { make in
make.horizontalEdges.equalToSuperview().inset(Layout.edgeSpacing)
make.height.equalTo(Layout.divideLineHeight)
divideLineTopConstraint = make.top
.equalTo(labelStackView.snp.bottom)
.offset(CGFloat.zero).constraint
}
contentView.snp.makeConstraints { make in
make.top.equalTo(divideLine.snp.bottom)
make.horizontalEdges.equalToSuperview()
make.bottom.equalToSuperview().offset(-Layout.edgeSpacing)
}
}
func configure(subTitles: [String]) {
labelStackView.arrangedSubviews.dropFirst(2).forEach { subview in
labelStackView.removeArrangedSubview(subview)
subview.removeFromSuperview()
}
for text in subTitles {
let label = UILabel()
label.text = text
label.font = BitnagilFont(style: .body1, weight: .semiBold).font
label.textColor = BitnagilColor.gray10
label.numberOfLines = 1
labelStackView.addArrangedSubview(label)
label.snp.makeConstraints { make in
make.height.equalTo(Layout.subTitleLabelHeight)
}
}
titleLabel.snp.updateConstraints { make in
make.height.equalTo(subTitles.isEmpty ? Layout.titleLabelLargeHeight : Layout.titleLabelSmallHeight)
}
updateHeaderVisibility()
setNeedsLayout()
}
func configure(dependency: ContentView.Dependency) {
contentView.configure(dependency: dependency)
}
@objc private func handleTap(_ gestureRecognizer: UIGestureRecognizer) {
let location = gestureRecognizer.location(in: self)
if location.y < labelStackView.frame.maxY {
toggleExpand()
}
}
private func configureContents(contents: [String]) {
defer {
updateHeaderVisibility()
setNeedsLayout()
}
guard !contents.isEmpty else { return }
labelStackView
.arrangedSubviews
.dropFirst(2)
.forEach { $0.removeFromSuperview() }
contents.forEach {
let label = UILabel()
label.text = $0
label.font = BitnagilFont.init(style: .body1, weight: .semiBold).font
label.textColor = .black
labelStackView.addArrangedSubview(label)
label.snp.makeConstraints { make in
make.height.equalTo(Layout.subTitleLabelHeight)
}
}
}
private func toggleExpand() {
let isExpanded = !(labelStackViewBottomConstraint?.isActive ?? false)
let nextExpandedState = !isExpanded
if nextExpandedState {
labelStackViewBottomConstraint?.isActive = false
contentView.isHidden = false
divideLine.isHidden = false
chevronImageView.image = BitnagilIcon.chevronIcon(direction: .up)
divideLineTopConstraint?.update(offset: Layout.edgeSpacing)
} else {
labelStackViewBottomConstraint?.isActive = true
contentView.isHidden = true
divideLine.isHidden = true
chevronImageView.image = BitnagilIcon.chevronIcon(direction: .down)
divideLineTopConstraint?.update(offset: CGFloat.zero)
}
contentView.setExpanded(expanded: nextExpandedState)
updateHeaderVisibility()
self.layoutIfNeeded()
}
private func updateHeaderVisibility() {
let arranged = labelStackView.arrangedSubviews
let subtitles = arranged.dropFirst(2)
let hasSubtitles = !subtitles.isEmpty
let isExpanded = !contentView.isHidden
let titleHeight: CGFloat = {
if isExpanded { return Layout.titleLabelLargeHeight }
return hasSubtitles ? Layout.titleLabelSmallHeight : Layout.titleLabelLargeHeight
}()
placeHolderLabel.isHidden = isExpanded || hasSubtitles
subtitles.forEach { $0.isHidden = isExpanded }
titleLabel.snp.updateConstraints { make in
make.height.equalTo(titleHeight)
}
if titleHeight == Layout.titleLabelLargeHeight {
titleLabel.font = isExpanded ? BitnagilFont.init(style: .body2, weight: .semiBold).font : BitnagilFont.init(style: .body1, weight: .semiBold).font
titleLabel.textColor = BitnagilColor.gray10
} else {
titleLabel.font = BitnagilFont.init(style: .body2, weight: .medium).font
titleLabel.textColor = BitnagilColor.gray50
}
}
}