This PR contains the work done to implement the **List Tasks** feature, and to integrate it into the main content view of the app. Reviewed-on: rock-n-code/todos-app#4 Co-authored-by: Javier Cicchelli <javier@rock-n-code.com> Co-committed-by: Javier Cicchelli <javier@rock-n-code.com>
100 lines
2.2 KiB
Swift
100 lines
2.2 KiB
Swift
import SwiftData
|
|
import SwiftUI
|
|
|
|
/// A view that displays all 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.
|
|
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.
|
|
@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.
|
|
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.
|
|
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)
|
|
}
|
|
}
|
|
.listStyle(.grouped)
|
|
}
|
|
}
|
|
.navigationTitle(viewModel.navigationTitle)
|
|
}
|
|
}
|
|
|
|
// 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)
|
|
}
|
|
}
|