Files
DP44/DataPRO/Modules/TestSetups/TestSetupsList/ViewModel/TestSetupsListViewModel.cs
2026-04-17 14:55:32 -04:00

385 lines
15 KiB
C#

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
{
/// <summary>
/// this class handles TestSetupsList functionality
/// </summary>
[PartCreationPolicy(CreationPolicy.Shared)]
public class TestSetupsListViewModel : ITestSetupsListViewModel
{
/// <summary>
/// The SensorList view
/// </summary>
public ITestSetupsListView View { get; set; }
private IEventAggregator _eventAggregator { get; }
private IRegionManager _regionManager;
private IUnityContainer UnityContainer { get; }
public InteractionRequest<Notification> NotificationRequest { get; }
public InteractionRequest<Confirmation> ConfirmationRequest { get; }
/// <inheritdoc />
/// <summary>
/// Occurs when a property value changes.
/// </summary>
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
#region constructors and initializers
/// <summary>
/// Creates a new instance of the TestSetupsListViewModel
/// </summary>
/// <param name="view"></param>
/// <param name="regionManager">The logical placeholder defined within the application's UI (in the shell or within views) into which views are displayed.</param>
/// <param name="eventAggregator">The EventAggregator which allows different components to publish/subscribe to events without being coupled to each other.</param>
/// <param name="unityContainer">The unityContainer.</param>
public TestSetupsListViewModel(ITestSetupsListView view, IRegionManager regionManager,
IEventAggregator eventAggregator, IUnityContainer unityContainer)
{
View = view;
View.DataContext = this;
NotificationRequest = new InteractionRequest<Notification>();
ConfirmationRequest = new InteractionRequest<Confirmation>();
_eventAggregator = eventAggregator;
UnityContainer = unityContainer;
_regionManager = regionManager;
_eventAggregator.GetEvent<RaiseNotification>().Subscribe(OnRaiseNotification);
_eventAggregator.GetEvent<BusyIndicatorChangeNotification>()
.Subscribe(OnBusyIndicatorNotification, ThreadOption.PublisherThread, true);
SelectedTestSetupItems = new BulkObservableCollection<ITestSetup>();
}
#endregion
#region Methods
public void ClearAllFilters()
{
_currentSearchTermByField.Clear();
}
public void Unset()
{
_allTestSetup = new ITestSetup[0];
TestSetups = new ITestSetup[0];
OnPropertyChanged("TestSetups");
ClearAllFilters();
_eventAggregator.GetEvent<ListViewStatusEvent>()
.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<ITestSetup>(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<TestSetupFields>().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()
{
}
/// <summary>
/// Private Event handler for RaiseNotification event.
/// </summary>
private void OnBusyIndicatorNotification(bool eventArg)
{
IsBusy = eventArg;
}
/// <summary>
/// Private Event handler for RaiseNotification event.
/// </summary>
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<TestSetupsListEditTestSetupEvent>().Publish(TestSetups[index].Name);
}
}
private void FireSelectionChanged()
{
_eventAggregator.GetEvent<TestSetupsListTestSetupSelectedEvent>().Publish(_selectedTestSetupItems.Select(ts => ts.Name).ToArray());
}
#endregion
#region Properties
private Dictionary<TestSetupFields, string> _currentSearchTermByField = new Dictionary<TestSetupFields, string>();
private TestSetupFields _currentSortField = TestSetupFields.LastModified;
private readonly TestSetupComparer _testComparer = new TestSetupComparer();
private bool _sortAscending = false;
public int SelectedTestSetupIndex { get; set; } = -1;
public BulkObservableCollection<ITestSetup> _selectedTestSetupItems = new BulkObservableCollection<ITestSetup>();
public BulkObservableCollection<ITestSetup> 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
}
}