2026-02-10 02:15:26 +01:00
|
|
|
import SwiftData
|
|
|
|
|
import SwiftUI
|
|
|
|
|
|
|
|
|
|
extension TaskGroupListView {
|
|
|
|
|
|
|
|
|
|
/// View model for managing the logic for the task group list view.
|
|
|
|
|
///
|
|
|
|
|
/// This observable class encapsulates the state and business logic for the task group list view.
|
|
|
|
|
/// It follows the MVVM pattern to separate presentation logic from the view layer.
|
|
|
|
|
///
|
|
|
|
|
/// ## Responsibilities
|
|
|
|
|
/// - Providing access to all task groups for iteration
|
|
|
|
|
/// - Efficiently counting tasks per group using SwiftData fetch descriptors
|
|
|
|
|
/// - Managing date calculations for group categorization
|
|
|
|
|
@Observable
|
|
|
|
|
final class ViewModel {
|
|
|
|
|
|
|
|
|
|
// MARK: Properties
|
|
|
|
|
|
|
|
|
|
/// The calendar used for date calculations.
|
|
|
|
|
///
|
|
|
|
|
/// This calendar is used to determine day boundaries for categorizing tasks into date groups: today, upcoming, overdue.
|
|
|
|
|
/// Defaults to the current calendar.
|
|
|
|
|
private let calendar: Calendar
|
|
|
|
|
|
|
|
|
|
// MARK: Initialization
|
|
|
|
|
|
|
|
|
|
/// Initializes a new view model instance.
|
|
|
|
|
///
|
|
|
|
|
/// - Parameter calendar: The calendar to use for date calculations. Defaults to `.current`.
|
|
|
|
|
init(calendar: Calendar = .current) {
|
|
|
|
|
self.calendar = calendar
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// MARK: Computed
|
|
|
|
|
|
|
|
|
|
/// Returns all available task groups in an predetermined order.
|
|
|
|
|
///
|
|
|
|
|
/// This computed property provides access to all task group cases for iteration in the view.
|
|
|
|
|
///
|
|
|
|
|
/// - Returns: An array of all `TodoGroup` cases in a predetermined order.
|
|
|
|
|
var groups: [TodoGroup] {
|
|
|
|
|
TodoGroup.allCases
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// MARK: Functions
|
|
|
|
|
|
2026-02-10 03:18:56 +01:00
|
|
|
/// Calculates the number of tasks for a given task group from an in-memory collection.
|
2026-02-10 02:15:26 +01:00
|
|
|
///
|
2026-02-10 03:18:56 +01:00
|
|
|
/// 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
|
2026-02-10 02:15:26 +01:00
|
|
|
///
|
|
|
|
|
/// - Parameters:
|
2026-02-10 03:18:56 +01:00
|
|
|
/// - tasks: The array of tasks to filter and count.
|
2026-02-10 02:15:26 +01:00
|
|
|
/// - group: The task group to count tasks for.
|
2026-02-10 03:18:56 +01:00
|
|
|
/// - Returns: The number of tasks in the specified group.
|
2026-02-10 02:15:26 +01:00
|
|
|
func count(
|
2026-02-10 03:18:56 +01:00
|
|
|
_ tasks: [Todo],
|
|
|
|
|
for group: TodoGroup
|
2026-02-10 02:15:26 +01:00
|
|
|
) -> Int {
|
|
|
|
|
let today = calendar.startOfDay(for: .now)
|
|
|
|
|
let tomorrow = calendar.date(byAdding: .day, value: 1, to: today) ?? .now
|
|
|
|
|
|
2026-02-10 03:18:56 +01:00
|
|
|
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
|
|
|
|
|
}
|
2026-02-10 02:15:26 +01:00
|
|
|
}
|
2026-02-10 03:18:56 +01:00
|
|
|
.count
|
2026-02-10 02:15:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|