-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSentenceAppreciationView.swift
More file actions
165 lines (135 loc) · 5.1 KB
/
SentenceAppreciationView.swift
File metadata and controls
165 lines (135 loc) · 5.1 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
// Copyright © 2025 Booket. All rights reserved
import BKDesign
import Combine
import SnapKit
import UIKit
struct SentenceAppreciationForm {
let appreciation: String?
}
final class SentenceAppreciationView: BaseView {
private let textDidChangeSubject = PassthroughSubject<Void, Never>()
private let containerView = UIView()
private let titleLabel = BKLabel(
text: "문장에 대한 감상을 남겨주세요",
fontStyle: .heading1(weight: .bold)
)
private let optionBadge = BadgeView(title: "선택")
private let subtitleLabel = BKLabel(
text: "떠오르는 생각이 있다면 자유롭게 작성해 주세요.",
fontStyle: .label1(weight: .medium),
color: .bkContentColor(.tertiary)
)
private let appreciationTextView = BKTextView(
placeholder: "내용을 입력해주세요."
)
private let titleRowStack: UIStackView = {
let stackView = UIStackView()
stackView.axis = .horizontal
stackView.spacing = LayoutConstants.titleRowStackSpacing
stackView.alignment = .center
return stackView
}()
private let titleStack: UIStackView = {
let stackView = UIStackView()
stackView.axis = .vertical
stackView.spacing = LayoutConstants.titleStackSpacing
stackView.alignment = .leading
return stackView
}()
private let guideButton = BKButton(
style: .stroke,
size: .rounded
)
private let tooltipView = TooltipView(text: "예시 문장을 알려드려요")
var onAppreciationTextViewFocused: (() -> Void)?
var appreciationTextViewFrame: CGRect {
return appreciationTextView.frame
}
var guideButtonFrame: CGRect {
return guideButton.frame
}
init(guideButtonAction: @escaping () -> Void) {
super.init(frame: .zero)
guideButton.addAction(UIAction { [weak self] _ in
guideButtonAction()
self?.tooltipView.isHidden = true
}, for: .touchUpInside)
setupTextViewFocusHandling()
}
deinit {
NotificationCenter.default.removeObserver(self)
}
override func setupView() {
addSubviews(titleStack, appreciationTextView, guideButton, tooltipView)
[titleLabel, optionBadge].forEach(titleRowStack.addArrangedSubview(_:))
[titleRowStack, subtitleLabel].forEach(titleStack.addArrangedSubview(_:))
}
override func configure() {
titleLabel.numberOfLines = .zero
guideButton.leftIcon = BKImage.Icon.bookOpen
guideButton.title = "감상평 가이드"
}
override func setupLayout() {
titleStack.snp.makeConstraints {
$0.top.equalToSuperview()
$0.leading.trailing.equalToSuperview()
.inset(LayoutConstants.horizontalInset)
}
appreciationTextView.snp.makeConstraints {
$0.top.equalTo(titleStack.snp.bottom)
.offset(LayoutConstants.sentenceViewOffset)
$0.leading.trailing.equalToSuperview()
.inset(LayoutConstants.horizontalInset)
}
guideButton.snp.makeConstraints {
$0.top.equalTo(appreciationTextView.snp.bottom)
.offset(LayoutConstants.buttonOffset)
$0.trailing.equalToSuperview()
.inset(LayoutConstants.horizontalInset)
$0.bottom.equalToSuperview()
}
tooltipView.snp.makeConstraints {
$0.trailing.equalTo(guideButton.snp.leading)
.offset(-8)
$0.centerY.equalTo(guideButton)
$0.height.equalTo(34)
}
}
func setText(_ content: String) {
appreciationTextView.appendText(content)
}
func startEditingIfNeeded() {
appreciationTextView.startEditing()
}
private func setupTextViewFocusHandling() {
// BKTextView의 메서드를 통한 포커스 감지
appreciationTextView.addTextViewFocusObserver(
target: self,
selector: #selector(textViewDidBeginEditing)
)
}
@objc private func textViewDidBeginEditing(_ notification: Notification) {
onAppreciationTextViewFocused?()
}
}
extension SentenceAppreciationView: RegistrationFormProvidable, FormInputNotifiable {
var inputChangedPublisher: AnyPublisher<Void, Never> {
appreciationTextView.textDidChangePublisher
}
func registrationForm() -> RegistrationForm? {
let trimmedSentence = appreciationTextView.text.trimmingCharacters(in: .whitespacesAndNewlines)
return .appreciation(SentenceAppreciationForm(
appreciation: trimmedSentence
))
}
}
private extension SentenceAppreciationView {
enum LayoutConstants {
static let horizontalInset = BKInset.inset5
static let buttonBorderWidth = BKBorder.border1
static let sentenceViewOffset: CGFloat = 40
static let buttonOffset: CGFloat = 12
static let titleStackSpacing = BKSpacing.spacing1
static let titleRowStackSpacing: CGFloat = 10
}
}