Show completed task feature (#5)

This PR contains the work done to add support for showing or hiding completed todo tasks to the *Task list* view.

Reviewed-on: rock-n-code/todos-app#5
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 #5.
This commit is contained in:
2026-02-11 13:00:46 +00:00
committed by javier
parent 2a3323b8e6
commit cbf33302dd
9 changed files with 457 additions and 63 deletions
@@ -19,36 +19,50 @@ extension FetchDescriptor where T == Todo {
///
/// - Parameters:
/// - group: The todo group category to filter by.
/// - showCompleted: Whether to include completed tasks. Only applies to groups other than `.completed`.
/// - calendar: The calendar to use for date calculations. Defaults to `.current`.
init(
group: TodoGroup,
showCompleted: Bool,
calendar: Calendar = .current
) {
let predicate: Predicate<Todo>
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)]
)
predicate = #Predicate { $0.completed != nil }
case .overdue:
self.init(
predicate: #Predicate { $0.completed == nil && $0.due < today },
sortBy: [.init(\.due, order: .reverse)]
)
predicate = showCompleted
? #Predicate { $0.due < today }
: #Predicate { $0.completed == nil && $0.due < today }
case .today:
self.init(
predicate: #Predicate { $0.completed == nil && $0.due >= today && $0.due < tomorrow },
sortBy: [.init(\.due, order: .forward)]
)
predicate = showCompleted
? #Predicate { $0.due >= today && $0.due < tomorrow }
: #Predicate { $0.completed == nil && $0.due >= today && $0.due < tomorrow }
case .upcoming:
self.init(
predicate: #Predicate { $0.completed == nil && $0.due >= tomorrow },
sortBy: [.init(\.due, order: .forward)]
)
predicate = showCompleted
? #Predicate { $0.due >= tomorrow }
: #Predicate { $0.completed == nil && $0.due >= tomorrow }
}
let sortDescriptors: [SortDescriptor] =
switch group {
case .completed:
[.init(\Todo.completed, order: .reverse)]
case .overdue:
[.init(\Todo.due, order: .reverse)]
case .today:
[.init(\Todo.due, order: .forward)]
case .upcoming:
[.init(\Todo.due, order: .forward)]
}
self.init(
predicate: predicate,
sortBy: sortDescriptors
)
}
}