Example #1
0
 public ConditionTypeChangedAction(IConditionsFactory conditionsFactory, ConditionViewModel condition, int old, int nnew)
 {
     this.conditionsFactory = conditionsFactory;
     this.condition         = condition;
     this.old  = old;
     this.nnew = nnew;
 }
Example #2
0
 public ConditionsEditorHistoryHandler(ConditionsEditorViewModel viewModel, IConditionsFactory conditionsFactory)
 {
     this.conditionsFactory = conditionsFactory;
     conditionsSub          = viewModel.Conditions.ToStream().SubscribeAction(a =>
     {
         if (a.Type == CollectionEventType.Add)
         {
             foreach (var val in  a.Item.Values())
             {
                 val.OnValueChanged += OnChanged;
             }
             a.Item.ConditionChanged += OnConditionChanged;
             PushAction(new ConditionAddedAction(viewModel, a.Item, a.Index));
         }
         else
         {
             a.Item.ConditionChanged -= OnConditionChanged;
             foreach (var val in  a.Item.Values())
             {
                 val.OnValueChanged -= OnChanged;
             }
             PushAction(new ConditionRemovedAction(viewModel, a.Item, a.Index));
         }
     });
 }
        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);
        }