Group tasks feature (#3)
This PR contains the work done to implement the **Group Task** feature, and to integrate it into the main content view of the app. Furthermore, an issue related to triggering a view update after inserting the todo tasks into the model container has been fixed. Reviewed-on: rock-n-code/todos-app#3 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 #3.
This commit is contained in:
@@ -0,0 +1,77 @@
|
||||
import SwiftData
|
||||
import SwiftUI
|
||||
|
||||
/// A view that displays todo tasks grouped by date and completion categories.
|
||||
///
|
||||
/// This view serves as the main interface for browsing tasks organized into four groups:
|
||||
/// - **Today**: Tasks due today that are not yet completed
|
||||
/// - **Upcoming**: Tasks due in the future that are not yet completed
|
||||
/// - **Overdue**: Tasks due in the past that are not yet completed
|
||||
/// - **Completed**: Tasks that have been marked as done
|
||||
struct TaskGroupListView: View {
|
||||
|
||||
// MARK: Properties
|
||||
|
||||
/// Query to monitor all todo tasks for changes.
|
||||
///
|
||||
/// This query triggers view updates whenever todo tasks are inserted, updated, or deleted,
|
||||
/// ensuring the task counts displayed in the view remain accurate.
|
||||
@Query
|
||||
private var tasks: [Todo]
|
||||
|
||||
/// The view model managing the state and logic for this view.
|
||||
///
|
||||
/// Handles task counting and provides access to all task groups.
|
||||
/// Initialized with the default calendar for date calculations.
|
||||
@State
|
||||
private var viewModel = ViewModel()
|
||||
|
||||
// MARK: Body
|
||||
|
||||
/// The content and behavior of the view.
|
||||
///
|
||||
/// Displays a scrollable vertical list of task group cards. Each card shows:
|
||||
/// - The group's icon in its associated color
|
||||
/// - The group's title
|
||||
/// - The current count of tasks in that group
|
||||
///
|
||||
/// Cards are tappable and navigate to a detailed task list for the selected group.
|
||||
/// Task counts are fetched on-demand from the model context for each render.
|
||||
var body: some View {
|
||||
ScrollView {
|
||||
VStack(spacing: 16) {
|
||||
ForEach(viewModel.groups) { group in
|
||||
NavigationLink(destination: TaskListView(group: group)) {
|
||||
Card(
|
||||
group: group,
|
||||
count: viewModel.count(tasks, for: group)
|
||||
)
|
||||
}
|
||||
.buttonStyle(.plain)
|
||||
}
|
||||
}
|
||||
.padding(.horizontal)
|
||||
}
|
||||
.navigationTitle("view.task-group-list.navigation.title")
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Previews
|
||||
|
||||
#Preview(
|
||||
"Task group list with empty tasks",
|
||||
traits: .emptyContainer
|
||||
) {
|
||||
NavigationStack {
|
||||
TaskGroupListView()
|
||||
}
|
||||
}
|
||||
|
||||
#Preview(
|
||||
"Task group list with sample tasks",
|
||||
traits: .sampleTasks
|
||||
) {
|
||||
NavigationStack {
|
||||
TaskGroupListView()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user