From 8acc1333a37f4608143d5415e10e20c2dec00340 Mon Sep 17 00:00:00 2001 From: GlenCFL Date: Wed, 5 Dec 2018 06:51:10 -0500 Subject: [PATCH] Add undo/redo support for the DisableSound commands. --- .../AddBlockItemToBlocksCommand.cs | 42 ++++++++++++++++ .../RemoveBlockItemFromBlocksCommand.cs | 48 +++++++++++++++++++ .../Filtration.ObjectModel.csproj | 2 + .../ViewModels/ItemFilterScriptViewModel.cs | 28 ++++++----- 4 files changed, 107 insertions(+), 13 deletions(-) create mode 100644 Filtration.ObjectModel/Commands/ItemFilterScript/AddBlockItemToBlocksCommand.cs create mode 100644 Filtration.ObjectModel/Commands/ItemFilterScript/RemoveBlockItemFromBlocksCommand.cs diff --git a/Filtration.ObjectModel/Commands/ItemFilterScript/AddBlockItemToBlocksCommand.cs b/Filtration.ObjectModel/Commands/ItemFilterScript/AddBlockItemToBlocksCommand.cs new file mode 100644 index 0000000..1deb97e --- /dev/null +++ b/Filtration.ObjectModel/Commands/ItemFilterScript/AddBlockItemToBlocksCommand.cs @@ -0,0 +1,42 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Collections.ObjectModel; + +namespace Filtration.ObjectModel.Commands.ItemFilterScript +{ + public class AddBlockItemToBlocksCommand : IUndoableCommand + { + private readonly List, IItemFilterBlockItem>> _input; + + public AddBlockItemToBlocksCommand(List, IItemFilterBlockItem>> input) + { + _input = input; + } + + public void Execute() + { + foreach (var v in _input) + { + var blockItems = v.Item1; + var item = v.Item2; + + blockItems.Add(item); + } + } + + public void Undo() + { + foreach (var v in _input) + { + var blockItems = v.Item1; + var item = v.Item2; + blockItems.Remove(item); + } + } + + public void Redo() => Execute(); + } +} diff --git a/Filtration.ObjectModel/Commands/ItemFilterScript/RemoveBlockItemFromBlocksCommand.cs b/Filtration.ObjectModel/Commands/ItemFilterScript/RemoveBlockItemFromBlocksCommand.cs new file mode 100644 index 0000000..e48355a --- /dev/null +++ b/Filtration.ObjectModel/Commands/ItemFilterScript/RemoveBlockItemFromBlocksCommand.cs @@ -0,0 +1,48 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Collections.ObjectModel; + +namespace Filtration.ObjectModel.Commands.ItemFilterScript +{ + public class RemoveBlockItemFromBlocksCommand : IUndoableCommand + { + private readonly List, IItemFilterBlockItem>> _input; + + public RemoveBlockItemFromBlocksCommand(List, IItemFilterBlockItem>> input) + { + _input = input; + } + + public void Execute() + { + foreach (var pair in _input) + { + var blockItems = pair.Item1; + var blockItem = pair.Item2; + + for (var i = 0; i < blockItems.Count; i++) + { + if (blockItems[i] == blockItem) + { + blockItems.RemoveAt(i--); + } + } + } + } + + public void Undo() + { + foreach (var pair in _input) + { + var blockItems = pair.Item1; + var blockItem = pair.Item2; + blockItems.Add(blockItem); + } + } + + public void Redo() => Execute(); + } +} diff --git a/Filtration.ObjectModel/Filtration.ObjectModel.csproj b/Filtration.ObjectModel/Filtration.ObjectModel.csproj index cec09a1..06fe4d0 100644 --- a/Filtration.ObjectModel/Filtration.ObjectModel.csproj +++ b/Filtration.ObjectModel/Filtration.ObjectModel.csproj @@ -114,11 +114,13 @@ + + diff --git a/Filtration/ViewModels/ItemFilterScriptViewModel.cs b/Filtration/ViewModels/ItemFilterScriptViewModel.cs index 71481f4..14d4cae 100644 --- a/Filtration/ViewModels/ItemFilterScriptViewModel.cs +++ b/Filtration/ViewModels/ItemFilterScriptViewModel.cs @@ -1294,24 +1294,25 @@ namespace Filtration.ViewModels { ValidateSelectedBlocks(); - var modified = false; + var input = new List, IItemFilterBlockItem>>(); foreach (var block in SelectedBlockViewModels.OfType()) { - var blockItems= block.Block.BlockItems; + var blockItems = block.Block.BlockItems; for (var i = 0; i < blockItems.Count; i++) { - if (blockItems[i] is DisableDropSoundBlockItem) + var blockItem = blockItems[i]; + if (blockItem is DisableDropSoundBlockItem) { - blockItems.RemoveAt(i--); - modified = true; + input.Add(new Tuple, IItemFilterBlockItem>(blockItems, blockItem)); } } } - if (modified) - { - SetDirtyFlag(); + if (input.Count > 0) + { + _scriptCommandManager.ExecuteCommand(new RemoveBlockItemFromBlocksCommand(input)); + SetDirtyFlag(); } } @@ -1319,7 +1320,7 @@ namespace Filtration.ViewModels { ValidateSelectedBlocks(); - var modified = false; + var input = new List, IItemFilterBlockItem>>(); foreach (var block in SelectedBlockViewModels.OfType()) { @@ -1335,13 +1336,14 @@ namespace Filtration.ViewModels if (!found) { var item = new DisableDropSoundBlockItem(true); - blockItems.Add(item); - modified = true; + input.Add(new Tuple, IItemFilterBlockItem>(blockItems, item)); } } - if (modified) { - SetDirtyFlag(); + if (input.Count > 0) + { + _scriptCommandManager.ExecuteCommand(new AddBlockItemToBlocksCommand(input)); + SetDirtyFlag(); } }