-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRoutineCreationViewController.swift
More file actions
405 lines (352 loc) · 16.3 KB
/
RoutineCreationViewController.swift
File metadata and controls
405 lines (352 loc) · 16.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
//
// RoutineCreationViewController.swift
// Presentation
//
// Created by 이동현 on 8/15/25.
//
import Combine
import Domain
import SnapKit
import UIKit
final class RoutineCreationViewController: BaseViewController<RoutineCreationViewModel> {
private enum DateSelectionType {
case start
case end
}
private enum Layout {
static let horizontalSpacing: CGFloat = 20
static let textFieldHeight: CGFloat = 24
static let textFieldTopSpacing: CGFloat = 90
static let textFieldUnderLineTopSpacing: CGFloat = 12
static let textFieldUnderLineHeight: CGFloat = 2
static let clearButtonSize: CGFloat = 16
static let subroutineViewTopSpacing: CGFloat = 40
static let routineCreationViewTopSpacing: CGFloat = 12
static let registerButtonTopSpacing: CGFloat = 59
static let registerButtonHeight: CGFloat = 54
static let registerButtonBottomSpacing: CGFloat = 14
static let timePickerBottomSheetHeight: CGFloat = 357
static let calendarBottomSheetHeight: CGFloat = 517
}
private let scrollView = UIScrollView()
private let contentView = UIView()
private let nameTextField = UITextField()
private let textViewUnderLineView = UIView()
private let clearButton = UIButton()
private var dateSelectionType: DateSelectionType?
private let subRoutineNameView = RoutineCreationCardView<RoutineNameContentView>(
title: "세부루틴",
placeHolder: "ex) 일어나자마자 이불 개기",
titleImage: BitnagilIcon.routineListIcon,
withInfoImage: true,
withAsteriskImage: false)
private let repeatView = RoutineCreationCardView<RoutineRepeatContentView>(
title: "반복 요일",
placeHolder: "ex) 매주 월,화,수,목,금",
titleImage: BitnagilIcon.routineRepeatIcon,
withInfoImage: false,
withAsteriskImage: false)
private let periodView = RoutineCreationCardView<RoutinePeriodContentView>(
title: "목표 기간",
placeHolder: "ex) 25.08.06 - 25.08.06",
titleImage: BitnagilIcon.routineDateIcon,
withInfoImage: false,
withAsteriskImage: true)
private let timeView = RoutineCreationCardView<RoutineTimeContentView>(
title: "시간",
placeHolder: "ex) 오전 9:40부터 시작",
titleImage: BitnagilIcon.routineTimeIcon,
withInfoImage: false,
withAsteriskImage: true)
private let registerButton = UIButton()
private let navigationTitle: String
private let registerButtonTitle: String
private let isFromMypage: Bool
private var cancellables = Set<AnyCancellable>()
init(
viewModel: RoutineCreationViewModel,
updateInfo: (routineId: String, updateType: RoutineUpdateApplyDateType)? = nil
) {
navigationTitle = updateInfo?.routineId == nil ? "루틴 등록" : "루틴 수정"
registerButtonTitle = updateInfo?.routineId == nil ? "등록하기" : "수정하기"
self.isFromMypage = false
super.init(viewModel: viewModel)
registerButton.setTitle(registerButtonTitle, for: .normal)
if let updateInfo {
viewModel.action(input: .fetchRoutine(id: updateInfo.routineId))
viewModel.action(input: .configureUpdateType(updateType: updateInfo.updateType))
}
}
init(
viewModel: RoutineCreationViewModel,
recommendRoutineId: Int,
isFromMypage: Bool = false
) {
navigationTitle = "루틴 등록"
registerButtonTitle = "등록하기"
self.isFromMypage = isFromMypage
super.init(viewModel: viewModel)
registerButton.setTitle(registerButtonTitle, for: .normal)
viewModel.action(input: .fetchRecommendedRoutine(id: recommendRoutineId))
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
configureCustomNavigationBar(navigationBarStyle: .withBackButton(title: navigationTitle))
}
override func configureAttribute() {
let attributes: [NSAttributedString.Key: Any] = [
.font: BitnagilFont(style: .title3, weight: .semiBold).font,
.foregroundColor: BitnagilColor.gray90 ?? .systemGray]
let tap = UITapGestureRecognizer(target: self, action: #selector(dismissKeyboard))
tap.cancelsTouchesInView = false
tap.delegate = self
view.backgroundColor = .white
view.addGestureRecognizer(tap)
scrollView.keyboardDismissMode = .interactive
nameTextField.attributedPlaceholder = NSAttributedString(string: "루틴 제목을 입력해주세요.", attributes: attributes)
nameTextField.font = BitnagilFont(style: .title3, weight: .semiBold).font
nameTextField.textColor = BitnagilColor.gray10
nameTextField.addTarget(self, action: #selector(textFieldEditingChanged(_:)), for: .editingChanged)
nameTextField.delegate = self
textViewUnderLineView.backgroundColor = BitnagilColor.gray90
clearButton.setImage(BitnagilIcon.clearIcon, for: .normal)
clearButton.addAction(
UIAction { [weak self] _ in
self?.nameTextField.text = ""
self?.viewModel.action(input: .configureName(name: ""))
},
for: .touchUpInside)
registerButton.layer.cornerRadius = 12
registerButton.layer.masksToBounds = true
registerButton.titleLabel?.font = BitnagilFont.init(style: .body1, weight: .semiBold).font
registerButton.addAction(
UIAction { [weak self] _ in
guard let self else { return }
self.viewModel.action(input: .registerRoutine)
if self.isFromMypage {
if
let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene,
let window = windowScene.windows.first(where: { $0.isKeyWindow }),
let tabBarView = window.rootViewController as? TabBarView {
self.navigationController?.popToRootViewController(animated: false)
tabBarView.selectedIndex = 1
viewModel.action(input: .showRecommendedRoutineToastMessageView)
}
} else {
viewModel.action(input: .showUpdateRoutineToastMessageView)
self.navigationController?.popViewController(animated: true)
}
},
for: .touchUpInside)
bindCreationCardViews()
}
override func configureLayout() {
let safeArea = view.safeAreaLayoutGuide
navigationController?.setNavigationBarHidden(true, animated: false)
view.addSubview(scrollView)
scrollView.addSubview(contentView)
contentView.addSubview(nameTextField)
contentView.addSubview(clearButton)
contentView.addSubview(textViewUnderLineView)
contentView.addSubview(subRoutineNameView)
contentView.addSubview(repeatView)
contentView.addSubview(periodView)
contentView.addSubview(timeView)
contentView.addSubview(registerButton)
scrollView.snp.makeConstraints { make in
make.edges.equalTo(safeArea)
}
contentView.snp.makeConstraints { make in
make.edges.equalTo(scrollView.contentLayoutGuide)
make.width.equalTo(scrollView.frameLayoutGuide)
make.height.equalTo(scrollView.frameLayoutGuide).priority(.low)
}
nameTextField.snp.makeConstraints { make in
make.top.equalToSuperview().offset(Layout.textFieldTopSpacing)
make.horizontalEdges.equalToSuperview().inset(Layout.horizontalSpacing)
make.height.equalTo(Layout.textFieldHeight)
}
clearButton.snp.makeConstraints { make in
make.trailing.equalToSuperview().offset(-Layout.horizontalSpacing)
make.centerY.equalTo(nameTextField)
make.size.equalTo(Layout.clearButtonSize)
}
textViewUnderLineView.snp.makeConstraints { make in
make.top.equalTo(nameTextField.snp.bottom).offset(Layout.textFieldUnderLineTopSpacing)
make.horizontalEdges.equalToSuperview().inset(Layout.horizontalSpacing)
make.height.equalTo(Layout.textFieldUnderLineHeight)
}
subRoutineNameView.snp.makeConstraints { make in
make.top.equalTo(textViewUnderLineView.snp.bottom).offset(Layout.subroutineViewTopSpacing)
make.horizontalEdges.equalToSuperview().inset(Layout.horizontalSpacing)
}
repeatView.snp.makeConstraints { make in
make.top.equalTo(subRoutineNameView.snp.bottom).offset(Layout.routineCreationViewTopSpacing)
make.horizontalEdges.equalToSuperview().inset(Layout.horizontalSpacing)
}
periodView.snp.makeConstraints { make in
make.top.equalTo(repeatView.snp.bottom).offset(Layout.routineCreationViewTopSpacing)
make.horizontalEdges.equalToSuperview().inset(Layout.horizontalSpacing)
}
timeView.snp.makeConstraints { make in
make.top.equalTo(periodView.snp.bottom).offset(Layout.routineCreationViewTopSpacing)
make.horizontalEdges.equalToSuperview().inset(Layout.horizontalSpacing)
}
registerButton.snp.makeConstraints { make in
make.top.greaterThanOrEqualTo(timeView.snp.bottom).offset(Layout.registerButtonTopSpacing)
make.horizontalEdges.equalToSuperview().inset(Layout.horizontalSpacing)
make.height.equalTo(Layout.registerButtonHeight)
make.bottom.equalToSuperview().inset(Layout.registerButtonBottomSpacing)
}
}
override func bind() {
viewModel.output.namePublisher
.receive(on: DispatchQueue.main)
.sink { [weak self] name in
self?.nameTextField.text = name
}
.store(in: &cancellables)
viewModel.output.subRoutinesPublisher
.receive(on: DispatchQueue.main)
.sink { [weak self] subroutines in
self?.subRoutineNameView.configure(dependency: .init(subRoutines: subroutines))
self?.subRoutineNameView.configure(subTitles: subroutines.filter { !$0.isEmpty })
}
.store(in: &cancellables)
viewModel.output.repeatTypePublisher
.receive(on: DispatchQueue.main)
.sink { [weak self] repeatType in
guard let repeatType else {
self?.repeatView.configure(dependency: .init(repeatType: .none))
self?.repeatView.configure(subTitles: [])
return
}
switch repeatType {
case .daily:
let subTitle = Week.allCases
.map { $0.koreanValue }
.joined(separator: ",")
self?.repeatView.configure(dependency: .init(repeatType: .daily))
self?.repeatView.configure(subTitles: ["매주 \(subTitle)"])
case .weekly(let weeks):
let subTitle = weeks
.sorted(by: { $0.id < $1.id })
.map { $0.koreanValue }
.joined(separator: ",")
self?.repeatView.configure(dependency: .init(repeatType: .weekly(weeks: weeks)))
self?.repeatView.configure(subTitles: weeks.isEmpty ? [] : ["매주 \(subTitle)"])
}
}
.store(in: &cancellables)
viewModel.output.periodPublisher
.receive(on: DispatchQueue.main)
.sink { [weak self] (start, end) in
self?.periodView.configure(dependency: .init(dates: (start, end)))
}
.store(in: &cancellables)
viewModel.output.executionTimePublisher
.receive(on: DispatchQueue.main)
.sink { [weak self] startAt in
self?.timeView.configure(dependency: .init(startTime: startAt))
if let startAt {
let timeString = startAt.isMidnight ? "하루 종일" : startAt.convertToString(dateType: .amPmTime)
self?.timeView.configure(subTitles: [timeString])
} else {
self?.timeView.configure(subTitles: [])
}
}
.store(in: &cancellables)
viewModel.output.isRoutineValid
.receive(on: DispatchQueue.main)
.sink { [weak self] isRoutineValid in
if isRoutineValid {
self?.registerButton.isEnabled = true
self?.registerButton.backgroundColor = BitnagilColor.gray10
self?.registerButton.setTitleColor(.white, for: .normal)
} else {
self?.registerButton.isEnabled = false
self?.registerButton.backgroundColor = BitnagilColor.gray95
self?.registerButton.setTitleColor(.white, for: .normal)
}
}
.store(in: &cancellables)
}
private func bindCreationCardViews() {
subRoutineNameView.onAction = { [weak self] action in
switch action {
case .subroutineChanged(let index, let text):
self?.viewModel.action(input: .configureSubRoutine(name: text, index: index))
case .deleteAllSubroutines:
self?.viewModel.action(input: .deleteAllSubRoutines)
}
}
repeatView.onAction = { [weak self] action in
switch action {
case .repeatDaily:
self?.viewModel.action(input: .configureRepeatType(type: .daily))
case .repeatWeekly:
self?.viewModel.action(input: .configureRepeatType(type: .weekly(weeks: [])))
case .setWeeks(let weeks):
self?.viewModel.action(input: .configureRepeatWeeks(weeks: weeks))
}
}
periodView.onAction = { [weak self] action in
switch action {
case .startDateSetTapped:
self?.dateSelectionType = .start
case .endDateSetTapped:
self?.dateSelectionType = .end
}
let datePickerView = BitnagilCalendarView()
datePickerView.delegate = self
self?.presentCustomBottomSheet(contentViewController: datePickerView, maxHeight: Layout.calendarBottomSheetHeight)
}
timeView.onAction = { [weak self] action in
switch action {
case .allDayTapped:
self?.viewModel.action(input: .toggleAllDay)
case .timeSetTapped:
let datePickerView = TimePickerView()
datePickerView.delegate = self
self?.presentCustomBottomSheet(contentViewController: datePickerView, maxHeight: Layout.timePickerBottomSheetHeight)
}
}
}
@objc private func dismissKeyboard() {
view.endEditing(true)
}
@objc private func textFieldEditingChanged(_ sender: UITextField) {
viewModel.action(input: .configureName(name: sender.text ?? ""))
}
}
extension RoutineCreationViewController: TimePickerViewDelegate {
func timePickerView(_ pickerView: TimePickerView, didSelectTime time: Date) {
viewModel.action(input: .configureExecution(type: .init(startAt: time)))
}
}
extension RoutineCreationViewController: BitnagilCalendarViewDelegate {
func bitnagilCalendarView(_ sender: BitnagilCalendarView, didSelectDate date: Date) {
guard let dateSelectionType else { return }
switch dateSelectionType {
case .start:
viewModel.action(input: .configureStartDate(date: date))
case .end:
viewModel.action(input: .configureEndDate(date: date))
}
self.dateSelectionType = nil
}
}
extension RoutineCreationViewController: UITextFieldDelegate {
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
}
extension RoutineCreationViewController: UIGestureRecognizerDelegate {
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
return !(touch.view is UIControl)
}
}