Files
todos/App/Sources/View Models/TaskListViewModel.swift
T
javier cbf33302dd Show completed task feature (#5)
This PR contains the work done to add support for showing or hiding completed todo tasks to the *Task list* view.

Reviewed-on: rock-n-code/todos-app#5
Co-authored-by: Javier Cicchelli <javier@rock-n-code.com>
Co-committed-by: Javier Cicchelli <javier@rock-n-code.com>
2026-02-11 13:00:46 +00:00

95 lines
3.6 KiB
Swift

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
@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.
/// 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
// 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
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
/// 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()
}
}
}