using System.ComponentModel; using System.ComponentModel.Composition; using System.Threading.Tasks; using DTS.Common.Events; using DTS.Common.Interface.TestSetups.TestSetupsList; using DTS.Common.Enums.TestSetups.TestSetupList; using TestSetupsList.Model; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Collections.Specialized; using System.Linq; using DTS.Common.Events.TestSetups.TestSetupsList; using Prism.Events; using Unity; using DTS.Common.Interactivity; using Prism.Regions; using DTS.Common.Classes; // ReSharper disable CheckNamespace // ReSharper disable MemberCanBePrivate.Global // ReSharper disable InconsistentNaming namespace TestSetupsList { /// /// this class handles TestSetupsList functionality /// [PartCreationPolicy(CreationPolicy.Shared)] public class TestSetupsListViewModel : ITestSetupsListViewModel { /// /// The SensorList view /// public ITestSetupsListView View { get; set; } private IEventAggregator _eventAggregator { get; } private IRegionManager _regionManager; private IUnityContainer UnityContainer { get; } public InteractionRequest NotificationRequest { get; } public InteractionRequest ConfirmationRequest { get; } /// /// /// Occurs when a property value changes. /// public event PropertyChangedEventHandler PropertyChanged; public void OnPropertyChanged(string propertyName) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } #region constructors and initializers /// /// Creates a new instance of the TestSetupsListViewModel /// /// /// The logical placeholder defined within the application's UI (in the shell or within views) into which views are displayed. /// The EventAggregator which allows different components to publish/subscribe to events without being coupled to each other. /// The unityContainer. public TestSetupsListViewModel(ITestSetupsListView view, IRegionManager regionManager, IEventAggregator eventAggregator, IUnityContainer unityContainer) { View = view; View.DataContext = this; NotificationRequest = new InteractionRequest(); ConfirmationRequest = new InteractionRequest(); _eventAggregator = eventAggregator; UnityContainer = unityContainer; _regionManager = regionManager; _eventAggregator.GetEvent().Subscribe(OnRaiseNotification); _eventAggregator.GetEvent() .Subscribe(OnBusyIndicatorNotification, ThreadOption.PublisherThread, true); SelectedTestSetupItems = new BulkObservableCollection(); } #endregion #region Methods public void ClearAllFilters() { _currentSearchTermByField.Clear(); } public void Unset() { _allTestSetup = new ITestSetup[0]; TestSetups = new ITestSetup[0]; OnPropertyChanged("TestSetups"); ClearAllFilters(); _eventAggregator.GetEvent() .Publish(new ListViewStatusArg(ListViewStatusArg.ListViewStatus.Unloaded, ListViewId)); } public void Sort(object o, bool bColumnClick) { if (!(o is string tag)) { return; } if (!Enum.TryParse(tag, out TestSetupFields field)) return; if (bColumnClick) { if (field != _currentSortField) { _currentSortField = field; _sortAscending = true; } else { _sortAscending = !_sortAscending; } } _testComparer.SortAscending = _sortAscending; _testComparer.SortField = _currentSortField; var list = new List(TestSetups); list.Sort(_testComparer); if (TestSetups.SequenceEqual(list) && TestSetups.Length > 1) { //list was already in order. flip it for the user _sortAscending = !_sortAscending; _testComparer.SortAscending = _sortAscending; list.Sort(_testComparer); } TestSetups = list.ToArray(); OnPropertyChanged("TestSetups"); } public void Filter(object objectTag, string term) { if (Enum.TryParse((string)objectTag, out TestSetupFields field)) { _currentSearchTermByField[field] = term; Filter(_currentSearchFilter); } } private bool TestSetupFilter(ITestSetup t) { var fields = Enum.GetValues(typeof(TestSetupFields)).Cast().ToArray(); foreach (var field in fields) { if (!_currentSearchTermByField.ContainsKey(field)) { continue; } var term = _currentSearchTermByField[field]; if (string.IsNullOrWhiteSpace(term)) { continue; } switch (field) { case TestSetupFields.Name: if (!(System.Globalization.CultureInfo.CurrentCulture.CompareInfo.IndexOf(t.Name, term, System.Globalization.CompareOptions.OrdinalIgnoreCase) >= 0)) { return false; } break; case TestSetupFields.Description: if (!(System.Globalization.CultureInfo.CurrentCulture.CompareInfo.IndexOf(t.Description, term, System.Globalization.CompareOptions.OrdinalIgnoreCase) >= 0)) { return false; } break; case TestSetupFields.RecordingMode: if (!(System.Globalization.CultureInfo.CurrentCulture.CompareInfo.IndexOf(t.RecordingMode.ToString(), term, System.Globalization.CompareOptions.OrdinalIgnoreCase) >= 0)) { return false; } break; case TestSetupFields.PreTriggerSeconds: if (!(System.Globalization.CultureInfo.CurrentCulture.CompareInfo.IndexOf(t.PreTriggerSeconds.ToString(), term, System.Globalization.CompareOptions.OrdinalIgnoreCase) >= 0)) { return false; } break; case TestSetupFields.PostTriggerSeconds: if (!(System.Globalization.CultureInfo.CurrentCulture.CompareInfo.IndexOf(t.PostTriggerSeconds.ToString(), term, System.Globalization.CompareOptions.OrdinalIgnoreCase) >= 0)) { return false; } break; case TestSetupFields.LastModified: if (!(System.Globalization.CultureInfo.CurrentCulture.CompareInfo.IndexOf(t.LastModified.ToString(), term, System.Globalization.CompareOptions.OrdinalIgnoreCase) >= 0)) { return false; } break; case TestSetupFields.LastModifiedBy: if (!(System.Globalization.CultureInfo.CurrentCulture.CompareInfo.IndexOf(t.LastModifiedBy, term, System.Globalization.CompareOptions.OrdinalIgnoreCase) >= 0)) { return false; } break; case TestSetupFields.IsComplete: if (t.IsComplete != Convert.ToBoolean(term)) { return false; } break; default: throw new ArgumentOutOfRangeException(); } } return true; } public void Filter(string term) { _currentSearchFilter = term; TestSetups = _allTestSetup.Where(t => t.Filter(_currentSearchFilter)).Where(TestSetupFilter).ToArray(); OnPropertyChanged("TestSetups"); } public void SetTestSetups(ITestSetup[] allTestSetupsForUser) { _allTestSetup = allTestSetupsForUser; Filter(_currentSearchFilter); Sort(_currentSortField.ToString(), false); } public void Cleanup() { } public Task CleanupAsync() { return Task.CompletedTask; } public void Initialize() { } public void Initialize(object parameter) { } public void Initialize(object parameter, object model) { } public Task InitializeAsync() { return Task.CompletedTask; } public Task InitializeAsync(object parameter) { return Task.CompletedTask; } public void Activated() { } /// /// Private Event handler for RaiseNotification event. /// private void OnBusyIndicatorNotification(bool eventArg) { IsBusy = eventArg; } /// /// Private Event handler for RaiseNotification event. /// private void OnRaiseNotification(NotificationContentEventArgs eventArgsWithTitle) { // The NotificationRequest.Raise triggers the Invoke() method of the PopupWindowAction object to show the NotificationWindow window // Notification object expects a NotificationContentEventArgsWithoutTitle object and a Title string. var eventArgsWithoutTitle = new NotificationContentEventArgs(eventArgsWithTitle.Message, "", eventArgsWithTitle.Image, string.Empty); NotificationRequest.Raise(new Notification { Content = eventArgsWithoutTitle, Title = eventArgsWithTitle.Title }); } public void MouseDoubleClick(int index) { if (index >= 0 && index < TestSetups.Length && SelectedTestSetupItems.Count == 1) { _eventAggregator.GetEvent().Publish(TestSetups[index].Name); } } private void FireSelectionChanged() { _eventAggregator.GetEvent().Publish(_selectedTestSetupItems.Select(ts => ts.Name).ToArray()); } #endregion #region Properties private Dictionary _currentSearchTermByField = new Dictionary(); private TestSetupFields _currentSortField = TestSetupFields.LastModified; private readonly TestSetupComparer _testComparer = new TestSetupComparer(); private bool _sortAscending = false; public int SelectedTestSetupIndex { get; set; } = -1; public BulkObservableCollection _selectedTestSetupItems = new BulkObservableCollection(); public BulkObservableCollection SelectedTestSetupItems { get => _selectedTestSetupItems; set { if (_selectedTestSetupItems != null) { _selectedTestSetupItems.CollectionChanged -= SelectedTestSetupItemsOnCollectionChanged; } _selectedTestSetupItems = value; if (_selectedTestSetupItems != null) { _selectedTestSetupItems.CollectionChanged += SelectedTestSetupItemsOnCollectionChanged; } FireSelectionChanged(); } } private void SelectedTestSetupItemsOnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e) { //if selection changed and the SelectedTestSetupItems is 0, maybe the units were deleted? //get updating appears to be checking whether the unit is already selected, but in this //case SelectedTestSetupItems is null and GetUpdating is returning true so the selection changed //was not firing //18382 DataPRO crash on Test Setup tab if (DTS.Common.Enums.SelectedItemsStatus.GetUpdating(SelectedTestSetupItems) && !(null == SelectedTestSetupItems || 0 == SelectedTestSetupItems.Count)) { return; } FireSelectionChanged(); } private ITestSetup[] _allTestSetup { get; set; } public ITestSetup[] TestSetups { get; set; } public bool IsDirty { get; private set; } private bool _isBusy; private string _currentSearchFilter = ""; public bool IsBusy { get => _isBusy; set { _isBusy = value; OnPropertyChanged("IsBusy"); } } private bool _isMenuIncluded; public bool IsMenuIncluded { get => _isMenuIncluded; set { _isMenuIncluded = value; OnPropertyChanged("IsMenuIncluded"); } } private bool _isNavigationIncluded; public bool IsNavigationIncluded { get => _isNavigationIncluded; set { _isNavigationIncluded = value; OnPropertyChanged("IsNavigationIncluded"); } } public string ListViewId => "TestSetupsListView"; #endregion Properties #region Commands #endregion } }