Implemented the "deleteTasks(_: at: from: )" function for the LastListViewModel view model in the app target.

This commit is contained in:
2026-02-11 17:33:22 +01:00
parent ac0e0f6556
commit 8324a64b24
2 changed files with 60 additions and 0 deletions
@@ -1,3 +1,4 @@
import Foundation
import SwiftData import SwiftData
import SwiftUI import SwiftUI
@@ -84,6 +85,26 @@ extension TaskListView {
// MARK: Methods // MARK: Methods
/// Deletes tasks at the specified indices.
///
/// This method removes tasks from the model context after a swipe-to-delete action.
/// The deletion is performed through SwiftData's model context, which handles
/// persistence and triggers automatic UI updates.
///
/// - Parameters:
/// - tasks: The current array of tasks.
/// - offsets: The index set of tasks to delete.
/// - modelContext: The SwiftData model context to perform the deletion.
func deleteTasks(
_ tasks: [Todo],
at offsets: IndexSet,
from modelContext: ModelContext
) {
for index in offsets {
modelContext.delete(tasks[index])
}
}
/// Reorders tasks after a drag and drop operation. /// Reorders tasks after a drag and drop operation.
/// ///
/// This method is called when users drag and drop tasks to reorder them within a group. /// This method is called when users drag and drop tasks to reorder them within a group.
@@ -91,6 +91,45 @@ struct TaskListViewModelTests {
// MARK: Method tests // MARK: Method tests
@Test("deleteTasks removes a single task from model context")
func deleteTasksSingle() throws {
// GIVEN
let container = try ModelContainer(
for: Todo.self,
configurations: .init(isStoredInMemoryOnly: true)
)
let context = ModelContext(container)
let viewModel = TaskListView.ViewModel(group: .today)
let todosToInsert: [Todo] = [
.init(title: "Task 1", due: .now, order: 0),
.init(title: "Task 2", due: .now, order: 1),
.init(title: "Task 3", due: .now, order: 2),
]
let countInitial = todosToInsert.count
for todo in todosToInsert {
context.insert(todo)
}
// WHEN
viewModel.deleteTasks(
todosToInsert,
at: IndexSet(integer: 1),
from: context
)
// THEN
let descriptor = FetchDescriptor<Todo>()
let todos = try context.fetch(descriptor)
#expect(countInitial == 3)
#expect(todos.count == 2)
#expect(todos.contains(where: { $0.title == "Task 1" }))
#expect(todos.contains(where: { $0.title == "Task 3" }))
#expect(!todos.contains(where: { $0.title == "Task 2" }))
}
@Test("reorderTasks updates order property when moving task forward") @Test("reorderTasks updates order property when moving task forward")
func reorderTasksMovingForward() { func reorderTasksMovingForward() {
// GIVEN // GIVEN