Implemented the TaskListView view and its respective view model in. the app target.

This commit is contained in:
2026-02-11 13:56:44 +01:00
parent a8f3ac28ed
commit 2e1c4274ba
4 changed files with 219 additions and 43 deletions
+62 -11
View File
@@ -3,13 +3,18 @@ import SwiftUI
extension TaskListView {
/// View model for managing the logic for the task list view.
/// View model for managing the state and business logic of the task list view.
///
/// This observable class encapsulates the state and business logic for the task list view.
/// It follows the MVVM pattern to separate presentation logic from the view layer.
/// 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
/// - Providing the navigation title for the current group
///
/// - 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 {
@@ -18,7 +23,15 @@ extension TaskListView {
/// The task group to display tasks for.
///
/// This property determines which tasks are fetched and displayed in the list.
private let group: TodoGroup
/// 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
@@ -27,17 +40,55 @@ extension TaskListView {
/// - Parameter group: The task group to display tasks for.
init(group: TodoGroup) {
self.group = group
showCompleted = false
}
// MARK: Computed
// MARK: Computed Properties
/// The navigation title for the current task group.
/// The localized button title and icon for the show/hide completed todo tasks.
///
/// This computed property provides the localized title for the current group,
/// suitable for display in a navigation bar.
/// This computed property provides the appropriate label and icon based on
/// the current `showCompleted` state.
///
/// - Returns: The localized title key for the current group.
var navigationTitle: LocalizedStringKey { group.title }
/// - 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()
}
}
}