aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/FSE/Modules/Tango.FSE.Insights/Dialogs/AnomaliesDialogViewVM.cs
blob: 42c71327b9658a6298335748e98dd49eefcf9c9f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;
using Tango.FSE.Common;
using Tango.FSE.Insights.SciChart;
using Tango.SharedUI.Components;

namespace Tango.FSE.Insights.Dialogs
{
    public class AnomaliesDialogViewVM : FSEDialogViewVM
    {
        private const int MAX_SELECTION = 8;
        private ICollectionView _view;

        public SelectedObjectCollection<InsightsChart> SelectedCharts { get; set; }

        private String _filter;
        public String Filter
        {
            get { return _filter; }
            set { _filter = value; RaisePropertyChangedAuto(); OnFilterChanged(); }
        }

        public bool MaximumSelectionReached
        {
            get { return SelectedCharts.SynchedSource.Count > MAX_SELECTION; }
        }

        public AnomaliesDialogViewVM(List<InsightsChart> availableCharts, List<InsightsChart> selectedCharts)
        {
            OKText = "ANALYZE";
            SelectedCharts = new SelectedObjectCollection<InsightsChart>(availableCharts.OrderByDescending(x => selectedCharts.Contains(x)).ToObservableCollection(), selectedCharts.ToObservableCollection());
            SelectedCharts.SelectionChanged += SelectedCharts_SelectionChanged;

            _view = CollectionViewSource.GetDefaultView(SelectedCharts);
            _view.Filter = FilterCharts;
        }

        private bool FilterCharts(object obj)
        {
            if (String.IsNullOrWhiteSpace(Filter)) return true;
            return (obj as SelectedObject<InsightsChart>).Data.Description.ToLower().Contains(Filter.ToLower());
        }

        private void SelectedCharts_SelectionChanged(object sender, EventArgs e)
        {
            RaisePropertyChanged(nameof(MaximumSelectionReached));
            InvalidateRelayCommands();
        }

        protected override bool CanOK()
        {
            return base.CanOK() && SelectedCharts.SynchedSource.Count > 0 && !MaximumSelectionReached;
        }

        private void OnFilterChanged()
        {
            _view?.Refresh();
        }
    }
}