@@ -101,18 +101,21 @@ final class DataChangeManager: ChangeManaging {
101101 return lo
102102 }
103103
104- private let undoManager : UndoManager = {
105- let manager = UndoManager ( )
106- manager. levelsOfUndo = 100
107- return manager
108- } ( )
104+ var undoManagerProvider : ( ( ) -> UndoManager ? ) ?
105+ var onUndoApplied : ( ( UndoResult ) -> Void ) ?
109106
110107 private var lastUndoResult : UndoResult ?
111108
112109 // MARK: - Undo/Redo Properties
113110
114- var canUndo : Bool { undoManager. canUndo }
115- var canRedo : Bool { undoManager. canRedo }
111+ var canUndo : Bool { undoManagerProvider ? ( ) ? . canUndo ?? false }
112+ var canRedo : Bool { undoManagerProvider ? ( ) ? . canRedo ?? false }
113+
114+ private func registerUndo( actionName: String , _ handler: @escaping ( DataChangeManager ) -> Void ) {
115+ guard let undoManager = undoManagerProvider ? ( ) else { return }
116+ undoManager. registerUndo ( withTarget: self , handler: handler)
117+ undoManager. setActionName ( actionName)
118+ }
116119
117120 // MARK: - Helper Methods
118121
@@ -138,7 +141,7 @@ final class DataChangeManager: ChangeManaging {
138141
139142 func clearChangesAndUndoHistory( ) {
140143 clearChanges ( )
141- undoManager . removeAllActions ( )
144+ undoManagerProvider ? ( ) ? . removeAllActions ( withTarget : self )
142145 }
143146
144147 func configureForTable(
@@ -159,7 +162,7 @@ final class DataChangeManager: ChangeManaging {
159162 modifiedCells. removeAll ( )
160163 insertedRowData. removeAll ( )
161164 changedRowIndices. removeAll ( )
162- undoManager . removeAllActions ( )
165+ undoManagerProvider ? ( ) ? . removeAllActions ( withTarget : self )
163166
164167 changes. removeAll ( )
165168 hasChanges = false
@@ -232,13 +235,12 @@ final class DataChangeManager: ChangeManaging {
232235 newValue: newValue
233236 ) )
234237 }
235- undoManager . registerUndo ( withTarget : self ) { target in
238+ registerUndo ( actionName : String ( localized : " Edit Cell " ) ) { target in
236239 target. applyDataUndo ( . cellEdit(
237240 rowIndex: rowIndex, columnIndex: columnIndex, columnName: columnName,
238241 previousValue: oldValue, newValue: newValue, originalRow: nil
239242 ) )
240243 }
241- undoManager. setActionName ( String ( localized: " Edit Cell " ) )
242244 changedRowIndices. insert ( rowIndex)
243245 hasChanges = !changes. isEmpty
244246 reloadVersion += 1
@@ -287,13 +289,12 @@ final class DataChangeManager: ChangeManaging {
287289 changedRowIndices. insert ( rowIndex)
288290 }
289291
290- undoManager . registerUndo ( withTarget : self ) { target in
292+ registerUndo ( actionName : String ( localized : " Edit Cell " ) ) { target in
291293 target. applyDataUndo ( . cellEdit(
292294 rowIndex: rowIndex, columnIndex: columnIndex, columnName: columnName,
293295 previousValue: oldValue, newValue: newValue, originalRow: originalRow
294296 ) )
295297 }
296- undoManager. setActionName ( String ( localized: " Edit Cell " ) )
297298 hasChanges = !changes. isEmpty
298299 reloadVersion += 1
299300 }
@@ -307,10 +308,9 @@ final class DataChangeManager: ChangeManaging {
307308 changeIndex [ RowChangeKey ( rowIndex: rowIndex, type: . delete) ] = changes. count - 1
308309 deletedRowIndices. insert ( rowIndex)
309310 changedRowIndices. insert ( rowIndex)
310- undoManager . registerUndo ( withTarget : self ) { target in
311+ registerUndo ( actionName : String ( localized : " Delete Row " ) ) { target in
311312 target. applyDataUndo ( . rowDeletion( rowIndex: rowIndex, originalRow: originalRow) )
312313 }
313- undoManager. setActionName ( String ( localized: " Delete Row " ) )
314314 hasChanges = true
315315 reloadVersion += 1
316316 }
@@ -336,10 +336,9 @@ final class DataChangeManager: ChangeManaging {
336336 changedRowIndices. insert ( rowIndex)
337337 batchData. append ( ( rowIndex: rowIndex, originalRow: originalRow) )
338338 }
339- undoManager . registerUndo ( withTarget : self ) { target in
339+ registerUndo ( actionName : String ( localized : " Delete Rows " ) ) { target in
340340 target. applyDataUndo ( . batchRowDeletion( rows: batchData) )
341341 }
342- undoManager. setActionName ( String ( localized: " Delete Rows " ) )
343342 hasChanges = true
344343 reloadVersion += 1
345344 }
@@ -351,10 +350,9 @@ final class DataChangeManager: ChangeManaging {
351350 changeIndex [ RowChangeKey ( rowIndex: rowIndex, type: . insert) ] = changes. count - 1
352351 insertedRowIndices. insert ( rowIndex)
353352 changedRowIndices. insert ( rowIndex)
354- undoManager . registerUndo ( withTarget : self ) { target in
353+ registerUndo ( actionName : String ( localized : " Insert Row " ) ) { target in
355354 target. applyDataUndo ( . rowInsertion( rowIndex: rowIndex) )
356355 }
357- undoManager. setActionName ( String ( localized: " Insert Row " ) )
358356 hasChanges = true
359357 reloadVersion += 1
360358 }
@@ -476,10 +474,9 @@ final class DataChangeManager: ChangeManaging {
476474 insertedRowData. removeValue ( forKey: rowIndex)
477475 }
478476
479- undoManager . registerUndo ( withTarget : self ) { target in
477+ registerUndo ( actionName : String ( localized : " Insert Rows " ) ) { target in
480478 target. applyDataUndo ( . batchRowInsertion( rowIndices: validRows, rowValues: rowValues) )
481479 }
482- undoManager. setActionName ( String ( localized: " Insert Rows " ) )
483480
484481 let sortedDeleted = validRows. sorted ( )
485482
@@ -506,13 +503,12 @@ final class DataChangeManager: ChangeManaging {
506503 private func applyDataUndo( _ action: UndoAction ) {
507504 switch action {
508505 case . cellEdit( let rowIndex, let columnIndex, let columnName, let previousValue, let newValue, let originalRow) :
509- undoManager . registerUndo ( withTarget : self ) { target in
506+ registerUndo ( actionName : String ( localized : " Edit Cell " ) ) { target in
510507 target. applyDataUndo ( . cellEdit(
511508 rowIndex: rowIndex, columnIndex: columnIndex, columnName: columnName,
512509 previousValue: newValue, newValue: previousValue, originalRow: originalRow
513510 ) )
514511 }
515- undoManager. setActionName ( String ( localized: " Edit Cell " ) )
516512
517513 let matchedIndex = changeIndex [ RowChangeKey ( rowIndex: rowIndex, type: . update) ]
518514 ?? changeIndex [ RowChangeKey ( rowIndex: rowIndex, type: . insert) ]
@@ -567,13 +563,12 @@ final class DataChangeManager: ChangeManaging {
567563
568564 case . rowInsertion( let rowIndex) :
569565 let savedValues = insertedRowData [ rowIndex]
570- undoManager . registerUndo ( withTarget : self ) { [ savedValues] target in
566+ registerUndo ( actionName : String ( localized : " Insert Row " ) ) { [ savedValues] target in
571567 if let savedValues {
572568 target. insertedRowData [ rowIndex] = savedValues
573569 }
574570 target. applyDataUndo ( . rowInsertion( rowIndex: rowIndex) )
575571 }
576- undoManager. setActionName ( String ( localized: " Insert Row " ) )
577572
578573 if insertedRowIndices. contains ( rowIndex) {
579574 undoRowInsertion ( rowIndex: rowIndex)
@@ -606,10 +601,9 @@ final class DataChangeManager: ChangeManaging {
606601 }
607602
608603 case . rowDeletion( let rowIndex, let originalRow) :
609- undoManager . registerUndo ( withTarget : self ) { target in
604+ registerUndo ( actionName : String ( localized : " Delete Row " ) ) { target in
610605 target. applyDataUndo ( . rowDeletion( rowIndex: rowIndex, originalRow: originalRow) )
611606 }
612- undoManager. setActionName ( String ( localized: " Delete Row " ) )
613607
614608 if deletedRowIndices. contains ( rowIndex) {
615609 undoRowDeletion ( rowIndex: rowIndex)
@@ -626,10 +620,9 @@ final class DataChangeManager: ChangeManaging {
626620 }
627621
628622 case . batchRowDeletion( let rows) :
629- undoManager . registerUndo ( withTarget : self ) { target in
623+ registerUndo ( actionName : String ( localized : " Delete Rows " ) ) { target in
630624 target. applyDataUndo ( . batchRowDeletion( rows: rows) )
631625 }
632- undoManager. setActionName ( String ( localized: " Delete Rows " ) )
633626
634627 let isUndo = rows. contains { deletedRowIndices. contains ( $0. rowIndex) }
635628 if isUndo {
@@ -651,10 +644,9 @@ final class DataChangeManager: ChangeManaging {
651644 }
652645
653646 case . batchRowInsertion( let rowIndices, let rowValues) :
654- undoManager . registerUndo ( withTarget : self ) { target in
647+ registerUndo ( actionName : String ( localized : " Insert Rows " ) ) { target in
655648 target. applyDataUndo ( . batchRowInsertion( rowIndices: rowIndices, rowValues: rowValues) )
656649 }
657- undoManager. setActionName ( String ( localized: " Insert Rows " ) )
658650
659651 let firstInserted = rowIndices. first. map { insertedRowIndices. contains ( $0) } ?? false
660652 if firstInserted {
@@ -701,9 +693,12 @@ final class DataChangeManager: ChangeManaging {
701693
702694 hasChanges = !changes. isEmpty
703695 reloadVersion += 1
696+
697+ if let result = lastUndoResult {
698+ onUndoApplied ? ( result)
699+ }
704700 }
705701
706- /// Re-apply a cell edit during redo without registering a duplicate undo
707702 private func recordCellChangeForRedo(
708703 rowIndex: Int ,
709704 columnIndex: Int ,
@@ -784,16 +779,16 @@ final class DataChangeManager: ChangeManaging {
784779 // MARK: - Undo/Redo Public API
785780
786781 func undoLastChange( ) -> UndoResult ? {
787- guard undoManager . canUndo else { return nil }
782+ guard let um = undoManagerProvider ? ( ) , um . canUndo else { return nil }
788783 lastUndoResult = nil
789- undoManager . undo ( )
784+ um . undo ( )
790785 return lastUndoResult
791786 }
792787
793788 func redoLastChange( ) -> UndoResult ? {
794- guard undoManager . canRedo else { return nil }
789+ guard let um = undoManagerProvider ? ( ) , um . canRedo else { return nil }
795790 lastUndoResult = nil
796- undoManager . redo ( )
791+ um . redo ( )
797792 return lastUndoResult
798793 }
799794
0 commit comments