using DTS.Common.Base; using DTS.Common.Events; using DTS.Common.Interactivity; using DTS.Common.Interface; using DTS.Common.Utils; using Prism.Commands; using Prism.Events; using Prism.Regions; using System.Collections.ObjectModel; using System.Threading.Tasks; using Unity; namespace DTS.Viewer.PSDReportResults { public class PSDReportResultsViewModel : BaseViewModel, IPSDReportResultsViewModel { #region properties / fields public IBaseView View { get; set; } public IBaseViewModel Parent { get; set; } public ObservableCollection Results { get; set; } private string Directory { get; set; } private IEventAggregator _eventAggregator { get; set; } private new IRegionManager _regionManager { get; set; } private IUnityContainer _unityContainer { get; set; } public InteractionRequest NotificationRequest { get; private set; } public new InteractionRequest ConfirmationRequest { get; private set; } #endregion /// /// Creates a new instance of the MainViewModel. /// /// 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 Unity container. public PSDReportResultsViewModel(IPSDReportSettingsView view, IRegionManager regionManager, IEventAggregator eventAggregator, IUnityContainer unityContainer) : base(regionManager, eventAggregator, unityContainer) { View = view; View.DataContext = this; NotificationRequest = new InteractionRequest(); ConfirmationRequest = new InteractionRequest(); _eventAggregator = eventAggregator; _unityContainer = unityContainer; _regionManager = regionManager; } public override void Initialize(object parameter) { Parent = (IBaseViewModel)parameter; Results = new ObservableCollection(); Subscribe(); } private void Subscribe() { _eventAggregator.GetEvent().Subscribe(OnGRMSValuesUpdated, ThreadOption.UIThread); _eventAggregator.GetEvent().Subscribe(OnGraphSelectedChannelsChanged); } private void OnGRMSValuesUpdated(PSDReportGRMSValuesUpdatedEventArg arg) { if (Parent != arg.ParentVM) return; Results.Clear(); foreach (var val in arg.Values) { Results.Add(val); } } private void OnGraphSelectedChannelsChanged(GraphSelectedChannelsNotificationArg arg) { if (Parent != arg?.ParentVM) return; var channels = arg?.SelectedChannels ?? new System.Collections.Generic.List(); Directory = channels.Count > 0 ? channels[0].BinaryFilePath.ReplaceLast("Binary", "Reports") : string.Empty; } private DelegateCommand _exportToPDFCommand; public DelegateCommand ExportToPDFCommand => _exportToPDFCommand ?? (_exportToPDFCommand = new DelegateCommand(ExportToPDFMethod)); private void ExportToPDFMethod() { _eventAggregator.GetEvent().Publish(new SaveReportToPDFRequestedEventArgs() { Directory = Directory, ParentVM = Parent }); } private DelegateCommand _exportToCSVCommand; public DelegateCommand ExportToCSVCommand => _exportToCSVCommand ?? (_exportToCSVCommand = new DelegateCommand(ExportToCSVMethod)); private void ExportToCSVMethod() { _eventAggregator.GetEvent().Publish(new SaveReportToCSVRequestedEventArgs() { Directory = Directory, ParentVM = Parent }); } } }