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 { /// /// this class handles RealtimeModule functionality /// [PartCreationPolicy(CreationPolicy.Shared)] public class RealtimeChannelSelectViewModel : IRealtimeChannelSelectViewModel { /// /// The IRealtimeChannelSelect view /// public IRealtimeChannelSelectView ChannelSelectView { get; set; } private IEventAggregator _eventAggregator { 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 RealtimeChannelSelectViewModel /// /// /// 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. // 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(); ConfirmationRequest = new InteractionRequest(); _eventAggregator = eventAggregator; _eventAggregator.GetEvent().Subscribe(OnRaiseNotification); _eventAggregator.GetEvent() .Subscribe(OnBusyIndicatorNotification, ThreadOption.PublisherThread, true); } #endregion #region Methods /// /// sets all available channels which are then filtered and displayed /// /// public void SetAvailableChannels(IRealtimeChannel[] channels) { _allChannels = channels; Filter(); } /// /// filters and displays channels /// private void Filter() { var channels = new List(); 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() { } /// /// 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 }); } /// /// sets the string used to filter, then filters /// /// public void SetSearchText(string searchText) { _searchTerm = searchText; Filter(); } /// /// called to notify realtime that a channel has been selected /// /// public void SetRealtimeChannel(IRealtimeChannel channel) { _eventAggregator.GetEvent().Publish(channel); } #endregion methods #region properties /// /// contains all channels [not filtered] /// private IRealtimeChannel[] _allChannels; /// /// contains channels to be displayed [filtered] /// 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 } }