Files
todos/App/Sources/Views/TaskListView.swift
T

127 lines
3.3 KiB
Swift
Raw Normal View History

2026-02-10 15:31:52 +00:00
import SwiftData
import SwiftUI
2026-02-11 13:00:46 +00:00
/// A view that displays filtered todo tasks for a specific task group.
2026-02-10 15:31:52 +00:00
///
2026-02-11 13:00:46 +00:00
/// This view provides the presentation layer for displaying and managing tasks within a specific group category.
2026-02-10 15:31:52 +00:00
struct TaskListView: View {
2026-02-11 13:00:46 +00:00
2026-02-10 15:31:52 +00:00
// MARK: Properties
2026-02-11 13:00:46 +00:00
2026-02-11 16:35:51 +00:00
/// The SwiftData model context for performing data operations.
@Environment(\.modelContext)
private var modelContext
2026-02-11 13:00:46 +00:00
/// The view model managing the state and business logic for this view.
///
/// This view model holds the group configuration and completion filter state.
2026-02-10 15:31:52 +00:00
@State
private var viewModel: ViewModel
// MARK: Initialization
2026-02-11 13:00:46 +00:00
2026-02-10 15:31:52 +00:00
/// Creates a new task list view for a specific group.
2026-02-11 13:00:46 +00:00
///
2026-02-11 15:28:22 +00:00
/// Initializes the view with a view model configured for the specified group.
/// The view model manages the state for completion filtering and task reordering,
/// while the view handles the presentation layer including navigation and toolbar.
///
2026-02-11 13:00:46 +00:00
/// - Parameter group: The todo group category to display tasks for.
2026-02-11 15:28:22 +00:00
/// - Note: The group determines which tasks are fetched, whether
/// completion filtering is available, and whether drag and drop reordering is enabled.
2026-02-10 15:31:52 +00:00
init(group: TodoGroup) {
_viewModel = .init(initialValue: ViewModel(group: group))
}
2026-02-11 13:00:46 +00:00
2026-02-10 15:31:52 +00:00
// MARK: Body
2026-02-11 13:00:46 +00:00
2026-02-10 15:31:52 +00:00
/// The content and behavior of the view.
///
2026-02-11 13:00:46 +00:00
/// Displays the task list content with navigation title and toolbar controls.
/// The toolbar includes a completion filter toggle for groups that support it.
2026-02-10 15:31:52 +00:00
var body: some View {
2026-02-11 13:00:46 +00:00
Content(
group: viewModel.group,
showCompleted: viewModel.showCompleted
2026-02-11 16:35:51 +00:00
) { tasks, offsets in
2026-02-11 18:22:26 +00:00
viewModel.delete(
2026-02-11 16:35:51 +00:00
tasks,
at: offsets,
from: modelContext
)
} onReorder: { tasks, source, destination in
viewModel.reorderTasks(
tasks,
from: source,
to: destination
)
2026-02-11 15:28:22 +00:00
}
2026-02-11 13:00:46 +00:00
.navigationTitle(viewModel.navigationTitle)
.toolbar {
if viewModel.canShowCompleted {
Menu {
Button.init(
viewModel.buttonCompleted.title,
systemImage: viewModel.buttonCompleted.icon
) {
viewModel.toggleCompletedVisibility()
2026-02-10 15:31:52 +00:00
}
2026-02-11 13:00:46 +00:00
} label: {
Label(
"view.task-list.label.actions",
systemImage: "ellipsis"
)
2026-02-10 15:31:52 +00:00
}
}
}
}
2026-02-11 13:00:46 +00:00
2026-02-10 15:31:52 +00:00
}
// MARK: - Previews
#Preview(
"Task list with empty tasks",
traits: .emptyContainer
) {
NavigationStack {
TaskListView(group: .today)
}
}
#Preview(
"Task list - today",
traits: .sampleTasks
) {
NavigationStack {
TaskListView(group: .today)
}
}
#Preview(
"Task list - upcoming",
traits: .sampleTasks
) {
NavigationStack {
TaskListView(group: .upcoming)
}
}
#Preview(
"Task list - overdue",
traits: .sampleTasks
) {
NavigationStack {
TaskListView(group: .overdue)
}
}
#Preview(
"Task list - completed",
traits: .sampleTasks
) {
NavigationStack {
TaskListView(group: .completed)
}
}