Files
todos/App/Sources/Extensions/TodoFetchDescriptor+Inits.swift
T

72 lines
3.3 KiB
Swift
Raw Normal View History

2026-02-10 15:31:52 +00:00
import Foundation
import SwiftData
/// Extension providing convenient initializers for fetching todos by group category.
///
/// This extension allows creating fetch descriptors that automatically filter and sort todos
/// based on their completion status and due dates according to the specified `TodoGroup`.
extension FetchDescriptor where T == Todo {
/// Creates a fetch descriptor for todos filtered and sorted by a specific group.
///
/// This initializer provides a convenient way to fetch todos categorized by their temporal
/// relevance and completion status. Each group applies appropriate predicates and sort orders:
///
/// - **Completed**: All tasks marked as done, sorted by completion date (most recent first)
/// - **Overdue**: Incomplete tasks with due dates before today, sorted by user-defined order, then by due date (most recent first)
/// - **Today**: Incomplete tasks due today, sorted by user-defined order, then by due date (earliest first)
/// - **Upcoming**: Incomplete tasks due after today, sorted by user-defined order, then by due date (earliest first)
///
/// The user-defined order allows tasks to be manually reordered within each group through drag and drop,
/// while maintaining a secondary sort by date for tasks with the same order value.
2026-02-10 15:31:52 +00:00
///
/// - Parameters:
/// - group: The todo group category to filter by.
2026-02-11 13:00:46 +00:00
/// - showCompleted: Whether to include completed tasks. Only applies to groups other than `.completed`.
2026-02-10 15:31:52 +00:00
/// - calendar: The calendar to use for date calculations. Defaults to `.current`.
init(
group: TodoGroup,
2026-02-11 13:00:46 +00:00
showCompleted: Bool,
2026-02-10 15:31:52 +00:00
calendar: Calendar = .current
) {
2026-02-11 13:00:46 +00:00
let predicate: Predicate<Todo>
2026-02-10 15:31:52 +00:00
let today = calendar.startOfDay(for: .now)
let tomorrow = calendar.date(byAdding: .day, value: 1, to: today) ?? today
switch group {
case .completed:
2026-02-11 13:00:46 +00:00
predicate = #Predicate { $0.completed != nil }
2026-02-10 15:31:52 +00:00
case .overdue:
2026-02-11 13:00:46 +00:00
predicate = showCompleted
? #Predicate { $0.due < today }
: #Predicate { $0.completed == nil && $0.due < today }
2026-02-10 15:31:52 +00:00
case .today:
2026-02-11 13:00:46 +00:00
predicate = showCompleted
? #Predicate { $0.due >= today && $0.due < tomorrow }
: #Predicate { $0.completed == nil && $0.due >= today && $0.due < tomorrow }
2026-02-10 15:31:52 +00:00
case .upcoming:
2026-02-11 13:00:46 +00:00
predicate = showCompleted
? #Predicate { $0.due >= tomorrow }
: #Predicate { $0.completed == nil && $0.due >= tomorrow }
2026-02-10 15:31:52 +00:00
}
2026-02-11 13:00:46 +00:00
let sortDescriptors: [SortDescriptor] =
switch group {
case .completed:
[.init(\Todo.completed, order: .reverse)]
case .overdue:
[.init(\Todo.order, order: .forward), .init(\Todo.due, order: .reverse)]
2026-02-11 13:00:46 +00:00
case .today:
[.init(\Todo.order, order: .forward), .init(\Todo.due, order: .forward)]
2026-02-11 13:00:46 +00:00
case .upcoming:
[.init(\Todo.order, order: .forward), .init(\Todo.due, order: .forward)]
2026-02-11 13:00:46 +00:00
}
self.init(
predicate: predicate,
sortBy: sortDescriptors
)
2026-02-10 15:31:52 +00:00
}
}