-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEmotionRegistrationViewController.swift
More file actions
518 lines (435 loc) · 21.3 KB
/
EmotionRegistrationViewController.swift
File metadata and controls
518 lines (435 loc) · 21.3 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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
//
// EmotionRegistrationViewController.swift
// Presentation
//
// Created by 이동현 on 8/19/25.
//
import Combine
import SnapKit
import UIKit
final class EmotionRegistrationViewController: BaseViewController<EmotionRegisterViewModel> {
private enum Layout {
static let smallMarbleScrollViewHeight: CGFloat = 40
static let smallMarbleScrollViewTopSpacing: CGFloat = 70
static let marbleStackViewHorizontalSpacing: CGFloat = 20
static let marbleStackViewContentSpacing: CGFloat = 19
static let smallMarbleImageSize: CGFloat = 40
static let emotionLabelWidth: CGFloat = 92
static let emotionLabelHeight: CGFloat = 36
static let emotionCollectionViewHeight: CGFloat = 291
static let emotionMarbleImageViewSize: CGFloat = 140
static let emotionMarbleImageViewTopSpacing: CGFloat = 13
static let speechImageTopSpacing: CGFloat = 26
static let speechImageHorizontalSpacing: CGFloat = 54
static let speechImageHeight: CGFloat = 102
static let speechLabelTopSpacing: CGFloat = 22
static let fomoHandImageWidth: CGFloat = 263
static let fomoHandImageHeight: CGFloat = 207
static let fomoThumbImageWidth: CGFloat = 80
static let fomoThumbImageHeight: CGFloat = 65
static let foromThumbTopSpacing: CGFloat = 64
static let foromThumbLeadingSpacing: CGFloat = 71
static let handMarbleImageViewBottomSpacing: CGFloat = 7
static let handMarbleImageViewTopSpacing: CGFloat = 50
static let infoLabelTopSpacing: CGFloat = -70
static let doubleChevronIconSize: CGFloat = 24
static let doubleChevronIconHorizontalSpacing: CGFloat = 26
static let doubleChevronIconTopSpacing: CGFloat = 10
static let infoViewSize: CGFloat = 0
static let infoSwipeOverlayViewHeight = 100
}
private let smallMarbleScrollView = UIScrollView()
private let smallMarbleStackView = UIStackView()
private let emotionLabel = UILabel()
private let emotionCollectionView = UICollectionView(frame: .zero, collectionViewLayout: EmotionCollectionViewLayout())
private let emotionMarbleImageView = UIImageView()
private let handMarbleView = UIView()
private let speechImageView = UIImageView()
private let speechLabel = UILabel()
private let handImageView = UIImageView()
private let thumbImageView = UIImageView()
private let infoView = UIView()
private let infoLabel = UILabel()
private let leftDoubleChevronImageView = UIImageView()
private let rightDoubleChevronImageView = UIImageView()
private let downDoubleChevronImageView = UIImageView()
private let infoSwipeOverlayView = UIView()
private let hapticGenerator = UISelectionFeedbackGenerator()
private let itemSetCount = 7
private var isMarbleHeld = false
private var marbleImageViewMidY: CGFloat?
private var marbleImageViewPanGesture: UIPanGestureRecognizer?
private var emotion: Emotion?
private var dataSource: UICollectionViewDiffableDataSource<Int, Emotion>?
private var cancellables: Set<AnyCancellable>
private var lastMarbleCellIndex: Int = 0
var itemCount: Int {
return (dataSource?.snapshot().numberOfItems ?? 0) / itemSetCount
}
override init(viewModel: EmotionRegisterViewModel) {
cancellables = []
super.init(viewModel: viewModel)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
super.viewDidLoad()
viewModel.action(input: .fetchEmotions)
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
if view.frame.maxY == view.safeAreaLayoutGuide.layoutFrame.maxY {
handMarbleView.snp.remakeConstraints { make in
make.centerX.equalToSuperview()
make.top.equalTo(infoLabel.snp.bottom).offset(Layout.handMarbleImageViewTopSpacing)
make.size.equalTo(Layout.emotionMarbleImageViewSize)
}
handImageView.isHidden = true
thumbImageView.isHidden = true
}
if marbleImageViewMidY == nil { marbleImageViewMidY = emotionMarbleImageView.center.y }
}
override func configureAttribute() {
view.backgroundColor = .white
configureMarbleImageView()
configureSmallMarbleViews()
configureCustomNavigationBar(navigationBarStyle: .withBackButton(title: "오늘 감정 등록하기"))
emotionCollectionView.register(EmotionCollectionViewCell.self, forCellWithReuseIdentifier: EmotionCollectionViewCell.className)
emotionCollectionView.delegate = self
emotionCollectionView.showsHorizontalScrollIndicator = false
emotionCollectionView.decelerationRate = .fast
emotionLabel.layer.cornerRadius = 10
emotionLabel.layer.masksToBounds = true
emotionLabel.font = BitnagilFont(style: .title3, weight: .semiBold).font
emotionLabel.textAlignment = .center
speechImageView.image = BitnagilGraphic.marbleSpeechGraphic?.withRenderingMode(.alwaysTemplate)
speechImageView.tintColor = .clear
speechLabel.numberOfLines = 2
speechLabel.textAlignment = .center
speechLabel.font = BitnagilFont.init(style:.cafe24Title2, weight: .light).font
speechLabel.textColor = .clear
infoSwipeOverlayView.backgroundColor = .clear
infoSwipeOverlayView.isUserInteractionEnabled = false
infoLabel.numberOfLines = 0
infoLabel.font = BitnagilFont.init(style: .body2, weight: .medium).font
infoLabel.textColor = BitnagilColor.gray50
infoLabel.textAlignment = .center
leftDoubleChevronImageView.image = BitnagilIcon.doubleChevronIcon(direction: .left)
rightDoubleChevronImageView.image = BitnagilIcon.doubleChevronIcon(direction: .right)
downDoubleChevronImageView.image = BitnagilIcon.doubleChevronIcon(direction: .down)
handImageView.image = BitnagilGraphic.fomoHandGraphic
thumbImageView.image = BitnagilGraphic.fomoThumbGraphic
[
leftDoubleChevronImageView,
rightDoubleChevronImageView,
downDoubleChevronImageView].forEach {
$0.tintColor = BitnagilColor.gray60
}
dataSource = UICollectionViewDiffableDataSource<Int, Emotion>(collectionView: emotionCollectionView) { collectionView, indexPath, item in
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: EmotionCollectionViewCell.className, for: indexPath) as? EmotionCollectionViewCell else { return UICollectionViewCell() }
cell.configure(image: item.image)
return cell
}
}
override func configureLayout() {
let safeArea = view.safeAreaLayoutGuide
view.addSubview(smallMarbleScrollView)
view.addSubview(emotionCollectionView)
view.addSubview(emotionLabel)
view.addSubview(speechImageView)
view.addSubview(speechLabel)
view.addSubview(handImageView)
view.addSubview(emotionMarbleImageView)
view.addSubview(handMarbleView)
view.addSubview(thumbImageView)
view.addSubview(infoView)
view.addSubview(infoSwipeOverlayView)
smallMarbleScrollView.addSubview(smallMarbleStackView)
infoView.addSubview(infoLabel)
infoView.addSubview(leftDoubleChevronImageView)
infoView.addSubview(rightDoubleChevronImageView)
infoView.addSubview(downDoubleChevronImageView)
smallMarbleScrollView.snp.makeConstraints { make in
make.height.equalTo(Layout.smallMarbleScrollViewHeight)
make.horizontalEdges.equalToSuperview()
make.top.equalTo(safeArea.snp.top).offset(Layout.smallMarbleScrollViewTopSpacing)
}
smallMarbleStackView.snp.makeConstraints { make in
make.verticalEdges.equalTo(smallMarbleScrollView.contentLayoutGuide)
make.horizontalEdges.equalTo(smallMarbleScrollView.contentLayoutGuide).inset(Layout.marbleStackViewHorizontalSpacing)
make.height.equalTo(smallMarbleScrollView.snp.height)
make.width.equalTo(smallMarbleScrollView.frameLayoutGuide).priority(.low)
}
speechImageView.snp.makeConstraints { make in
make.top.equalTo(smallMarbleStackView.snp.bottom).offset(Layout.speechImageTopSpacing)
make.horizontalEdges.equalToSuperview().inset(Layout.speechImageHorizontalSpacing)
make.height.equalTo(Layout.speechImageHeight)
}
speechLabel.snp.makeConstraints { make in
make.top.equalTo(speechImageView.snp.top).offset(Layout.speechLabelTopSpacing)
make.centerX.equalTo(speechImageView)
}
emotionCollectionView.snp.makeConstraints {
$0.top.equalTo(speechImageView.snp.bottom).offset(17)
$0.leading.trailing.equalToSuperview()
$0.height.equalTo(Layout.emotionCollectionViewHeight)
}
emotionLabel.snp.makeConstraints { make in
make.centerX.equalToSuperview()
make.top.equalTo(emotionCollectionView)
make.height.equalTo(Layout.emotionLabelHeight)
make.width.equalTo(Layout.emotionLabelWidth)
}
emotionMarbleImageView.snp.makeConstraints { make in
make.top.equalTo(emotionLabel.snp.bottom).offset(Layout.emotionMarbleImageViewTopSpacing)
make.centerX.equalToSuperview()
make.size.equalTo(Layout.emotionMarbleImageViewSize)
}
infoView.snp.makeConstraints { make in
make.top.equalTo(emotionCollectionView.snp.bottom).offset(Layout.infoLabelTopSpacing)
make.centerX.equalToSuperview()
make.width.equalTo(Layout.infoViewSize).priority(.low)
make.height.equalTo(Layout.infoViewSize).priority(.low)
}
infoLabel.snp.makeConstraints { make in
make.top.equalToSuperview()
make.centerX.equalToSuperview()
}
leftDoubleChevronImageView.snp.makeConstraints { make in
make.centerY.equalTo(infoLabel)
make.trailing.equalTo(infoLabel.snp.leading).offset(-Layout.doubleChevronIconHorizontalSpacing)
make.size.equalTo(Layout.doubleChevronIconSize)
}
rightDoubleChevronImageView.snp.makeConstraints { make in
make.centerY.equalTo(infoLabel)
make.leading.equalTo(infoLabel.snp.trailing).offset(Layout.doubleChevronIconHorizontalSpacing)
make.size.equalTo(Layout.doubleChevronIconSize)
}
downDoubleChevronImageView.snp.makeConstraints { make in
make.centerX.equalTo(infoLabel)
make.top.equalTo(infoLabel.snp.bottom).offset(Layout.doubleChevronIconTopSpacing)
make.size.equalTo(Layout.doubleChevronIconSize)
}
handImageView.snp.makeConstraints { make in
make.bottom.equalToSuperview()
make.centerX.equalToSuperview()
make.height.equalTo(Layout.fomoHandImageHeight)
make.width.equalTo(Layout.fomoHandImageWidth)
}
thumbImageView.snp.makeConstraints { make in
make.top.equalTo(handImageView.snp.top).offset(Layout.foromThumbTopSpacing)
make.leading.equalTo(handImageView.snp.leading).offset(Layout.foromThumbLeadingSpacing)
make.height.equalTo(Layout.fomoThumbImageHeight)
make.width.equalTo(Layout.fomoThumbImageWidth)
}
handMarbleView.snp.makeConstraints { make in
make.centerX.equalToSuperview()
make.bottom.equalTo(thumbImageView.snp.bottom).offset(-Layout.handMarbleImageViewBottomSpacing)
make.size.equalTo(Layout.emotionMarbleImageViewSize)
}
infoSwipeOverlayView.snp.makeConstraints { make in
make.horizontalEdges.equalToSuperview()
make.bottom.equalTo(emotionCollectionView.snp.bottom)
make.height.equalTo(Layout.infoSwipeOverlayViewHeight)
}
}
override func bind() {
viewModel.output.emotionListPublisher
.receive(on: DispatchQueue.main)
.sink {[weak self] emotions in
guard let self = self else { return }
let originalEmotionCount = emotions.count
let centerIndex = itemSetCount / 2 + 1
let multipliedEmotions = (0..<itemSetCount).flatMap { i in
i == centerIndex ? emotions : emotions.map { $0.copy() }
}
guard
let layout = emotionCollectionView.collectionViewLayout as? EmotionCollectionViewLayout,
let currentIndex = layout.fetchcurrentIndex()
else { return }
lastMarbleCellIndex = currentIndex
var snapshot = NSDiffableDataSourceSnapshot<Int, Emotion>()
snapshot.appendSections([0])
snapshot.appendItems(multipliedEmotions)
self.dataSource?.apply(snapshot, animatingDifferences: false)
self.scrollToIndex(index: originalEmotionCount)
viewModel.action(input: .selectEmotion(index: 0))
}
.store(in: &cancellables)
viewModel.output.selectedEmotionPublisher
.receive(on: DispatchQueue.main)
.sink { [weak self] emotion in
guard
let self = self,
let emotion
else { return }
self.emotion = emotion
UIView.animate(
withDuration: 0.4,
delay: 0,
options: [.curveEaseInOut]
) {
self.emotionMarbleImageView.image = emotion.image
self.emotionLabel.backgroundColor = emotion.backgroundColor
self.speechImageView.tintColor = emotion.backgroundColor
self.speechLabel.textColor = emotion.textColor
self.emotionLabel.textColor = emotion.textColor
self.speechLabel.text = emotion.emotionMessage
self.emotionLabel.text = emotion.emotionTitle
self.emotionMarbleImageView.isHidden = false
}
}
.store(in: &cancellables)
viewModel.output.confirmEmotionEnabledPublisher
.receive(on: DispatchQueue.main)
.sink { [weak self] canConfirmEmotion in
self?.emotionMarbleImageView.isUserInteractionEnabled = canConfirmEmotion
self?.rightDoubleChevronImageView.isHidden = canConfirmEmotion
self?.leftDoubleChevronImageView.isHidden = canConfirmEmotion
self?.downDoubleChevronImageView.isHidden = !canConfirmEmotion
let infoMessage = canConfirmEmotion
? """
선택한 감정 구슬을 아래로 놓아주세요
"""
: """
좌우로 스와이프해
감정 구슬을 골라주세요
"""
self?.infoLabel.text = infoMessage
}
.store(in: &cancellables)
}
private func configureSmallMarbleViews() {
smallMarbleScrollView.showsHorizontalScrollIndicator = false
smallMarbleStackView.axis = .horizontal
smallMarbleStackView.distribution = .equalSpacing
smallMarbleStackView.spacing = Layout.marbleStackViewContentSpacing
Marble.allCases.dropFirst().forEach {
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(handleTapSmallMarbleImage(gesture:)))
let smallMarbleImageView = UIImageView()
smallMarbleImageView.image = $0.marbleImage
smallMarbleImageView.addGestureRecognizer(tapGesture)
smallMarbleImageView.isUserInteractionEnabled = true
smallMarbleImageView.snp.makeConstraints { make in
make.size.equalTo(Layout.smallMarbleImageSize)
}
smallMarbleStackView.addArrangedSubview(smallMarbleImageView)
}
}
private func configureMarbleImageView() {
marbleImageViewPanGesture = UIPanGestureRecognizer(target: self, action: #selector(handleEmotionCollectionViewPanning(gesture:)))
handMarbleView.layer.cornerRadius = Layout.emotionMarbleImageViewSize / 2
handMarbleView.layer.masksToBounds = true
handMarbleView.backgroundColor = .clear
emotionMarbleImageView.isUserInteractionEnabled = true
emotionMarbleImageView.layer.cornerRadius = Layout.emotionMarbleImageViewSize / 2
emotionMarbleImageView.layer.masksToBounds = true
guard let marbleImageViewPanGesture else { return }
emotionMarbleImageView.addGestureRecognizer(marbleImageViewPanGesture)
}
@objc private func handleTapSmallMarbleImage(gesture: UITapGestureRecognizer) {
guard
let tappedSmallMarbleView = gesture.view,
let index = smallMarbleStackView.arrangedSubviews.firstIndex(of: tappedSmallMarbleView)
else { return }
scrollToIndex(index: index + 1, animated: true) { [weak self] in
self?.viewModel.action(input: .selectEmotion(index: index + 1))
}
}
@objc private func handleEmotionCollectionViewPanning(gesture: UIPanGestureRecognizer) {
guard let marbleImageViewMidY else { return }
switch gesture.state {
case .began:
infoView.isHidden = true
case .changed:
let translationY = gesture.translation(in: view).y
let offSet = max(0, translationY)
guard offSet <= handMarbleView.frame.midY - marbleImageViewMidY else { return }
emotionMarbleImageView.transform = CGAffineTransform(translationX: 0, y: offSet)
case .ended, .cancelled, .failed:
if emotionMarbleImageView.frame.maxY > handMarbleView.frame.minY + (Layout.emotionMarbleImageViewSize / 2) {
let dy = handMarbleView.frame.midY - marbleImageViewMidY
UIView.animate(
withDuration: 0.3,
delay: 0,
options: [.curveEaseInOut]
) {
self.emotionMarbleImageView.transform = CGAffineTransform(translationX: 0, y: dy)
} completion: { _ in
guard let emotion = self.emotion else { return }
let completionViewController = EmotionRegisterCompletionViewController(emotion: emotion)
DispatchQueue.main.asyncAfter(deadline: .now() + 0.8) {
self.navigationController?.pushViewController(completionViewController, animated: true)
}
}
} else {
UIView.animate(
withDuration: 0.2,
delay: 0,
options: [.curveEaseInOut]
) {
self.emotionMarbleImageView.transform = .identity
self.infoView.isHidden = false
}
}
default:
break
}
}
private func scrollToIndex(
index: Int,
animated: Bool = false,
completion: (() -> Void)? = nil
) {
guard itemCount > 0 else { return }
let index = (index + itemCount) % itemCount
let targetIndex = index + itemCount
let indexPath = IndexPath(item: targetIndex, section: 0)
emotionMarbleImageView.isHidden = true
emotionCollectionView.scrollToItem(
at: indexPath,
at: .centeredHorizontally,
animated: animated)
if !animated {
completion?()
} else {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
completion?()
}
}
}
}
extension EmotionRegistrationViewController: UICollectionViewDelegate {
}
extension EmotionRegistrationViewController: UIScrollViewDelegate {
func scrollViewDidScroll(_ scrollView: UIScrollView) {
guard
let layout = emotionCollectionView.collectionViewLayout as? EmotionCollectionViewLayout,
let currentIndex = layout.fetchcurrentIndex(),
lastMarbleCellIndex != currentIndex
else { return }
lastMarbleCellIndex = currentIndex
hapticGenerator.selectionChanged()
hapticGenerator.prepare()
}
func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
emotionMarbleImageView.isHidden = true
hapticGenerator.prepare()
}
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
let centerPoint = CGPoint(
x: emotionCollectionView.contentOffset.x + emotionCollectionView.frame.midX,
y: emotionCollectionView.contentOffset.y)
guard
let collectionView = scrollView as? UICollectionView,
let indexPath = collectionView.indexPathForItem(at: centerPoint),
itemCount > 0
else { return }
let index = indexPath.row % itemCount
if indexPath.row < itemCount / itemSetCount || indexPath.row >= itemCount * (itemSetCount / 2 + 1) / itemSetCount {
scrollToIndex(index: index)
}
viewModel.action(input: .selectEmotion(index: index))
}
}