-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRoutineListViewController.swift
More file actions
296 lines (250 loc) · 11.7 KB
/
RoutineListViewController.swift
File metadata and controls
296 lines (250 loc) · 11.7 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
//
// RoutineListViewController.swift
// Presentation
//
// Created by 최정인 on 8/18/25.
//
import Combine
import Shared
import SnapKit
import UIKit
final class RoutineListViewController: BaseViewController<RoutineListViewModel> {
private enum Layout {
static let horizontalMargin: CGFloat = 20
static let weekViewTopSpacing: CGFloat = 58
static let weekViewHeight: CGFloat = 92
static let emptyViewCenterYSpacing: CGFloat = 40
static let emptyViewHeight: CGFloat = 102
static let routineScrollViewTopSpacing: CGFloat = 16
static let routineStackViewSpacing: CGFloat = 12
static let routineStackViewBottomSpacing: CGFloat = 60
static let toastMessageViewHeight: CGFloat = 52
static let toastMessageViewBottomSpacing: CGFloat = 20
}
private let weekView: WeekView
private let emptyView = HomeEmptyView()
private let routineScrollView = UIScrollView()
private let routineStackView = UIStackView()
private var routineCardViews: [String: RoutineCardView] = [:]
private let deleteToastMessage: String = "삭제가 완료되었습니다."
private let updateToastMessage: String = "루틴 수정이 완료되었습니다."
private var toastMessageView = ToastView(message: "")
private var dimmedView: UIView?
private var cancellables: Set<AnyCancellable>
init(viewModel: RoutineListViewModel, selectedDate: Date) {
self.weekView = WeekView(date: selectedDate)
cancellables = []
viewModel.action(input: .selectDate(date: selectedDate))
super.init(viewModel: viewModel)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
super.viewDidLoad()
viewModel.action(input: .fetchRoutineList)
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
view.subviews.first(where: { $0.tag == 999 })?.removeFromSuperview()
}
override func configureAttribute() {
weekView.delegate = self
emptyView.isHidden = true
emptyView.didTapRegisterRoutineButton = {
guard let routineCreationViewModel = DIContainer.shared.resolve(type: RoutineCreationViewModel.self)
else { fatalError("routineCreationViewModel 의존성이 등록되지 않았습니다.") }
let routineCreationView = RoutineCreationViewController(viewModel: routineCreationViewModel)
routineCreationView.hidesBottomBarWhenPushed = true
self.navigationController?.pushViewController(routineCreationView, animated: true)
}
routineScrollView.showsVerticalScrollIndicator = false
routineStackView.axis = .vertical
routineStackView.spacing = Layout.routineStackViewSpacing
}
override func configureLayout() {
let safeArea = view.safeAreaLayoutGuide
view.backgroundColor = BitnagilColor.gray99
configureCustomNavigationBar(navigationBarStyle: .withBackButton(title: "루틴 리스트"), backgroundColor: BitnagilColor.gray99)
view.addSubview(weekView)
view.addSubview(emptyView)
view.addSubview(routineScrollView)
routineScrollView.addSubview(routineStackView)
view.addSubview(toastMessageView)
weekView.snp.makeConstraints { make in
make.top.equalTo(safeArea).offset(Layout.weekViewTopSpacing)
make.horizontalEdges.equalToSuperview()
make.height.equalTo(Layout.weekViewHeight)
}
emptyView.snp.makeConstraints { make in
make.centerX.equalTo(safeArea)
make.centerY.equalTo(safeArea).offset(Layout.emptyViewCenterYSpacing)
make.height.equalTo(Layout.emptyViewHeight)
}
routineScrollView.snp.makeConstraints { make in
make.top.equalTo(weekView.snp.bottom).offset(Layout.routineScrollViewTopSpacing)
make.bottom.equalTo(safeArea)
make.leading.equalTo(safeArea).offset(Layout.horizontalMargin)
make.trailing.equalTo(safeArea).inset(Layout.horizontalMargin)
}
routineStackView.snp.makeConstraints { make in
make.top.leading.trailing.equalToSuperview()
make.bottom.equalToSuperview().inset(Layout.routineStackViewBottomSpacing)
make.width.equalTo(routineScrollView.snp.width)
}
toastMessageView.snp.makeConstraints { make in
make.leading.equalTo(safeArea).offset(Layout.horizontalMargin)
make.trailing.equalTo(safeArea).inset(Layout.horizontalMargin)
make.height.equalTo(Layout.toastMessageViewHeight)
make.bottom.equalTo(safeArea).inset(Layout.toastMessageViewBottomSpacing)
}
}
override func bind() {
viewModel.output.fetchRoutinesResultPublisher
.receive(on: DispatchQueue.main)
.sink { [weak self] isFetchRoutines in
if isFetchRoutines {
self?.viewModel.action(input: .fetchDailyRoutine)
}
}
.store(in: &cancellables)
viewModel.output.selectedDatePublisher
.receive(on: DispatchQueue.main)
.sink { [weak self] selectedDate in
self?.weekView.updateWeekDateViews(date: selectedDate)
}
.store(in: &cancellables)
viewModel.output.routinesPublisher
.receive(on: DispatchQueue.main)
.sink { [weak self] routines in
self?.updateRoutineStackView(routines: routines)
}
.store(in: &cancellables)
NotificationCenter.default.publisher(for: .showDeletedRoutineToast)
.receive(on: DispatchQueue.main)
.sink { [weak self] _ in
guard let self else { return }
self.toastMessageView.showToastMessageView(message: deleteToastMessage)
}
.store(in: &cancellables)
NotificationCenter.default.publisher(for: .showUpdatedRoutineToast)
.receive(on: DispatchQueue.main)
.sink { [weak self] _ in
guard let self else { return }
self.viewModel.action(input: .fetchRoutineList)
self.viewModel.action(input: .fetchDailyRoutine)
self.toastMessageView.showToastMessageView(message: updateToastMessage)
}
.store(in: &cancellables)
}
private func updateRoutineStackView(routines: [Routine]) {
routineStackView.arrangedSubviews.forEach { view in
routineStackView.removeArrangedSubview(view)
view.removeFromSuperview()
}
routineCardViews.removeAll()
emptyView.isHidden = !routines.isEmpty
routineScrollView.isHidden = routines.isEmpty
for routine in routines {
let routineCardView = RoutineCardView(routine: routine)
routineCardView.delegate = self
routineCardViews[routine.id] = routineCardView
routineStackView.addArrangedSubview(routineCardView)
}
}
private func goToRoutineCreationView(routineId: String, isApplyToday: Bool = true) {
guard let routineCreationViewModel = DIContainer.shared.resolve(type: RoutineCreationViewModel.self)
else { fatalError("routineCreationViewModel 의존성이 등록되지 않았습니다.") }
let routineCreationView = RoutineCreationViewController(
viewModel: routineCreationViewModel,
updateInfo: (routineId, isApplyToday ? .today : .tomorrow))
routineCreationView.hidesBottomBarWhenPushed = true
self.navigationController?.pushViewController(routineCreationView, animated: true)
}
}
// MARK: WeekViewDelegate
extension RoutineListViewController: WeekViewDelegate {
func weekView(_ sender: WeekView, didSelectDate date: Date) {
viewModel.action(input: .selectDate(date: date))
}
}
// MARK: RoutineCardViewDelegate
extension RoutineListViewController: RoutineCardViewDelegate {
func routineCardView(_ sender: RoutineCardView, didTapPlusButton routine: RecommendedRoutine) { }
func routineCardView(_ sender: RoutineCardView, didTapEditButton routine: Routine) {
viewModel.action(input: .selectRoutine(routine: routine))
guard !routine.repeatDay.isEmpty else {
goToRoutineCreationView(routineId: routine.id)
return
}
dimmedView?.removeFromSuperview()
let newDimmedView = UIView()
newDimmedView.backgroundColor = .black.withAlphaComponent(0.7)
newDimmedView.frame = view.bounds
view.addSubview(newDimmedView)
dimmedView = newDimmedView
let routineEditAlertViewController = RoutineEditAlertViewController()
if let sheet = routineEditAlertViewController.sheetPresentationController {
sheet.prefersGrabberVisible = false
if #available(iOS 16.0, *) {
sheet.detents = [.custom { _ in 250 }]
} else {
sheet.detents = [.medium()]
}
sheet.prefersScrollingExpandsWhenScrolledToEdge = false
sheet.preferredCornerRadius = 20
}
routineEditAlertViewController.onDismiss = { [weak self] in
self?.dimmedView?.removeFromSuperview()
self?.dimmedView = nil
}
routineEditAlertViewController.goToRoutineCreationView = { [weak self] isApplyToday in
self?.goToRoutineCreationView(routineId: routine.id, isApplyToday: isApplyToday)
}
present(routineEditAlertViewController, animated: true)
}
func routineCardView(_ sender: RoutineCardView, didTapDeleteButton routine: Routine) {
viewModel.action(input: .selectRoutine(routine: routine))
dimmedView?.removeFromSuperview()
let newDimmedView = UIView()
newDimmedView.backgroundColor = .black.withAlphaComponent(0.7)
newDimmedView.frame = view.bounds
view.addSubview(newDimmedView)
dimmedView = newDimmedView
if routine.repeatDay.isEmpty || routine.isDeleted {
let routineDeleteAlertViewController = RoutineDeleteAlertViewController(viewModel: viewModel, isDeleteAllRoutines: false)
if let sheet = routineDeleteAlertViewController.sheetPresentationController {
sheet.prefersGrabberVisible = false
if #available(iOS 16.0, *) {
sheet.detents = [.custom { _ in 204 }]
} else {
sheet.detents = [.medium()]
}
sheet.prefersScrollingExpandsWhenScrolledToEdge = false
sheet.preferredCornerRadius = 20
}
routineDeleteAlertViewController.onDismiss = { [weak self] in
self?.dimmedView?.removeFromSuperview()
self?.dimmedView = nil
}
present(routineDeleteAlertViewController, animated: true)
} else {
let routineDeleteViewController = RoutineDeleteViewController(viewModel: viewModel)
if let sheet = routineDeleteViewController.sheetPresentationController {
sheet.prefersGrabberVisible = false
if #available(iOS 16.0, *) {
sheet.detents = [.custom { _ in 270 }]
} else {
sheet.detents = [.medium()]
}
sheet.prefersScrollingExpandsWhenScrolledToEdge = false
sheet.preferredCornerRadius = 20
}
routineDeleteViewController.onDismiss = { [weak self] in
self?.dimmedView?.removeFromSuperview()
self?.dimmedView = nil
}
present(routineDeleteViewController, animated: true)
}
}
}