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

95 lines
3.6 KiB
Swift
Raw Normal View History

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-10 15:31:52 +00:00
@Observable
final class ViewModel {
// MARK: Properties
/// The task group to display tasks for.
///
/// This property determines which tasks are fetched and displayed in the list.
2026-02-11 13:00:46 +00:00
/// 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
/// 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
// 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 13:00:46 +00:00
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
/// 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
}
}