@@ -17,6 +17,21 @@ struct UndoResult {
1717 let needsRowRemoval : Bool
1818 let needsRowRestore : Bool
1919 let restoreRow : [ String ? ] ?
20+ let delta : Delta
21+
22+ init (
23+ action: UndoAction ,
24+ needsRowRemoval: Bool ,
25+ needsRowRestore: Bool ,
26+ restoreRow: [ String ? ] ? ,
27+ delta: Delta = . none
28+ ) {
29+ self . action = action
30+ self . needsRowRemoval = needsRowRemoval
31+ self . needsRowRestore = needsRowRestore
32+ self . restoreRow = restoreRow
33+ self . delta = delta
34+ }
2035}
2136
2237/// Manager for tracking and applying data changes
@@ -222,7 +237,6 @@ final class DataChangeManager: ChangeManaging {
222237 }
223238
224239 hasChanges = !pending. isEmpty
225- reloadVersion += 1
226240
227241 if let result = lastUndoResult {
228242 onUndoApplied ? ( result)
@@ -259,7 +273,10 @@ final class DataChangeManager: ChangeManaging {
259273 originalDBValue: newValue, newValue: previousValue, originalRow: originalRow
260274 )
261275 }
262- lastUndoResult = UndoResult ( action: action, needsRowRemoval: false , needsRowRestore: false , restoreRow: nil )
276+ lastUndoResult = UndoResult (
277+ action: action, needsRowRemoval: false , needsRowRestore: false , restoreRow: nil ,
278+ delta: . cellChanged( row: rowIndex, column: columnIndex)
279+ )
263280 }
264281
265282 private func applyRowInsertionUndo( rowIndex: Int , action: UndoAction ) {
@@ -274,12 +291,14 @@ final class DataChangeManager: ChangeManaging {
274291 if pending. isRowInserted ( rowIndex) {
275292 _ = pending. undoRowInsertion ( rowIndex: rowIndex)
276293 lastUndoResult = UndoResult (
277- action: action, needsRowRemoval: true , needsRowRestore: false , restoreRow: nil
294+ action: action, needsRowRemoval: true , needsRowRestore: false , restoreRow: nil ,
295+ delta: . rowsRemoved( IndexSet ( integer: rowIndex) )
278296 )
279297 } else {
280298 pending. reinsertRow ( rowIndex: rowIndex, columns: columns, savedValues: savedValues)
281299 lastUndoResult = UndoResult (
282- action: action, needsRowRemoval: false , needsRowRestore: true , restoreRow: savedValues
300+ action: action, needsRowRemoval: false , needsRowRestore: true , restoreRow: savedValues,
301+ delta: . rowsInserted( IndexSet ( integer: rowIndex) )
283302 )
284303 }
285304 }
@@ -292,12 +311,14 @@ final class DataChangeManager: ChangeManaging {
292311 if pending. isRowDeleted ( rowIndex) {
293312 _ = pending. undoRowDeletion ( rowIndex: rowIndex)
294313 lastUndoResult = UndoResult (
295- action: action, needsRowRemoval: false , needsRowRestore: true , restoreRow: originalRow
314+ action: action, needsRowRemoval: false , needsRowRestore: true , restoreRow: originalRow,
315+ delta: . fullReplace
296316 )
297317 } else {
298318 pending. reapplyRowDeletion ( rowIndex: rowIndex, originalRow: originalRow)
299319 lastUndoResult = UndoResult (
300- action: action, needsRowRemoval: true , needsRowRestore: false , restoreRow: nil
320+ action: action, needsRowRemoval: true , needsRowRestore: false , restoreRow: nil ,
321+ delta: . fullReplace
301322 )
302323 }
303324 }
@@ -315,14 +336,16 @@ final class DataChangeManager: ChangeManaging {
315336 _ = pending. undoRowDeletion ( rowIndex: rowIndex)
316337 }
317338 lastUndoResult = UndoResult (
318- action: action, needsRowRemoval: false , needsRowRestore: true , restoreRow: nil
339+ action: action, needsRowRemoval: false , needsRowRestore: true , restoreRow: nil ,
340+ delta: . fullReplace
319341 )
320342 } else {
321343 for (rowIndex, originalRow) in rows {
322344 pending. reapplyRowDeletion ( rowIndex: rowIndex, originalRow: originalRow)
323345 }
324346 lastUndoResult = UndoResult (
325- action: action, needsRowRemoval: true , needsRowRestore: false , restoreRow: nil
347+ action: action, needsRowRemoval: true , needsRowRestore: false , restoreRow: nil ,
348+ delta: . fullReplace
326349 )
327350 }
328351 }
@@ -335,15 +358,18 @@ final class DataChangeManager: ChangeManaging {
335358 }
336359
337360 let firstInserted = rowIndices. first. map { pending. isRowInserted ( $0) } ?? false
361+ let indices = IndexSet ( rowIndices)
338362 if firstInserted {
339363 _ = pending. undoBatchRowInsertion ( rowIndices: rowIndices, columnCount: columns. count)
340364 lastUndoResult = UndoResult (
341- action: action, needsRowRemoval: true , needsRowRestore: false , restoreRow: nil
365+ action: action, needsRowRemoval: true , needsRowRestore: false , restoreRow: nil ,
366+ delta: . rowsRemoved( indices)
342367 )
343368 } else {
344369 pending. reinsertBatch ( rowIndices: rowIndices, rowValues: rowValues, columns: columns)
345370 lastUndoResult = UndoResult (
346- action: action, needsRowRemoval: false , needsRowRestore: true , restoreRow: nil
371+ action: action, needsRowRemoval: false , needsRowRestore: true , restoreRow: nil ,
372+ delta: . rowsInserted( indices)
347373 )
348374 }
349375 }
0 commit comments