-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTodayWidgetSnapshotFactory.swift
More file actions
201 lines (179 loc) · 5.94 KB
/
Copy pathTodayWidgetSnapshotFactory.swift
File metadata and controls
201 lines (179 loc) · 5.94 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
//
// TodayWidgetSnapshotFactory.swift
// DevLogWidgetCore
//
// Created by opfic on 4/17/26.
//
import Foundation
import DevLogCore
public struct TodayWidgetSnapshotFactory {
private enum SectionCategory: String, CaseIterable {
case focused
case overdue
case dueSoon
case later
case unscheduled
}
private struct SectionCollection {
var focused = [TodayWidgetTodoItem]()
var overdue = [TodayWidgetTodoItem]()
var dueSoon = [TodayWidgetTodoItem]()
var later = [TodayWidgetTodoItem]()
var unscheduled = [TodayWidgetTodoItem]()
func items(for category: SectionCategory) -> [TodayWidgetTodoItem] {
switch category {
case .focused:
focused
case .overdue:
overdue
case .dueSoon:
dueSoon
case .later:
later
case .unscheduled:
unscheduled
}
}
}
private struct TodayWidgetTodoItem {
let id: String
let number: Int
let title: String
let isPinned: Bool
let dueDate: Date?
init?(from todo: WidgetTodoSnapshot) {
guard let number = todo.number else { return nil }
self.id = todo.id
self.number = number
self.title = todo.title
self.isPinned = todo.isPinned
self.dueDate = todo.dueDate
}
}
private let calendar: Calendar
private let upcomingWindowDays: Int
public init(
calendar: Calendar = .current,
upcomingWindowDays: Int = 7
) {
self.calendar = calendar
self.upcomingWindowDays = upcomingWindowDays
}
public func makeSnapshot(
todos: [WidgetTodoSnapshot],
displayOptions: TodayDisplayOptions,
now: Date = Date()
) -> TodayWidgetSnapshot {
let todayWidgetTodoItems = todos.compactMap { TodayWidgetTodoItem(from: $0) }
let displayedTodos = displayedTodos(
from: todayWidgetTodoItems,
displayOptions: displayOptions
)
let sections = groupedSectionItems(
from: displayedTodos,
now: now
)
return TodayWidgetSnapshot(
generatedAt: now,
totalCount: displayedTodos.count,
focusedCount: displayedTodos.filter(\.isPinned).count,
overdueCount: displayedTodos.filter { isOverdue($0, now: now) }.count,
dueSoonCount: displayedTodos.filter { isDueSoon($0, now: now) }.count,
sections: SectionCategory.allCases.compactMap { category in
let items = sections.items(for: category)
guard !items.isEmpty else { return nil }
return TodayWidgetSectionSnapshot(
category: category.rawValue,
items: items.map {
WidgetTodoSnapshotItem(
id: $0.id,
number: $0.number,
title: $0.title,
isPinned: $0.isPinned,
dueDate: $0.dueDate
)
}
)
}
)
}
private func displayedTodos(
from todos: [TodayWidgetTodoItem],
displayOptions: TodayDisplayOptions
) -> [TodayWidgetTodoItem] {
let dueDateFilteredTodos: [TodayWidgetTodoItem]
switch displayOptions.dueDateVisibility {
case .all:
dueDateFilteredTodos = todos
case .withDueDateOnly:
dueDateFilteredTodos = todos.filter { $0.dueDate != nil }
case .withoutDueDateOnly:
dueDateFilteredTodos = todos.filter { $0.dueDate == nil }
}
switch displayOptions.focusVisibility {
case .all:
return dueDateFilteredTodos
case .focusedOnly:
return dueDateFilteredTodos.filter(\.isPinned)
}
}
private func groupedSectionItems(
from items: [TodayWidgetTodoItem],
now: Date
) -> SectionCollection {
let startOfToday = calendar.startOfDay(for: now)
guard let windowEnd = calendar.date(
byAdding: .day,
value: upcomingWindowDays,
to: startOfToday
) else {
return SectionCollection(
focused: items.filter(\.isPinned),
unscheduled: items.filter { !$0.isPinned && $0.dueDate == nil }
)
}
var collection = SectionCollection()
for item in items {
if item.isPinned {
collection.focused.append(item)
continue
}
guard let dueDate = item.dueDate else {
collection.unscheduled.append(item)
continue
}
let dueDay = calendar.startOfDay(for: dueDate)
if dueDay < startOfToday {
collection.overdue.append(item)
} else if dueDay <= windowEnd {
collection.dueSoon.append(item)
} else {
collection.later.append(item)
}
}
return collection
}
private func isOverdue(
_ item: TodayWidgetTodoItem,
now: Date
) -> Bool {
guard let dueDate = item.dueDate else { return false }
return calendar.startOfDay(for: dueDate) < calendar.startOfDay(for: now)
}
private func isDueSoon(
_ item: TodayWidgetTodoItem,
now: Date
) -> Bool {
guard let dueDate = item.dueDate else { return false }
let startOfToday = calendar.startOfDay(for: now)
guard let windowEnd = calendar.date(
byAdding: .day,
value: upcomingWindowDays,
to: startOfToday
) else {
return false
}
let dueDay = calendar.startOfDay(for: dueDate)
return startOfToday <= dueDay && dueDay <= windowEnd
}
}