Files
DP44/DataPRO/Modules/Realtime/RealtimeModule/ViewModel/RealtimeChannelSelectViewModel.cs
2026-04-17 14:55:32 -04:00

235 lines
7.8 KiB
C#

using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.Composition;
using System.Linq;
using System.Threading.Tasks;
using DTS.Common.Events;
using DTS.Common.Events.Realtime;
using DTS.Common.Interactivity;
using DTS.Common.Interface.Realtime;
using Prism.Events;
using Prism.Regions;
using Unity;
// ReSharper disable CheckNamespace
// ReSharper disable MemberCanBePrivate.Global
// ReSharper disable InconsistentNaming
namespace RealtimeModule
{
/// <summary>
/// this class handles RealtimeModule functionality
/// </summary>
[PartCreationPolicy(CreationPolicy.Shared)]
public class RealtimeChannelSelectViewModel : IRealtimeChannelSelectViewModel
{
/// <summary>
/// The IRealtimeChannelSelect view
/// </summary>
public IRealtimeChannelSelectView ChannelSelectView { get; set; }
private IEventAggregator _eventAggregator { 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 RealtimeChannelSelectViewModel
/// </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>
// ReSharper disable once UnusedParameter.Local
public RealtimeChannelSelectViewModel(IRealtimeChannelSelectView view, IRegionManager regionManager,
// ReSharper disable once UnusedParameter.Local
IEventAggregator eventAggregator, IUnityContainer unityContainer)
{
ChannelSelectView = view;
ChannelSelectView.DataContext = this;
NotificationRequest = new InteractionRequest<Notification>();
ConfirmationRequest = new InteractionRequest<Confirmation>();
_eventAggregator = eventAggregator;
_eventAggregator.GetEvent<RaiseNotification>().Subscribe(OnRaiseNotification);
_eventAggregator.GetEvent<BusyIndicatorChangeNotification>()
.Subscribe(OnBusyIndicatorNotification, ThreadOption.PublisherThread, true);
}
#endregion
#region Methods
/// <summary>
/// sets all available channels which are then filtered and displayed
/// </summary>
/// <param name="channels"></param>
public void SetAvailableChannels(IRealtimeChannel[] channels)
{
_allChannels = channels;
Filter();
}
/// <summary>
/// filters and displays channels
/// </summary>
private void Filter()
{
var channels = new List<IRealtimeChannel>();
if (string.IsNullOrWhiteSpace(_searchTerm))
{
channels.AddRange(_allChannels);
}
else
{
var ci = System.Globalization.CultureInfo.CurrentCulture;
channels.AddRange(_allChannels.Where(channel =>
ci.CompareInfo.IndexOf(channel.ToString(), _searchTerm,
System.Globalization.CompareOptions.IgnoreCase) >= 0));
}
RealtimeChannels = channels.ToArray();
OnPropertyChanged("RealtimeChannels");
}
public void Unset()
{
}
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
});
}
/// <summary>
/// sets the string used to filter, then filters
/// </summary>
/// <param name="searchText"></param>
public void SetSearchText(string searchText)
{
_searchTerm = searchText;
Filter();
}
/// <summary>
/// called to notify realtime that a channel has been selected
/// </summary>
/// <param name="channel"></param>
public void SetRealtimeChannel(IRealtimeChannel channel)
{
_eventAggregator.GetEvent<RealtimeChannelSelectedEvent>().Publish(channel);
}
#endregion methods
#region properties
/// <summary>
/// contains all channels [not filtered]
/// </summary>
private IRealtimeChannel[] _allChannels;
/// <summary>
/// contains channels to be displayed [filtered]
/// </summary>
public IRealtimeChannel[] RealtimeChannels { get; set; } = new IRealtimeChannel[0];
public bool IsDirty => false;
private string _searchTerm = "";
private bool _isBusy;
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");
}
}
#endregion Properties
#region Commands
#endregion
}
}