-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPushNotificationListView.swift
More file actions
343 lines (325 loc) · 12.8 KB
/
PushNotificationListView.swift
File metadata and controls
343 lines (325 loc) · 12.8 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
//
// PushNotificationListView.swift
// DevLog
//
// Created by opfic on 5/14/25.
//
import SwiftUI
struct PushNotificationListView: View {
@State private var router = NavigationRouter()
@State var viewModel: PushNotificationListViewModel
@Environment(\.sceneWidth) private var sceneWidth
@Environment(\.colorScheme) private var colorScheme
@Environment(\.diContainer) private var container: DIContainer
@State private var headerOffset: CGFloat = 0
@State private var isScrollTrackingEnabled = false
var body: some View {
NavigationStack(path: $router.path) {
notificationList
.listStyle(.plain)
.background(NavigationBarConfigurator(.secondarySystemBackground, alwaysVisible: true))
.onScrollOffsetChange { offset in
guard isScrollTrackingEnabled else { return }
headerOffset = max(0, -offset)
}
.safeAreaInset(edge: .top) { safeAreaHeader }
.background(Color(.secondarySystemBackground))
.onAppear {
viewModel.send(.fetchNotifications)
headerOffset = 0
isScrollTrackingEnabled = false
DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
isScrollTrackingEnabled = true
}
}
.refreshable { viewModel.send(.fetchNotifications) }
.navigationTitle(String(localized: "nav_push_notifications"))
.alert(
"",
isPresented: Binding(
get: { viewModel.state.showAlert },
set: { viewModel.send(.setAlert(isPresented: $0)) }
)) {
Button(String(localized: "common_close"), role: .cancel) { }
} message: {
Text(viewModel.state.alertMessage)
}
.toast(
isPresented: Binding(
get: { viewModel.state.showToast },
set: { viewModel.send(.setToast(isPresented: $0)) }),
duration: 5,
action: { viewModel.send(.undoDelete) }
) {
Label(viewModel.state.toastMessage, systemImage: "arrow.uturn.left")
.font(.caption)
.multilineTextAlignment(.center)
.lineLimit(3)
}
.sheet(item: Binding(
get: { viewModel.state.selectedTodoId },
set: { viewModel.send(.setSelectedTodoId($0)) }
)) { item in
NavigationStack {
TodoDetailView(viewModel: TodoDetailViewModel(
fetchTodoUseCase: container.resolve(FetchTodoByIdUseCase.self),
fetchReferenceItemsUseCase: container.resolve(FetchReferenceItemsUseCase.self),
upsertUseCase: container.resolve(UpsertTodoUseCase.self),
todoId: item.id,
showEditButton: false
))
.toolbar {
ToolbarLeadingButton {
viewModel.send(.setSelectedTodoId(nil))
}
}
}
.background(Color(.secondarySystemBackground))
.presentationDragIndicator(.visible)
}
.overlay {
if viewModel.state.isLoading {
LoadingView()
}
}
}
}
private var notificationList: some View {
List {
Group {
if viewModel.state.notifications.isEmpty {
HStack {
Spacer()
Text(String(localized: "push_notifications_empty"))
.foregroundStyle(Color.gray)
Spacer()
}
.listRowSeparator(.hidden)
} else {
let notifications = viewModel.state.notifications
ForEach(Array(zip(notifications.indices, notifications)), id: \.1.id) { idx, notification in
Button {
viewModel.send(.tapNotification(notification))
} label: {
notificationRow(notification)
.padding(.vertical, 8)
}
.buttonStyle(.plain)
.onAppear {
let lastID = viewModel.state.notifications.last?.id
if notification.id == lastID, viewModel.state.hasMore {
viewModel.send(.loadNextPage)
}
}
.listRowInsets(EdgeInsets(top: 0, leading: 16, bottom: 0, trailing: 16))
.overlay(alignment: .top) {
if #available(iOS 26.0, *) {
if idx == 0 {
Divider()
.padding(.horizontal, -16)
}
}
}
}
}
}
.listSectionSeparator(.hidden, edges: .top)
.listRowBackground(Color.clear)
}
}
private var safeAreaHeader: some View {
VStack(spacing: 4) {
headerView
.clipped()
if #unavailable(iOS 26) {
Divider()
.padding(.horizontal, -16)
}
}
.background {
if #available(iOS 26.0, *) {
Color.clear
} else {
Color(.secondarySystemBackground)
}
}
.offset(y: headerOffset)
}
private var headerView: some View {
Group {
if #available(iOS 18, *) {
ScrollView(.horizontal) { headerContent }
.scrollIndicators(.never)
.scrollDisabled(!isScrollTrackingEnabled)
.contentMargins(.leading, 16, for: .scrollContent)
} else {
headerContent
.padding(.leading, 16)
.frame(maxWidth: .infinity, alignment: .leading)
}
}
}
private var headerContent: some View {
HStack(spacing: 8) {
if 0 < viewModel.appliedFilterCount {
Menu {
Text(
String.localizedStringWithFormat(
String(localized: "push_filters_applied_format"),
Int64(viewModel.appliedFilterCount)
)
)
Button(role: .destructive) {
viewModel.send(.resetFilters)
} label: {
Text(String(localized: "push_clear_all_filters"))
}
} label: {
HStack(spacing: 6) {
Image(systemName: "line.3.horizontal.decrease")
filterBadge
}
.adaptiveButtonStyle()
}
}
Button {
viewModel.send(.toggleSortOption)
} label: {
let condition = viewModel.state.query.sortOrder == .oldest
Text(
String.localizedStringWithFormat(
String(localized: "push_sort_format"),
viewModel.state.query.sortOrder.title
)
)
.foregroundStyle(condition ? .white : Color(.label))
.adaptiveButtonStyle(color: condition ? .blue : .clear)
}
Menu {
Picker(selection: Binding(
get: { viewModel.state.query.timeFilter },
set: { viewModel.send(.setTimeFilter($0)) }
)) {
ForEach(PushNotificationQuery.TimeFilter.availableOptions, id: \.self) { option in
Text(option.title).tag(option)
}
} label: {
Text(String(localized: "push_period"))
}
} label: {
let condition = viewModel.state.query.timeFilter == .none
HStack {
Text(String(localized: "push_period"))
Image(systemName: "chevron.down")
}
.foregroundStyle(condition ? Color(.label) : .white)
.adaptiveButtonStyle(color: condition ? .clear : .blue)
}
Button {
viewModel.send(.toggleUnreadOnly)
} label: {
let condition = viewModel.state.query.unreadOnly
Text(String(localized: "push_unread"))
.foregroundStyle(condition ? .white : Color(.label))
.adaptiveButtonStyle(color: condition ? .blue : .clear)
}
}
.frame(height: 36)
}
private var filterBadge: some View {
let isDark = colorScheme == .dark
let blue = Color(uiColor: .systemBlue) // 흰 배경에 따른 청록색화 방지
let textColor: Color = isDark ? blue : .white
let backgroundColor: Color = isDark ? .white : blue
return Text("\(viewModel.appliedFilterCount)")
.font(.caption2.weight(.bold))
.foregroundColor(textColor)
.lineLimit(1)
.minimumScaleFactor(0.6)
.frame(width: 20, height: 20)
.background(Circle().fill(backgroundColor))
}
// swiftlint:disable function_body_length
private func notificationRow(_ item: PushNotificationItem) -> some View {
HStack {
VStack {
let todoCategoryItem = TodoCategoryItem(from: item.todoCategory)
RoundedRectangle(cornerRadius: 8)
.fill(todoCategoryItem.color)
.frame(width: sceneWidth * 0.08, height: sceneWidth * 0.08)
.overlay {
Image(systemName: todoCategoryItem.symbolName)
.foregroundStyle(Color.white)
.font(.title3)
}
Circle()
.fill(Color.blue)
.frame(width: 8, height: 8)
.opacity(item.isRead ? 0 : 1)
}
VStack(alignment: .leading, spacing: 5) {
Text(item.title)
.font(.headline)
.lineLimit(1)
Text(item.body)
.font(.subheadline)
.foregroundStyle(Color.gray)
.lineLimit(1)
}
Spacer()
TimelineView(.periodic(from: .now, by: 1.0)) { context in
Text(timeAgoText(from: item.receivedAt, now: context.date))
.font(.caption2)
.foregroundStyle(Color.gray)
}
}
.padding(.vertical, 5)
.contentShape(.rect)
.swipeActions(edge: .leading) {
Button {
viewModel.send(.toggleRead(item))
} label: {
Image(systemName: "checkmark.circle\(item.isRead ? ".badge.xmark" : "")")
.tint(.blue)
}
}
.swipeActions(edge: .trailing) {
Button(
role: .destructive,
action: {
viewModel.send(.deleteNotification(item))
}
) {
Image(systemName: "trash")
}
}
}
// swiftlint:enable function_body_length
private func timeAgoText(from date: Date, now: Date) -> String {
let seconds = Int(now.timeIntervalSince(date))
if seconds < 60 {
return String.localizedStringWithFormat(
String(localized: "push_time_seconds_ago_format"),
Int64(max(0, seconds))
)
} else if seconds < 3600 {
let minutes = seconds / 60
return String.localizedStringWithFormat(
String(localized: "push_time_minutes_ago_format"),
Int64(minutes)
)
} else if seconds < 86400 {
let hours = seconds / 3600
return String.localizedStringWithFormat(
String(localized: "push_time_hours_ago_format"),
Int64(hours)
)
} else {
let days = seconds / 86400
return String.localizedStringWithFormat(
String(localized: "push_time_days_ago_format"),
Int64(days)
)
}
}
}