2026-02-10 02:22:41 +00: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
|
2026-02-12 01:21:19 +00:00
|
|
|
/// - Providing access to all todo task groups for iteration
|
|
|
|
|
/// - Efficiently counting todo tasks per group using SwiftData fetch descriptors
|
2026-02-10 02:22:41 +00:00
|
|
|
/// - 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.
|
|
|
|
|
///
|
2026-02-12 01:21:19 +00:00
|
|
|
/// - Returns: An array of all the todo group cases in a predetermined order.
|
2026-02-10 02:22:41 +00:00
|
|
|
var groups: [TodoGroup] {
|
|
|
|
|
TodoGroup.allCases
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// MARK: Functions
|
|
|
|
|
|
2026-02-12 01:21:19 +00:00
|
|
|
/// Calculates the number of todo tasks for a given todo group from an in-memory collection.
|
2026-02-10 02:22:41 +00:00
|
|
|
///
|
2026-02-12 01:21:19 +00:00
|
|
|
/// This method filters the provided todo tasks array to count only those todo tasks that belong
|
|
|
|
|
/// to the specified group based on their due date and completion status. Todo tasks are
|
2026-02-10 02:22:41 +00:00
|
|
|
/// categorized as follows:
|
|
|
|
|
///
|
2026-02-12 01:21:19 +00:00
|
|
|
/// - **Completed**: Todo tasks with a non-nil `completed` date
|
|
|
|
|
/// - **Overdue**: Incomplete todo tasks due before today
|
|
|
|
|
/// - **Today**: Incomplete todo tasks due today (from start of day to end of day)
|
|
|
|
|
/// - **Upcoming**: Incomplete todo tasks due tomorrow or later
|
2026-02-10 02:22:41 +00:00
|
|
|
///
|
|
|
|
|
/// - Parameters:
|
2026-02-12 01:21:19 +00:00
|
|
|
/// - todos: The array of todo tasks to filter and count.
|
2026-02-10 02:22:41 +00:00
|
|
|
/// - group: The task group to count tasks for.
|
|
|
|
|
/// - Returns: The number of tasks in the specified group.
|
|
|
|
|
func count(
|
2026-02-12 01:21:19 +00:00
|
|
|
_ todos: [Todo],
|
2026-02-10 02:22:41 +00:00
|
|
|
for group: TodoGroup
|
|
|
|
|
) -> Int {
|
|
|
|
|
let today = calendar.startOfDay(for: .now)
|
|
|
|
|
let tomorrow = calendar.date(byAdding: .day, value: 1, to: today) ?? .now
|
|
|
|
|
|
2026-02-12 01:21:19 +00:00
|
|
|
return todos
|
|
|
|
|
.filter { todo in
|
2026-02-10 02:22:41 +00:00
|
|
|
switch group {
|
|
|
|
|
case .completed:
|
2026-02-12 01:21:19 +00:00
|
|
|
todo.completed != nil
|
2026-02-10 02:22:41 +00:00
|
|
|
case .overdue:
|
2026-02-12 01:21:19 +00:00
|
|
|
todo.completed == nil && todo.due < today
|
2026-02-10 02:22:41 +00:00
|
|
|
case .today:
|
2026-02-12 01:21:19 +00:00
|
|
|
todo.completed == nil && todo.due >= today && todo.due < tomorrow
|
2026-02-10 02:22:41 +00:00
|
|
|
case .upcoming:
|
2026-02-12 01:21:19 +00:00
|
|
|
todo.completed == nil && todo.due >= tomorrow
|
2026-02-10 02:22:41 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
.count
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|