Task edition improvements #9
@@ -9,8 +9,8 @@ extension TaskGroupListView {
|
|||||||
/// It follows the MVVM pattern to separate presentation logic from the view layer.
|
/// It follows the MVVM pattern to separate presentation logic from the view layer.
|
||||||
///
|
///
|
||||||
/// ## Responsibilities
|
/// ## Responsibilities
|
||||||
/// - Providing access to all task groups for iteration
|
/// - Providing access to all todo task groups for iteration
|
||||||
/// - Efficiently counting tasks per group using SwiftData fetch descriptors
|
/// - Efficiently counting todo tasks per group using SwiftData fetch descriptors
|
||||||
/// - Managing date calculations for group categorization
|
/// - Managing date calculations for group categorization
|
||||||
@Observable
|
@Observable
|
||||||
final class ViewModel {
|
final class ViewModel {
|
||||||
@@ -38,46 +38,46 @@ extension TaskGroupListView {
|
|||||||
///
|
///
|
||||||
/// This computed property provides access to all task group cases for iteration in the view.
|
/// 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.
|
/// - Returns: An array of all the todo group cases in a predetermined order.
|
||||||
var groups: [TodoGroup] {
|
var groups: [TodoGroup] {
|
||||||
TodoGroup.allCases
|
TodoGroup.allCases
|
||||||
}
|
}
|
||||||
|
|
||||||
// MARK: Functions
|
// MARK: Functions
|
||||||
|
|
||||||
/// Calculates the number of tasks for a given task group from an in-memory collection.
|
/// Calculates the number of todo tasks for a given todo group from an in-memory collection.
|
||||||
///
|
///
|
||||||
/// This method filters the provided tasks array to count only those tasks that belong
|
/// 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. Tasks are
|
/// to the specified group based on their due date and completion status. Todo tasks are
|
||||||
/// categorized as follows:
|
/// categorized as follows:
|
||||||
///
|
///
|
||||||
/// - **Completed**: Tasks with a non-nil `completed` date
|
/// - **Completed**: Todo tasks with a non-nil `completed` date
|
||||||
/// - **Overdue**: Incomplete tasks due before today
|
/// - **Overdue**: Incomplete todo tasks due before today
|
||||||
/// - **Today**: Incomplete tasks due today (from start of day to end of day)
|
/// - **Today**: Incomplete todo tasks due today (from start of day to end of day)
|
||||||
/// - **Upcoming**: Incomplete tasks due tomorrow or later
|
/// - **Upcoming**: Incomplete todo tasks due tomorrow or later
|
||||||
///
|
///
|
||||||
/// - Parameters:
|
/// - Parameters:
|
||||||
/// - tasks: The array of tasks to filter and count.
|
/// - todos: The array of todo tasks to filter and count.
|
||||||
/// - group: The task group to count tasks for.
|
/// - group: The task group to count tasks for.
|
||||||
/// - Returns: The number of tasks in the specified group.
|
/// - Returns: The number of tasks in the specified group.
|
||||||
func count(
|
func count(
|
||||||
_ tasks: [Todo],
|
_ todos: [Todo],
|
||||||
for group: TodoGroup
|
for group: TodoGroup
|
||||||
) -> Int {
|
) -> Int {
|
||||||
let today = calendar.startOfDay(for: .now)
|
let today = calendar.startOfDay(for: .now)
|
||||||
let tomorrow = calendar.date(byAdding: .day, value: 1, to: today) ?? .now
|
let tomorrow = calendar.date(byAdding: .day, value: 1, to: today) ?? .now
|
||||||
|
|
||||||
return tasks
|
return todos
|
||||||
.filter { task in
|
.filter { todo in
|
||||||
switch group {
|
switch group {
|
||||||
case .completed:
|
case .completed:
|
||||||
task.completed != nil
|
todo.completed != nil
|
||||||
case .overdue:
|
case .overdue:
|
||||||
task.completed == nil && task.due < today
|
todo.completed == nil && todo.due < today
|
||||||
case .today:
|
case .today:
|
||||||
task.completed == nil && task.due >= today && task.due < tomorrow
|
todo.completed == nil && todo.due >= today && todo.due < tomorrow
|
||||||
case .upcoming:
|
case .upcoming:
|
||||||
task.completed == nil && task.due >= tomorrow
|
todo.completed == nil && todo.due >= tomorrow
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
.count
|
.count
|
||||||
|
|||||||
@@ -85,52 +85,52 @@ extension TaskListView {
|
|||||||
|
|
||||||
// MARK: Methods
|
// MARK: Methods
|
||||||
|
|
||||||
/// Deletes tasks at the specified indices.
|
/// Deletes todo tasks at the specified indices.
|
||||||
///
|
///
|
||||||
/// This method removes tasks from the repository after a swipe-to-delete action.
|
/// This method removes tasks from the repository after a swipe-to-delete action.
|
||||||
/// The deletion is performed through the repository, which handles
|
/// The deletion is performed through the repository, which handles
|
||||||
/// persistence and triggers automatic UI updates.
|
/// persistence and triggers automatic UI updates.
|
||||||
///
|
///
|
||||||
/// - Parameters:
|
/// - Parameters:
|
||||||
/// - tasks: The current array of tasks.
|
/// - todos: The current array of todo tasks.
|
||||||
/// - offsets: The index set of tasks to delete.
|
/// - offsets: The index set of tasks to delete.
|
||||||
/// - repository: The repository to perform the deletion.
|
/// - repository: The repository to perform the deletion.
|
||||||
func delete(
|
func delete(
|
||||||
_ tasks: [Todo],
|
_ todos: [Todo],
|
||||||
at offsets: IndexSet,
|
at offsets: IndexSet,
|
||||||
from repository: TodoRepository
|
from repository: TodoRepository
|
||||||
) {
|
) {
|
||||||
for index in offsets {
|
for index in offsets {
|
||||||
repository.delete(tasks[index])
|
repository.delete(todos[index])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Reorders tasks after a drag and drop operation.
|
/// Reorders todo tasks after a drag and drop operation.
|
||||||
///
|
///
|
||||||
/// This method is called when users drag and drop tasks to reorder them within a group.
|
/// This method is called when users drag and drop tasks to reorder them within a group.
|
||||||
/// It updates the `order` property of all tasks to reflect their new positions, ensuring
|
/// It updates the `order` property of all todo tasks to reflect their new positions, ensuring
|
||||||
/// that the user's manual ordering preference is persisted.
|
/// that the user's manual ordering preference is persisted.
|
||||||
///
|
///
|
||||||
/// - Parameters:
|
/// - Parameters:
|
||||||
/// - tasks: The current array of tasks before reordering.
|
/// - todos: The current array of todo tasks before reordering.
|
||||||
/// - source: The index set of items being moved (typically a single index).
|
/// - source: The index set of items being moved (typically a single index).
|
||||||
/// - destination: The destination index where items should be inserted.
|
/// - destination: The destination index where items should be inserted.
|
||||||
/// - Note: This method should only be called for groups where `canReorder` is `true`.
|
/// - Note: This method should only be called for groups where `canReorder` is `true`.
|
||||||
/// The view layer is responsible for only enabling drag and drop for applicable groups.
|
/// The view layer is responsible for only enabling drag and drop for applicable groups.
|
||||||
/// - SeeAlso: ``TodoGroup/canReorder`` for determining which groups support reordering.
|
/// - SeeAlso: ``TodoGroup/canReorder`` for determining which groups support reordering.
|
||||||
func reorderTasks(
|
func reorderTasks(
|
||||||
_ tasks: [Todo],
|
_ todos: [Todo],
|
||||||
from source: IndexSet,
|
from source: IndexSet,
|
||||||
to destination: Int
|
to destination: Int
|
||||||
) {
|
) {
|
||||||
var updatedTasks = tasks
|
var todosUpdated = todos
|
||||||
|
|
||||||
updatedTasks.move(
|
todosUpdated.move(
|
||||||
fromOffsets: source,
|
fromOffsets: source,
|
||||||
toOffset: destination
|
toOffset: destination
|
||||||
)
|
)
|
||||||
|
|
||||||
for (index, task) in updatedTasks.enumerated() {
|
for (index, task) in todosUpdated.enumerated() {
|
||||||
task.order = index
|
task.order = index
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user