File tree Expand file tree Collapse file tree 1 file changed +15
-2
lines changed
Expand file tree Collapse file tree 1 file changed +15
-2
lines changed Original file line number Diff line number Diff line change @@ -118,18 +118,31 @@ final class AppState {
118118 guard let listId = selectedListId else { return }
119119 var updated = task
120120 updated. isCompleted. toggle ( )
121+
122+ // Optimistic update: immediately reflect in UI
123+ if let index = tasks. firstIndex ( where: { $0. id == task. id } ) {
124+ tasks [ index] = updated
125+ }
126+
121127 do {
122128 let result = try await api. updateTask ( listId: listId, taskId: task. id, task: updated)
123- if let index = tasks. firstIndex ( where: { $0. id == task. id } ) {
129+ // Update with server response
130+ if let index = tasks. firstIndex ( where: { $0. id == result. id } ) {
124131 tasks [ index] = result
125132 }
126133 // Update completed tasks cache
127134 if result. isCompleted {
128- completedTasksCache. append ( result)
135+ if !completedTasksCache. contains ( where: { $0. id == result. id } ) {
136+ completedTasksCache. append ( result)
137+ }
129138 } else {
130139 completedTasksCache. removeAll { $0. id == result. id }
131140 }
132141 } catch {
142+ // Revert optimistic update on failure
143+ if let index = tasks. firstIndex ( where: { $0. id == task. id } ) {
144+ tasks [ index] = task
145+ }
133146 handleError ( error)
134147 }
135148 }
You can’t perform that action at this time.
0 commit comments