|
| 1 | +// |
| 2 | +// TodayFeature+State.swift |
| 3 | +// DevLogPresentation |
| 4 | +// |
| 5 | +// Created by opfic on 6/14/26. |
| 6 | +// |
| 7 | + |
| 8 | +import DevLogCore |
| 9 | +import Foundation |
| 10 | + |
| 11 | +extension TodayFeature { |
| 12 | + static func summaryValue( |
| 13 | + for scope: SectionScope, |
| 14 | + todos: [TodayTodoItem], |
| 15 | + displayOptions: TodayDisplayOptions, |
| 16 | + now: Date |
| 17 | + ) -> Int { |
| 18 | + let displayedTodos = displayedTodos( |
| 19 | + todos: todos, |
| 20 | + displayOptions: displayOptions |
| 21 | + ) |
| 22 | + |
| 23 | + switch scope { |
| 24 | + case .all: |
| 25 | + return displayedTodos.count |
| 26 | + case .focused: |
| 27 | + return displayedTodos.filter(\.isPinned).count |
| 28 | + case .overdue: |
| 29 | + return displayedTodos.filter { isOverdue($0, now: now) }.count |
| 30 | + case .dueSoon: |
| 31 | + return displayedTodos.filter { isDueSoon($0, now: now) }.count |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + static func displayedTodos( |
| 36 | + todos: [TodayTodoItem], |
| 37 | + displayOptions: TodayDisplayOptions |
| 38 | + ) -> [TodayTodoItem] { |
| 39 | + let dueDateFilteredTodos: [TodayTodoItem] |
| 40 | + switch displayOptions.dueDateVisibility { |
| 41 | + case .all: |
| 42 | + dueDateFilteredTodos = todos |
| 43 | + case .withDueDateOnly: |
| 44 | + dueDateFilteredTodos = todos.filter { $0.dueDate != nil } |
| 45 | + case .withoutDueDateOnly: |
| 46 | + dueDateFilteredTodos = todos.filter { $0.dueDate == nil } |
| 47 | + } |
| 48 | + |
| 49 | + switch displayOptions.focusVisibility { |
| 50 | + case .all: |
| 51 | + return dueDateFilteredTodos |
| 52 | + case .focusedOnly: |
| 53 | + return dueDateFilteredTodos.filter(\.isPinned) |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + static func groupedSectionItems( |
| 58 | + from items: [TodayTodoItem], |
| 59 | + now: Date |
| 60 | + ) -> TodayFeature.SectionCollection { |
| 61 | + let calendar = Calendar.current |
| 62 | + let startOfToday = calendar.startOfDay(for: now) |
| 63 | + guard let windowEnd = calendar.date( |
| 64 | + byAdding: .day, |
| 65 | + value: TodayFeature.upcomingWindowDays, |
| 66 | + to: startOfToday |
| 67 | + ) else { |
| 68 | + return TodayFeature.SectionCollection( |
| 69 | + focused: items.filter(\.isPinned), |
| 70 | + unscheduled: items.filter { !$0.isPinned && $0.dueDate == nil } |
| 71 | + ) |
| 72 | + } |
| 73 | + |
| 74 | + var collection = TodayFeature.SectionCollection() |
| 75 | + |
| 76 | + for item in items { |
| 77 | + if item.isPinned { |
| 78 | + collection.focused.append(item) |
| 79 | + continue |
| 80 | + } |
| 81 | + |
| 82 | + guard let dueDate = item.dueDate else { |
| 83 | + collection.unscheduled.append(item) |
| 84 | + continue |
| 85 | + } |
| 86 | + |
| 87 | + let dueDay = calendar.startOfDay(for: dueDate) |
| 88 | + if dueDay < startOfToday { |
| 89 | + collection.overdue.append(item) |
| 90 | + } else if dueDay <= windowEnd { |
| 91 | + collection.dueSoon.append(item) |
| 92 | + } else { |
| 93 | + collection.later.append(item) |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + return collection |
| 98 | + } |
| 99 | + |
| 100 | + static func isOverdue(_ item: TodayTodoItem, now: Date) -> Bool { |
| 101 | + guard let dueDate = item.dueDate else { return false } |
| 102 | + let calendar = Calendar.current |
| 103 | + return calendar.startOfDay(for: dueDate) < calendar.startOfDay(for: now) |
| 104 | + } |
| 105 | + |
| 106 | + static func isDueSoon(_ item: TodayTodoItem, now: Date) -> Bool { |
| 107 | + guard let dueDate = item.dueDate else { return false } |
| 108 | + let calendar = Calendar.current |
| 109 | + let startOfToday = calendar.startOfDay(for: now) |
| 110 | + guard let windowEnd = calendar.date( |
| 111 | + byAdding: .day, |
| 112 | + value: TodayFeature.upcomingWindowDays, |
| 113 | + to: startOfToday |
| 114 | + ) else { |
| 115 | + return false |
| 116 | + } |
| 117 | + let dueDay = calendar.startOfDay(for: dueDate) |
| 118 | + return startOfToday <= dueDay && dueDay <= windowEnd |
| 119 | + } |
| 120 | + |
| 121 | + static func makeSection( |
| 122 | + category: SectionCategory, |
| 123 | + title: String, |
| 124 | + items: [TodayTodoItem] |
| 125 | + ) -> [SectionContent] { |
| 126 | + guard !items.isEmpty else { return [] } |
| 127 | + return [SectionContent(category: category, title: title, items: items)] |
| 128 | + } |
| 129 | +} |
0 commit comments