-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWithdrawViewController.swift
More file actions
395 lines (327 loc) · 16.6 KB
/
WithdrawViewController.swift
File metadata and controls
395 lines (327 loc) · 16.6 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
//
// WithdrawViewController.swift
// Presentation
//
// Created by 최정인 on 9/4/25.
//
import Combine
import Shared
import SnapKit
import UIKit
final class WithdrawViewController: BaseViewController<WithdrawViewModel> {
private enum Layout {
static let horizontalMargin: CGFloat = 20
static let mainLabelTopSpacing: CGFloat = 86
static let mainLabelTopMinSpacing: CGFloat = 60
static let subLabelTopSpacing: CGFloat = 5
static let confirmStackViewTopSpacing: CGFloat = 24
static let confirmStackViewTopMinSpacing: CGFloat = 15
static let confirmButtonSize: CGFloat = 24
static let withdrawReasonViewTopSpacing: CGFloat = 62
static let withdrawReasonViewTopMinSpacing: CGFloat = 22
static let withdrawChoiceButtonHeight: CGFloat = 56
static let withdrawReasonTextViewPlaceholderTopSpacing: CGFloat = 13
static let withdrawReasonTextViewHorizontalMargin: CGFloat = 16
static let withdrawReasonTextViewVerticalMargin: CGFloat = 13
static let withdrawReasonTextBackgroundViewHeight: CGFloat = 112
static let withdrawReasonStackViewTopSpacing: CGFloat = 16
static let withdrawButtonBottomSpacing: CGFloat = 14
static let withdrawButtonHeight: CGFloat = 54
}
private let mainLabel = UILabel()
private let subLabel = UILabel()
private let confirmStackView = UIStackView()
private let confirmButton = UIButton()
private let confirmLabel = UILabel()
private let withdrawReasonView = UIView()
private let withdrawReasonLabel = UILabel()
private let withdrawReasonStackView = UIStackView()
private var withdrawReasonButtons: [WithdrawReason: BitnagilChoiceButton] = [:]
private let withdrawReasonTextBackgroundView = UIView()
private let withdrawReasonTextViewPlaceholder = UILabel()
private let withdrawReasonTextView = UITextView()
private let withdrawReasonMaxLengthLabel = UILabel()
private let withdrawReasonMaxLength: Int = 100
private let withdrawButton = PrimaryButton(buttonState: .disabled, buttonTitle: "탈퇴하기")
private var isLayoutConfigured: Bool = false
private var mainLabelTopConstraint: Constraint?
private var confirmStackViewTopConstraint: Constraint?
private var withdrawReasonViewTopConstraint: Constraint?
private var cancellables: Set<AnyCancellable> = []
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
if !isLayoutConfigured {
updateConstraint()
isLayoutConfigured = true
}
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
removeKeyboardNotification()
}
private func updateConstraint() {
let height = view.bounds.height
if height <= 667 {
mainLabelTopConstraint?.update(offset: Layout.mainLabelTopMinSpacing)
confirmStackViewTopConstraint?.update(offset: Layout.confirmStackViewTopMinSpacing)
withdrawReasonViewTopConstraint?.update(offset: Layout.withdrawReasonViewTopMinSpacing)
}
}
override func configureAttribute() {
view.backgroundColor = .systemBackground
navigationController?.setNavigationBarHidden(true, animated: false)
configureCustomNavigationBar(navigationBarStyle: .withBackButton(title: "탈퇴하기"))
mainLabel.text = "정말 탈퇴하시겠어요?"
mainLabel.font = BitnagilFont(style: .title3, weight: .semiBold).font
mainLabel.textColor = BitnagilColor.gray10
let subLabelText = "탈퇴하면 보관 중인 데이터와 서비스 이용 내역이\n모두 삭제되고, 다시 가입해도 복구되지 않아요."
subLabel.attributedText = BitnagilFont(style: .body1, weight: .medium).attributedString(text: subLabelText)
subLabel.textColor = BitnagilColor.gray50
subLabel.numberOfLines = 2
confirmStackView.axis = .horizontal
confirmStackView.spacing = 10
confirmButton.setImage(BitnagilIcon.uncheckedCircleSmallIcon, for: .normal)
confirmButton.addAction(
UIAction { [weak self] _ in
self?.viewModel.action(input: .toggleConfirmButton)
},
for: .touchUpInside)
confirmLabel.text = "유의사항을 확인했어요."
confirmLabel.font = BitnagilFont(style: .body2, weight: .medium).font
confirmLabel.textColor = BitnagilColor.gray40
withdrawReasonView.isHidden = true
withdrawReasonLabel.text = "탈퇴 사유를 알려주실 수 있나요?"
withdrawReasonLabel.font = BitnagilFont(style: .title3, weight: .semiBold).font
withdrawReasonLabel.textColor = BitnagilColor.gray10
withdrawReasonStackView.axis = .vertical
withdrawReasonStackView.spacing = 10
WithdrawReason.allCases.forEach { withdrawReason in
let withdrawChoiceButton = BitnagilChoiceButton(bitnagilChoice: withdrawReason, forWithdrawChoice: true)
withdrawChoiceButton.addAction(
UIAction { [weak self] _ in
self?.viewModel.action(input: .choiceWithdrawReason(reason: withdrawReason))
},
for: .touchUpInside)
withdrawChoiceButton.snp.makeConstraints { make in
make.height.equalTo(Layout.withdrawChoiceButtonHeight)
}
withdrawReasonStackView.addArrangedSubview(withdrawChoiceButton)
withdrawReasonButtons[withdrawReason] = withdrawChoiceButton
}
withdrawReasonTextBackgroundView.backgroundColor = BitnagilColor.gray99
withdrawReasonTextBackgroundView.layer.masksToBounds = true
withdrawReasonTextBackgroundView.layer.cornerRadius = 12
withdrawReasonTextViewPlaceholder.attributedText = BitnagilFont(style: .subtitle1, weight: .medium)
.attributedString(text: "기타사항(직접 입력)")
withdrawReasonTextViewPlaceholder.textColor = BitnagilColor.gray90
withdrawReasonTextView.delegate = self
withdrawReasonTextView.font = BitnagilFont(style: .subtitle1, weight: .medium).font
withdrawReasonTextView.textColor = BitnagilColor.gray10
withdrawReasonTextView.backgroundColor = .clear
withdrawReasonMaxLengthLabel.text = "* 최대 \(withdrawReasonMaxLength)자까지만 입력할 수 있어요."
withdrawReasonMaxLengthLabel.font = BitnagilFont(style: .body2, weight: .medium).font
withdrawReasonMaxLengthLabel.textColor = BitnagilColor.error
withdrawReasonMaxLengthLabel.isHidden = true
withdrawButton.addAction(
UIAction { [weak self] _ in
self?.viewModel.action(input: .withdrawService)
},
for: .touchUpInside)
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(dismissKeyboard))
tapGesture.cancelsTouchesInView = false
view.addGestureRecognizer(tapGesture)
configureKeyboardNotification()
}
override func configureLayout() {
let safeArea = view.safeAreaLayoutGuide
view.addSubview(mainLabel)
view.addSubview(subLabel)
[confirmButton, confirmLabel].forEach {
confirmStackView.addArrangedSubview($0)
}
view.addSubview(confirmStackView)
[withdrawReasonLabel, withdrawReasonStackView].forEach {
withdrawReasonView.addSubview($0)
}
view.addSubview(withdrawReasonView)
view.addSubview(withdrawButton)
mainLabel.snp.makeConstraints { make in
mainLabelTopConstraint = make.top.equalTo(safeArea).offset(Layout.mainLabelTopSpacing).constraint
make.horizontalEdges.equalTo(safeArea).inset(Layout.horizontalMargin)
}
subLabel.snp.makeConstraints { make in
make.top.equalTo(mainLabel.snp.bottom).offset(Layout.subLabelTopSpacing)
make.horizontalEdges.equalTo(safeArea).inset(Layout.horizontalMargin)
}
confirmStackView.snp.makeConstraints { make in
confirmStackViewTopConstraint = make.top.equalTo(subLabel.snp.bottom).offset(Layout.confirmStackViewTopSpacing).constraint
make.horizontalEdges.equalTo(safeArea).inset(Layout.horizontalMargin)
}
confirmButton.snp.makeConstraints { make in
make.size.equalTo(Layout.confirmButtonSize)
}
withdrawReasonView.snp.makeConstraints { make in
withdrawReasonViewTopConstraint = make.top.equalTo(confirmStackView.snp.bottom).offset(Layout.withdrawReasonViewTopSpacing).constraint
make.horizontalEdges.equalTo(safeArea)
make.bottom.equalTo(withdrawButton.snp.top)
}
withdrawReasonLabel.snp.makeConstraints { make in
make.top.equalToSuperview()
make.horizontalEdges.equalTo(safeArea).inset(Layout.horizontalMargin)
}
withdrawReasonTextBackgroundView.addSubview(withdrawReasonTextViewPlaceholder)
withdrawReasonTextBackgroundView.addSubview(withdrawReasonTextView)
withdrawReasonStackView.addArrangedSubview(withdrawReasonTextBackgroundView)
withdrawReasonStackView.addArrangedSubview(withdrawReasonMaxLengthLabel)
withdrawReasonTextViewPlaceholder.snp.makeConstraints { make in
make.top.equalToSuperview().offset(Layout.withdrawReasonTextViewPlaceholderTopSpacing)
make.horizontalEdges.equalToSuperview().inset(Layout.horizontalMargin)
}
withdrawReasonTextView.snp.makeConstraints { make in
make.horizontalEdges.equalToSuperview().inset(Layout.withdrawReasonTextViewHorizontalMargin)
make.verticalEdges.equalToSuperview().inset(Layout.withdrawReasonTextViewVerticalMargin)
}
withdrawReasonTextBackgroundView.snp.makeConstraints { make in
make.height.equalTo(Layout.withdrawReasonTextBackgroundViewHeight)
}
withdrawReasonStackView.snp.makeConstraints { make in
make.top.equalTo(withdrawReasonLabel.snp.bottom).offset(Layout.withdrawReasonStackViewTopSpacing)
make.horizontalEdges.equalTo(safeArea).inset(Layout.horizontalMargin)
}
withdrawButton.snp.makeConstraints { make in
make.horizontalEdges.equalTo(safeArea).inset(Layout.horizontalMargin)
make.bottom.equalTo(safeArea).inset(Layout.withdrawButtonBottomSpacing)
make.height.equalTo(Layout.withdrawButtonHeight)
}
}
override func bind() {
viewModel.output.confirmPublisher
.receive(on: DispatchQueue.main)
.sink { [weak self] comfirm in
self?.updateConfirmButtonState(confirm: comfirm)
}
.store(in: &cancellables)
viewModel.output.withdrawReasonPublisher
.receive(on: DispatchQueue.main)
.sink { [weak self] withdrawReason in
self?.updateWithdrawReason(selectedWithdrawReason: withdrawReason)
}
.store(in: &cancellables)
viewModel.output.withdrawButtonPublisher
.receive(on: DispatchQueue.main)
.sink { [weak self] canWithdraw in
self?.withdrawButton.updateButtonState(buttonState: canWithdraw ? .default : .disabled)
}
.store(in: &cancellables)
viewModel.output.withdrawResultPublisher
.receive(on: DispatchQueue.main)
.sink { withdrawResult in
if withdrawResult {
let confirmDialog = BitnagilConfirmDialog(
title: "탈퇴가 완료되었어요",
message: "이용해 주셔서 감사합니다. 언제든 다시\n돌아오실 수 있어요:)",
confirmHandler: {
guard
let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene,
let sceneDelegate = windowScene.delegate as? UIWindowSceneDelegate,
let window = sceneDelegate.window
else { return }
guard let loginViewModel = DIContainer.shared.resolve(type: LoginViewModel.self)
else { fatalError("loginViewModel 의존성이 등록되지 않았습니다.") }
let loginView = LoginViewController(viewModel: loginViewModel)
let navigationController = UINavigationController(rootViewController: loginView)
window?.rootViewController = navigationController
window?.makeKeyAndVisible()
})
confirmDialog.modalPresentationStyle = .overFullScreen
self.present(confirmDialog, animated: false)
}
}
.store(in: &cancellables)
}
private func updateConfirmButtonState(confirm: Bool) {
let corfirmButtonImage = confirm ? BitnagilIcon.checkedCircleSmallIcon : BitnagilIcon.uncheckedCircleSmallIcon
confirmButton.setImage(corfirmButtonImage, for: .normal)
withdrawReasonView.isHidden = !confirm
}
private func updateWithdrawReason(selectedWithdrawReason: WithdrawReason?) {
withdrawReasonButtons.forEach { withdrawReason in
let isSelected = withdrawReason.key == selectedWithdrawReason
withdrawReason.value.updateButtonState(isChecked: isSelected)
}
if selectedWithdrawReason != nil {
withdrawReasonTextView.resignFirstResponder()
withdrawReasonTextView.text = ""
withdrawReasonTextViewPlaceholder.isHidden = false
withdrawReasonMaxLengthLabel.isHidden = true
}
}
private func configureKeyboardNotification() {
NotificationCenter.default.addObserver(
self,
selector: #selector(keyboardWillAppear),
name: UIResponder.keyboardWillShowNotification,
object: nil)
NotificationCenter.default.addObserver(
self,
selector: #selector(keyboardWillDisappear),
name: UIResponder.keyboardWillHideNotification,
object: nil)
}
private func removeKeyboardNotification() {
NotificationCenter.default.removeObserver(
self,
name: UIResponder.keyboardWillShowNotification,
object: nil)
NotificationCenter.default.removeObserver(
self,
name: UIResponder.keyboardWillHideNotification,
object: nil)
}
@objc private func dismissKeyboard() {
view.endEditing(true)
}
@objc private func keyboardWillAppear(_ sender: Notification) {
guard
let keyboardFrame = sender.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? CGRect,
let duration = sender.userInfo?[UIResponder.keyboardAnimationDurationUserInfoKey] as? Double
else { return }
let keyboardHeight = keyboardFrame.height
let buttonFrame = withdrawButton.convert(withdrawButton.bounds, to: view)
let buttonBottom = buttonFrame.maxY
let visibleHeight = view.frame.height - keyboardHeight
if buttonBottom > visibleHeight {
let offset = buttonBottom - visibleHeight + 50
UIView.animate(withDuration: duration) {
self.view.frame.origin.y = -offset
}
}
}
@objc private func keyboardWillDisappear(_ sender: Notification) {
guard let duration = sender.userInfo?[UIResponder.keyboardAnimationDurationUserInfoKey] as? Double
else { return }
UIView.animate(withDuration: duration) {
self.view.frame.origin.y = 0
}
}
}
extension WithdrawViewController: UITextViewDelegate {
func textViewDidBeginEditing(_ textView: UITextView) {
viewModel.action(input: .choiceWithdrawReason(reason: nil))
if !textView.text.isEmpty {
viewModel.action(input: .inputWithdrawReason(reason: textView.text))
}
}
func textViewDidChange(_ textView: UITextView) {
withdrawReasonTextViewPlaceholder.isHidden = !textView.text.isEmpty
let reason = textView.text ?? ""
viewModel.action(input: .inputWithdrawReason(reason: reason))
let isMaxLength = textView.text.count >= withdrawReasonMaxLength
withdrawReasonMaxLengthLabel.isHidden = !isMaxLength
}
func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
let currentText = textView.text ?? ""
let newLength = currentText.count + text.count - range.length
return newLength <= withdrawReasonMaxLength
}
}