public ConditionsDefinitionEditorViewModel(Func <IHistoryManager> historyCreator, IConditionDataProvider conditionDataProvider, IWindowManager windowManager, ITaskRunner taskRunner, IParameterFactory parameterFactory) { this.conditionDataProvider = conditionDataProvider; SourceItems = new ObservableCollection <ConditionJsonData>(conditionDataProvider.GetConditions().ToList()); this.windowManager = windowManager; this.parameterFactory = parameterFactory; SelectedIndex = -1; Save = new DelegateCommand(() => { taskRunner.ScheduleTask("Saving conditions definition list", SaveConditions); }, () => IsModified); Delete = new DelegateCommand(DeleteItem); AddItem = new AsyncCommand(AddNewItem); EditItem = new AsyncCommand <ConditionJsonData?>(EditCondition); // history setup historyHandler = new ConditionsEditorHistoryHandler(SourceItems); History = historyCreator(); undoCommand = new DelegateCommand(History.Undo, () => History.CanUndo); redoCommand = new DelegateCommand(History.Redo, () => History.CanRedo); History.PropertyChanged += (sender, args) => { undoCommand.RaiseCanExecuteChanged(); redoCommand.RaiseCanExecuteChanged(); IsModified = !History.IsSaved; }; History.AddHandler(historyHandler); }
public ConditionsEditorViewModel( IConditionsFactory conditionsFactory, IConditionDataManager conditionDataManager, IItemFromListProvider itemFromListProvider, IHistoryManager historyManager, IEnumerable <ICondition>?conditions, int conditionSourceType) { this.conditionsFactory = conditionsFactory; this.conditionDataManager = conditionDataManager; this.HistoryManager = historyManager; ConditionTypes = conditionDataManager .GetConditionGroups() .SelectMany(group => group.Members) .Where(conditionDataManager.HasConditionData) .Select(conditionDataManager.GetConditionData) .ToList(); if (conditions != null) { int previousElseGroup = -1; foreach (var c in conditions) { var vm = conditionsFactory.Create(conditionSourceType, c); if (vm == null) { continue; } if (c.ElseGroup != previousElseGroup && previousElseGroup != -1) { Conditions.Add(conditionsFactory.CreateOr(conditionSourceType)); } previousElseGroup = c.ElseGroup; Conditions.Add(vm); } } Accept = new DelegateCommand(() => CloseOk?.Invoke()); Cancel = new DelegateCommand(() => CloseCancel?.Invoke()); PickCommand = new AsyncAutoCommand <ParameterValueHolder <long> >(async prh => { if (!prh.HasItems) { return; } var newItem = await itemFromListProvider.GetItemFromList(prh.Parameter.Items, prh.Parameter is FlagParameter, prh.Value); if (newItem.HasValue) { prh.Value = newItem.Value; } }); AddItemCommand = new DelegateCommand(() => { int index = Conditions.Count; if (SelectedCondition != null) { index = Conditions.IndexOf(SelectedCondition) + 1; } index = Math.Clamp(index, 0, Conditions.Count); Conditions.Insert(index, conditionsFactory.Create(conditionSourceType, 0) ?? conditionsFactory.CreateOr(conditionSourceType)); }); RemoveItemCommand = new DelegateCommand(() => { if (SelectedCondition == null) { return; } int indexOf = Conditions.IndexOf(SelectedCondition); if (indexOf != -1) { Conditions.RemoveAt(indexOf); if (indexOf - 1 >= 0 && Conditions.Count > 0) { SelectedCondition = Conditions[indexOf - 1]; } else if (Conditions.Count > 0) { SelectedCondition = Conditions[indexOf]; } } }, () => SelectedCondition != null).ObservesProperty(() => SelectedCondition); CopyCommand = new DelegateCommand(() => { if (SelectedCondition != null) { Clipboard = SelectedCondition?.ToCondition(0); } }, () => SelectedCondition != null).ObservesProperty(() => SelectedCondition); CutCommand = new DelegateCommand(() => { if (SelectedCondition != null) { Clipboard = SelectedCondition?.ToCondition(0); Conditions.Remove(SelectedCondition !); SelectedCondition = null; } }, () => SelectedCondition != null).ObservesProperty(() => SelectedCondition); PasteCommand = new DelegateCommand(() => { if (clipboard != null) { int indexOf = Conditions.Count; if (SelectedCondition != null) { indexOf = Conditions.IndexOf(SelectedCondition) + 1; } var item = conditionsFactory.Create(conditionSourceType, clipboard); if (item != null) { Conditions.Insert(indexOf, item); } } }, () => Clipboard != null).ObservesProperty(() => Clipboard); UndoCommand = new DelegateCommand(HistoryManager.Undo, () => HistoryManager.CanUndo).ObservesProperty(() => HistoryManager.CanUndo); RedoCommand = new DelegateCommand(HistoryManager.Redo, () => HistoryManager.CanRedo).ObservesProperty(() => HistoryManager.CanRedo); Watch(this, t => t.SelectedCondition, nameof(SelectedConditionsType)); historyHandler = AutoDispose(new ConditionsEditorHistoryHandler(this, conditionsFactory)); HistoryManager.AddHandler(historyHandler); }