-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTodayView.swift
More file actions
360 lines (334 loc) · 12.1 KB
/
TodayView.swift
File metadata and controls
360 lines (334 loc) · 12.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
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
//
// TodayView.swift
// DevLog
//
// Created by opfic on 3/6/26.
//
import SwiftUI
struct TodayView: View {
@Environment(\.diContainer) private var container: any DIContainer
@State private var router = NavigationRouter()
@State var viewModel: TodayViewModel
var body: some View {
NavigationStack(path: $router.path) {
List {
summarySection
if viewModel.sections.isEmpty, !viewModel.state.isLoading {
emptySection
} else {
ForEach(viewModel.sections) { section in
todoSection(section.title, items: section.items)
}
}
}
.listStyle(.insetGrouped)
.navigationTitle("오늘")
.toolbar { toolbarContent }
.navigationDestination(for: Path.self) { path in
switch path {
case .detail(let todoId):
TodoDetailView(viewModel: TodoDetailViewModel(
fetchTodoUseCase: container.resolve(FetchTodoByIdUseCase.self),
fetchReferenceItemsUseCase: container.resolve(FetchReferenceItemsUseCase.self),
upsertUseCase: container.resolve(UpsertTodoUseCase.self),
todoId: todoId
))
}
}
.background(NavigationBarConfigurator())
.refreshable { viewModel.send(.refresh) }
.onAppear { viewModel.send(.onAppear) }
.alert(
viewModel.state.alertTitle,
isPresented: Binding(
get: { viewModel.state.showAlert },
set: { viewModel.send(.setAlert($0)) }
)
) {
Button("확인", role: .cancel) { }
} message: {
Text(viewModel.state.alertMessage)
}
.overlay {
if viewModel.state.isLoading {
LoadingView()
}
}
}
}
private var summarySection: some View {
Section {
ScrollView(.horizontal) {
HStack(spacing: 12) {
ForEach(TodayViewModel.SummaryScope.allCases, id: \.self) { scope in
Button {
withAnimation(.easeInOut) {
viewModel.send(.setSummaryScope(scope))
}
} label: {
SummaryCard(
title: scope.title,
value: viewModel.summaryValue(for: scope),
accentColor: scope.accentColor,
isSelected: viewModel.state.selectedSummaryScope == scope
)
}
.buttonStyle(.plain)
}
}
}
.scrollIndicators(.never)
.contentMargins(.horizontal, 16)
}
.listRowInsets(EdgeInsets(top: 16, leading: 0, bottom: 16, trailing: 0))
}
@ToolbarContentBuilder
private var toolbarContent: some ToolbarContent {
ToolbarItem(placement: .topBarTrailing) {
Menu {
Picker(
"보기 범위",
selection: Binding(
get: { viewModel.state.displayOptions.dueDateVisibility },
set: { viewModel.send(.setDueDateVisibility($0)) }
)
) {
ForEach(TodayDisplayOptions.DueDateVisibility.allCases, id: \.self) { option in
Text(option.title).tag(option)
}
}
Toggle(
"중요 표시만",
isOn: Binding(
get: { viewModel.state.displayOptions.focusVisibility == .focusedOnly },
set: {
viewModel.send(.setFocusVisibility($0 ? .focusedOnly : .all))
}
)
)
.tint(.orange)
if viewModel.state.displayOptions.focusVisibility == .focusedOnly {
Text("중요 표시한 Todo만 표시됩니다.")
.font(.caption)
}
} label: {
let options = viewModel.state.displayOptions
Image(systemName: "line.3.horizontal.decrease.circle\(options == .default ? "" : ".fill")")
}
}
}
private var emptySection: some View {
Section {
VStack(spacing: 8) {
Text(emptyStateContent.title)
.foregroundStyle(.primary)
Text(emptyStateContent.message)
.font(.caption)
.foregroundStyle(.secondary)
.multilineTextAlignment(.center)
}
.frame(maxWidth: .infinity)
.padding(.vertical, 28)
}
}
@ViewBuilder
private func todoSection(_ title: String, items: [TodayTodoItem]) -> some View {
if !items.isEmpty {
Section {
ForEach(items) { item in
NavigationLink(value: Path.detail(item.id)) {
TodayTodoRow(item: item)
.listRowInsets(EdgeInsets(top: 0, leading: 16, bottom: 0, trailing: 16))
}
.swipeActions(edge: .leading, allowsFullSwipe: false) {
Button {
viewModel.send(.togglePinned(item))
} label: {
Image(systemName: item.isPinned ? "star.slash" : "star.fill")
}
.tint(.orange)
}
.swipeActions(edge: .trailing, allowsFullSwipe: false) {
Button {
viewModel.send(.completeTodo(item))
} label: {
Label("완료", systemImage: "checkmark")
}
.tint(.green)
}
}
} header: {
Text(title)
.listRowInsets(EdgeInsets())
}
}
}
private var emptyStateContent: EmptyStateContent {
switch viewModel.state.selectedSummaryScope {
case .all:
if viewModel.state.todos.isEmpty {
return EmptyStateContent(
title: "남아 있는 Todo가 없습니다.",
message: "완료되지 않은 일이 생기면 이곳에서 우선순위대로 볼 수 있습니다."
)
}
return EmptyStateContent(
title: "선택한 보기 옵션에 맞는 Todo가 없습니다.",
message: "툴바에서 보기 범위를 조정하거나 전체 보기로 돌아가세요."
)
case .focused:
return EmptyStateContent(
title: "집중할 일이 없습니다.",
message: "중요 표시한 Todo가 생기면 이곳에서 바로 볼 수 있습니다."
)
case .overdue:
return EmptyStateContent(
title: "지난 마감 Todo가 없습니다.",
message: "지금은 기한이 지난 Todo가 없습니다."
)
case .dueSoon:
return EmptyStateContent(
title: "7일 내 일정이 없습니다.",
message: "곧 마감되는 Todo가 생기면 이곳에서 먼저 볼 수 있습니다."
)
}
}
private struct EmptyStateContent {
let title: String
let message: String
}
private enum Path: Hashable {
case detail(String)
}
}
private extension TodayDisplayOptions.DueDateVisibility {
var title: String {
switch self {
case .all:
return "전체"
case .withDueDateOnly:
return "기한 있는 Todo만"
case .withoutDueDateOnly:
return "기한 없는 Todo만"
}
}
}
private extension TodayViewModel.SummaryScope {
var title: String {
switch self {
case .all:
return "남은 일"
case .focused:
return "집중"
case .overdue:
return "지연"
case .dueSoon:
return "7일 내"
}
}
var accentColor: Color {
switch self {
case .all:
return .blue
case .focused:
return .orange
case .overdue:
return .red
case .dueSoon:
return .green
}
}
}
private struct SummaryCard: View {
let title: String
let value: Int
let accentColor: Color
let isSelected: Bool
var body: some View {
VStack(alignment: .leading, spacing: 10) {
Text(title)
.font(.caption)
.foregroundStyle(isSelected ? accentColor : .secondary)
Text("\(value)")
.font(.title2.bold())
.foregroundStyle(Color(.label))
}
.frame(width: 96, alignment: .leading)
.padding(14)
.background(
RoundedRectangle(cornerRadius: 16)
.fill(isSelected ? accentColor.opacity(0.2) : accentColor.opacity(0.12))
.strokeBorder(
isSelected ? accentColor.opacity(0.55) : accentColor.opacity(0.18),
lineWidth: isSelected ? 1.5 : 1
)
)
.scaleEffect(isSelected ? 1 : 0.98)
}
}
private struct TodayTodoRow: View {
private let calendar = Calendar.current
let item: TodayTodoItem
var body: some View {
VStack(alignment: .leading, spacing: 8) {
HStack(spacing: 8) {
Image(systemName: item.category.symbolName)
.foregroundStyle(item.category.color)
.frame(width: 18)
Text(item.title)
.font(.headline)
.foregroundStyle(Color(.label))
.lineLimit(1)
if let number = item.number {
Text("#\(number)")
.font(.subheadline.weight(.semibold))
.foregroundStyle(.gray)
.fixedSize(horizontal: true, vertical: false)
}
Spacer()
}
HStack(spacing: 8) {
Text(item.category.localizedName)
.font(.caption.weight(.semibold))
.foregroundStyle(item.category.color)
if let dueDate {
Text(dueDate.text)
.font(.caption2.weight(.semibold))
.foregroundStyle(dueDate.textColor)
.padding(.horizontal, 8)
.padding(.vertical, 4)
.background(
Capsule()
.fill(dueDate.backgroundColor)
)
}
}
if !item.tags.isEmpty {
TagList(item.tags, lineLimit: 1)
}
}
}
private var dueDate: DueDateBadge? {
guard let date = item.dueDate else { return nil }
let today = calendar.startOfDay(for: Date())
let dueDay = calendar.startOfDay(for: date)
if dueDay < today {
return DueDateBadge(
text: "기한 지남",
textColor: .red,
backgroundColor: Color.red.opacity(0.12)
)
}
let formatted = date.formatted(date: .abbreviated, time: .omitted)
return DueDateBadge(
text: formatted,
textColor: .blue,
backgroundColor: Color.blue.opacity(0.12)
)
}
private struct DueDateBadge {
let text: String
let textColor: Color
let backgroundColor: Color
}
}