import Foundation import SwiftData import SwiftUI extension TaskListView { /// View model for managing the state and business logic of the task list view. /// /// 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. /// /// ## Responsibilities /// /// - 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 /// - Handles task reordering through drag and drop interactions @Observable final class ViewModel { // MARK: Properties /// 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 /// 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 // 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 self.showCompleted = false } // MARK: Computed Properties /// The localized button title and icon for the show/hide completed todo tasks. /// /// This computed property provides the appropriate label and icon based on /// the current `showCompleted` state. /// /// - 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]) } } /// 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 } } /// 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() } } }