Manual task reordering feature (#6)
This PR contains the work done to add support for manual todo task reordering in the *Task list* view Reviewed-on: rock-n-code/todos-app#6 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 #6.
This commit is contained in:
@@ -15,17 +15,12 @@ extension TaskListView {
|
||||
/// - 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
|
||||
/// - Handles task reordering through drag and drop interactions
|
||||
@Observable
|
||||
final class ViewModel {
|
||||
|
||||
// MARK: Properties
|
||||
|
||||
/// The task group to display tasks for.
|
||||
///
|
||||
/// This property determines which tasks are fetched and displayed in the list.
|
||||
/// 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:
|
||||
@@ -33,6 +28,13 @@ extension TaskListView {
|
||||
/// - `false`: Completed tasks are filtered out at the database level
|
||||
var showCompleted: Bool
|
||||
|
||||
/// The task group to display tasks for.
|
||||
///
|
||||
/// This property determines which tasks are fetched and displayed in the list.
|
||||
/// 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
|
||||
|
||||
// MARK: Initialization
|
||||
|
||||
/// Initializes a new view model instance for a specific task group.
|
||||
@@ -40,7 +42,7 @@ extension TaskListView {
|
||||
/// - Parameter group: The task group to display tasks for.
|
||||
init(group: TodoGroup) {
|
||||
self.group = group
|
||||
showCompleted = false
|
||||
self.showCompleted = false
|
||||
}
|
||||
|
||||
// MARK: Computed Properties
|
||||
@@ -82,6 +84,36 @@ extension TaskListView {
|
||||
|
||||
// MARK: Methods
|
||||
|
||||
/// Reorders tasks after a drag and drop operation.
|
||||
///
|
||||
/// This method is called when users drag and drop tasks to reorder them within a group.
|
||||
/// It updates the `order` property of all tasks to reflect their new positions, ensuring
|
||||
/// that the user's manual ordering preference is persisted.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - tasks: The current array of tasks before reordering.
|
||||
/// - source: The index set of items being moved (typically a single index).
|
||||
/// - destination: The destination index where items should be inserted.
|
||||
/// - Note: This method should only be called for groups where `canReorder` is `true`.
|
||||
/// The view layer is responsible for only enabling drag and drop for applicable groups.
|
||||
/// - SeeAlso: ``TodoGroup/canReorder`` for determining which groups support reordering.
|
||||
func reorderTasks(
|
||||
_ tasks: [Todo],
|
||||
from source: IndexSet,
|
||||
to destination: Int
|
||||
) {
|
||||
var updatedTasks = tasks
|
||||
|
||||
updatedTasks.move(
|
||||
fromOffsets: source,
|
||||
toOffset: destination
|
||||
)
|
||||
|
||||
for (index, task) in updatedTasks.enumerated() {
|
||||
task.order = index
|
||||
}
|
||||
}
|
||||
|
||||
/// Toggles the visibility of completed tasks.
|
||||
///
|
||||
/// This method flips the `showCompleted` state, which triggers SwiftUI to recreate the task list view and its children.
|
||||
|
||||
Reference in New Issue
Block a user