List tasks feature (#4)
This PR contains the work done to implement the **List Tasks** feature, and to integrate it into the main content view of the app. Reviewed-on: rock-n-code/todos-app#4 Co-authored-by: Javier Cicchelli <javier@rock-n-code.com> Co-committed-by: Javier Cicchelli <javier@rock-n-code.com>
This commit was merged in pull request #4.
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
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 due date (most recent first)
|
||||
/// - **Today**: Incomplete tasks due today, sorted by due date (earliest first)
|
||||
/// - **Upcoming**: Incomplete tasks due after today, sorted by due date (earliest first)
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - group: The todo group category to filter by.
|
||||
/// - calendar: The calendar to use for date calculations. Defaults to `.current`.
|
||||
init(
|
||||
group: TodoGroup,
|
||||
calendar: Calendar = .current
|
||||
) {
|
||||
let today = calendar.startOfDay(for: .now)
|
||||
let tomorrow = calendar.date(byAdding: .day, value: 1, to: today) ?? today
|
||||
|
||||
switch group {
|
||||
case .completed:
|
||||
self.init(
|
||||
predicate: #Predicate { $0.completed != nil },
|
||||
sortBy: [.init(\.completed, order: .reverse)]
|
||||
)
|
||||
case .overdue:
|
||||
self.init(
|
||||
predicate: #Predicate { $0.completed == nil && $0.due < today },
|
||||
sortBy: [.init(\.due, order: .reverse)]
|
||||
)
|
||||
case .today:
|
||||
self.init(
|
||||
predicate: #Predicate { $0.completed == nil && $0.due >= today && $0.due < tomorrow },
|
||||
sortBy: [.init(\.due, order: .forward)]
|
||||
)
|
||||
case .upcoming:
|
||||
self.init(
|
||||
predicate: #Predicate { $0.completed == nil && $0.due >= tomorrow },
|
||||
sortBy: [.init(\.due, order: .forward)]
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user