Files
todos/App/Sources/View Models/TaskListViewModel.swift
T

148 lines
5.8 KiB
Swift
Raw Normal View History

import Foundation
2026-02-10 15:31:52 +00:00
import SwiftData
import SwiftUI
extension TaskListView {
2026-02-11 13:00:46 +00:00
/// View model for managing the state and business logic of the task list view.
2026-02-10 15:31:52 +00:00
///
2026-02-11 13:00:46 +00:00
/// This observable class encapsulates the presentation logic for the task list view, following the MVVM pattern to
/// separate concerns between the view and business logic layers.
2026-02-10 15:31:52 +00:00
///
/// ## Responsibilities
2026-02-11 13:00:46 +00:00
///
/// - Maintains the task group category (today, upcoming, overdue, completed)
/// - Manages the visibility preference for completed tasks
/// - Provides the localized title for the navigation bar
/// - Determines whether the completion filter toggle should be displayed
/// - Provides label and icon configuration for the completion toggle button
2026-02-11 15:28:22 +00:00
/// - Handles task reordering through drag and drop interactions
2026-02-10 15:31:52 +00:00
@Observable
final class ViewModel {
// MARK: Properties
2026-02-11 13:00:46 +00:00
/// Whether to show completed tasks in the list.
///
/// This property controls the inclusion of completed tasks in the query results:
/// - `true`: Completed tasks are included in the fetch results
/// - `false`: Completed tasks are filtered out at the database level
var showCompleted: Bool
2026-02-10 15:31:52 +00:00
2026-02-11 15:28:22 +00:00
/// The task group to display tasks for.
///
/// This property determines which tasks are fetched and displayed in the list.
/// It is used by the content view to initialize the SwiftData query with the appropriate
/// predicates and sort descriptors for the group.
let group: TodoGroup
2026-02-10 15:31:52 +00:00
// MARK: Initialization
/// Initializes a new view model instance for a specific task group.
///
/// - Parameter group: The task group to display tasks for.
init(group: TodoGroup) {
self.group = group
2026-02-11 15:28:22 +00:00
self.showCompleted = false
2026-02-10 15:31:52 +00:00
}
2026-02-11 13:00:46 +00:00
// MARK: Computed Properties
2026-02-10 15:31:52 +00:00
2026-02-11 13:00:46 +00:00
/// The localized button title and icon for the show/hide completed todo tasks.
2026-02-10 15:31:52 +00:00
///
2026-02-11 13:00:46 +00:00
/// This computed property provides the appropriate label and icon based on
/// the current `showCompleted` state.
2026-02-10 15:31:52 +00:00
///
2026-02-11 13:00:46 +00:00
/// - Returns: A tuple containing the localized title key and SF Symbol icon name.
var buttonCompleted: (title: LocalizedStringKey, icon: String) {
let title: LocalizedStringKey = showCompleted
? "view.task-list.label.hide-completed"
: "view.task-list.label.show-completed"
let icon = showCompleted
? "eye.slash"
: "eye"
return (title, icon)
}
/// Whether the completed todo tasks should be displayed.
///
/// This property delegates to the group's `canShowCompleted` property to determine if completed todo tasks should be shown.
///
/// - Returns: `true` if the completed todo tasks should be displayed, `false` otherwise.
var canShowCompleted: Bool {
group.canShowCompleted
}
/// The navigation title for the task list view.
///
/// This computed property provides the localized title for the task list view, suitable for display in a navigation bar.
///
/// - Returns: The localized title key for the task list view.
var navigationTitle: LocalizedStringKey {
group.title
}
// MARK: Methods
/// Deletes tasks at the specified indices.
///
/// This method removes tasks from the model context after a swipe-to-delete action.
/// The deletion is performed through SwiftData's model context, which handles
/// persistence and triggers automatic UI updates.
///
/// - Parameters:
/// - tasks: The current array of tasks.
/// - offsets: The index set of tasks to delete.
/// - modelContext: The SwiftData model context to perform the deletion.
func deleteTasks(
_ tasks: [Todo],
at offsets: IndexSet,
from modelContext: ModelContext
) {
for index in offsets {
modelContext.delete(tasks[index])
}
}
2026-02-11 15:28:22 +00:00
/// Reorders tasks after a drag and drop operation.
///
/// 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
/// that the user's manual ordering preference is persisted.
///
/// - Parameters:
/// - tasks: The current array of tasks before reordering.
/// - source: The index set of items being moved (typically a single index).
/// - destination: The destination index where items should be inserted.
/// - 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.
/// - SeeAlso: ``TodoGroup/canReorder`` for determining which groups support reordering.
func reorderTasks(
_ tasks: [Todo],
from source: IndexSet,
to destination: Int
) {
var updatedTasks = tasks
updatedTasks.move(
fromOffsets: source,
toOffset: destination
)
for (index, task) in updatedTasks.enumerated() {
task.order = index
}
}
2026-02-11 13:00:46 +00:00
/// Toggles the visibility of completed tasks.
///
/// This method flips the `showCompleted` state, which triggers SwiftUI to recreate the task list view and its children.
/// The toggle has no effect when `canShowCompleted` is `false`.
func toggleCompletedVisibility() {
showCompleted.toggle()
}
2026-02-10 15:31:52 +00:00
}
}