/// <summary> /// Iterate over all commands and delete those no longer valid. /// Call it whenever data set was modified externally without undo/redo tracking. /// </summary> public void Refresh() { // edit command relies on add and remove so we have to first iterate over these for (int i = 0; i < _undoRedoStack.Count; i++) { IUndoRedoCommand cmd = _undoRedoStack[i]; if (cmd is AddCommand <IModel> || cmd is RemoveCommand <IModel> ) { if (!cmd.CheckExecutionContext()) { _undoRedoStack.RemoveAt(i--); _pointer--; } } } for (int i = 0; i < _undoRedoStack.Count; i++) { IUndoRedoCommand cmd = _undoRedoStack[i]; if (cmd is EditCommand <IModel> ) { if (!cmd.CheckExecutionContext()) { _undoRedoStack.RemoveAt(i--); _pointer--; } } } }
public void Undo() { IUndoRedoCommand cmd = _undoRedoStack[--_pointer]; if (cmd.CheckExecutionContext()) // if command is no longer valid dispose it { cmd.Undo(); } else { _undoRedoStack.RemoveAt(_pointer); } _onUndoAction?.Invoke(); _onRedoAction?.Invoke(); }