Implemented the view refresh after data updates for the TaskGroupListView view in the app target.
This commit is contained in:
@@ -45,41 +45,42 @@ extension TaskGroupListView {
|
||||
|
||||
// MARK: Functions
|
||||
|
||||
/// Calculates the number of tasks for a given task group using SwiftData.
|
||||
/// Calculates the number of tasks for a given task group from an in-memory collection.
|
||||
///
|
||||
/// This method performs an efficient database-level count using SwiftData's `fetchCount` method with predicate-based filtering.
|
||||
/// Tasks are categorized based on their due date and completion status.
|
||||
/// This method filters the provided tasks array to count only those tasks that belong
|
||||
/// to the specified group based on their due date and completion status. Tasks are
|
||||
/// categorized as follows:
|
||||
///
|
||||
/// - **Completed**: Tasks with a non-nil `completed` date
|
||||
/// - **Overdue**: Incomplete tasks due before today
|
||||
/// - **Today**: Incomplete tasks due today (from start of day to end of day)
|
||||
/// - **Upcoming**: Incomplete tasks due tomorrow or later
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - tasks: The array of tasks to filter and count.
|
||||
/// - group: The task group to count tasks for.
|
||||
/// - modelContext: The SwiftData model context to query.
|
||||
/// - Returns: The number of tasks in the specified group, or 0 if an error occurs.
|
||||
/// - Returns: The number of tasks in the specified group.
|
||||
func count(
|
||||
for group: TodoGroup,
|
||||
in modelContext: ModelContext
|
||||
_ tasks: [Todo],
|
||||
for group: TodoGroup
|
||||
) -> Int {
|
||||
let today = calendar.startOfDay(for: .now)
|
||||
let tomorrow = calendar.date(byAdding: .day, value: 1, to: today) ?? .now
|
||||
|
||||
let fetchDescriptor: FetchDescriptor<Todo> =
|
||||
switch group {
|
||||
case .completed:
|
||||
.init(predicate: #Predicate { $0.completed != nil })
|
||||
case .overdue:
|
||||
.init(predicate: #Predicate { $0.completed == nil && $0.due < today })
|
||||
case .today:
|
||||
.init(predicate: #Predicate { $0.completed == nil && $0.due >= today && $0.due < tomorrow })
|
||||
case .upcoming:
|
||||
.init(predicate: #Predicate { $0.completed == nil && $0.due >= tomorrow })
|
||||
return tasks
|
||||
.filter { task in
|
||||
switch group {
|
||||
case .completed:
|
||||
task.completed != nil
|
||||
case .overdue:
|
||||
task.completed == nil && task.due < today
|
||||
case .today:
|
||||
task.completed == nil && task.due >= today && task.due < tomorrow
|
||||
case .upcoming:
|
||||
task.completed == nil && task.due >= tomorrow
|
||||
}
|
||||
}
|
||||
|
||||
do {
|
||||
return try modelContext.fetchCount(fetchDescriptor)
|
||||
}
|
||||
catch {
|
||||
print("Error fetching count for group \(group): \(error)")
|
||||
return 0
|
||||
}
|
||||
.count
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user