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>
This commit was merged in pull request #5.
This commit is contained in:
2026-02-11 13:00:46 +00:00
committed by javier
parent 2a3323b8e6
commit cbf33302dd
9 changed files with 457 additions and 63 deletions
+34 -28
View File
@@ -1,54 +1,60 @@
import SwiftData
import SwiftUI
/// A view that displays all todo tasks for a specific task group.
/// A view that displays filtered todo tasks for a specific task group.
///
/// This view shows a filtered list of tasks based on the selected group (today, upcoming, past, or completed).
/// The view uses a view model to separate business logic from presentation.
/// This view provides the presentation layer for displaying and managing tasks within a specific group category.
/// It follows the MVVM pattern, delegating business logic to a view model and data fetching to a separate content view.
struct TaskListView: View {
// MARK: Properties
/// Query to fetch todos filtered by the current group.
@Query
private var tasks: [Todo]
/// The view model managing the business logic for this view.
/// The view model managing the state and business logic for this view.
///
/// This view model holds the group configuration and completion filter state.
@State
private var viewModel: ViewModel
// MARK: Initialization
/// Creates a new task list view for a specific group.
/// - Parameter group: The todo group to display tasks for.
///
/// - Parameter group: The todo group category to display tasks for.
init(group: TodoGroup) {
_tasks = .init(FetchDescriptor<Todo>(group: group))
_viewModel = .init(initialValue: ViewModel(group: group))
}
// MARK: Body
/// The content and behavior of the view.
///
/// Displays a list of tasks filtered by the current group.
/// Displays the task list content with navigation title and toolbar controls.
/// The toolbar includes a completion filter toggle for groups that support it.
var body: some View {
Group {
if tasks.isEmpty {
ContentUnavailableView(
"view.task-list.content-unavailable.title",
systemImage: "checkmark.circle"
)
} else {
List {
ForEach(tasks) { todo in
Item(todo)
Content(
group: viewModel.group,
showCompleted: viewModel.showCompleted
)
.navigationTitle(viewModel.navigationTitle)
.toolbar {
if viewModel.canShowCompleted {
Menu {
Button.init(
viewModel.buttonCompleted.title,
systemImage: viewModel.buttonCompleted.icon
) {
viewModel.toggleCompletedVisibility()
}
} label: {
Label(
"view.task-list.label.actions",
systemImage: "ellipsis"
)
}
.listStyle(.grouped)
}
}
.navigationTitle(viewModel.navigationTitle)
}
}
// MARK: - Previews