using DTS.Common.Base; using DTS.Common.Enums; using DTS.Common.Enums.Sensors; using DTS.Common.Interface; using System; using System.Windows; using System.Collections.Generic; using System.ComponentModel; using System.Threading.Tasks; using DTS.Common.Events; using DTS.Viewer.PSDReport.Resources; using DTS.Common.Utils; using Prism.Events; using Unity; using Prism.Regions; using DTS.Common.Interactivity; namespace DTS.Viewer.PSDReport { public class PSDReportMainViewModel : BaseViewModel, IPSDReportMainViewModel { public IBaseView View { get; set; } private IBaseWindowModel Parent { get; set; } private IEventAggregator _eventAggregator { get; set; } private IUnityContainer _unityContainer { get; set; } public InteractionRequest NotificationRequest { get; private set; } public new InteractionRequest ConfirmationRequest { get; private set; } /// /// 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 PSDReportMainViewModel(IRegionManager regionManager, IEventAggregator eventAggregator, IUnityContainer unityContainer) : base(regionManager, eventAggregator, unityContainer) { NotificationRequest = new InteractionRequest(); ConfirmationRequest = new InteractionRequest(); _eventAggregator = eventAggregator; _unityContainer = unityContainer; View = _unityContainer.Resolve(); View.DataContext = this; } #region properties public object ContextNavigationRegion { get => ((PSDReportMainViewGrid)View).NavigationRegion.Content; set { ((PSDReportMainViewGrid)View).NavigationRegion.Content = value; OnPropertyChanged("ContextNavigationRegion"); } } public object ContextGraphsRegion { get => ((PSDReportMainViewGrid)View).GraphListRegion.Content; set { ((PSDReportMainViewGrid)View).GraphListRegion.Content = value; OnPropertyChanged("ContextGraphsRegion"); } } public object ContextGraphListRegion { get => ((PSDReportMainViewGrid)View).GraphListRegion.Content; set { ((PSDReportMainViewGrid)View).GraphListRegion.Content = value; OnPropertyChanged("ContextGraphListRegion"); } } public object ContextTestsRegion { get => ((PSDReportMainViewGrid)View).TestsRegion.Content; set { ((PSDReportMainViewGrid)View).TestsRegion.Content = value; OnPropertyChanged("ContextTestsRegion"); } } public object ContextLegendRegion { get => ((PSDReportMainViewGrid)View).LegendRegion.Content; set { ((PSDReportMainViewGrid)View).LegendRegion.Content = value; OnPropertyChanged("ContextLegendRegion"); } } public object ContextPropertyRegion { get; set; } public object ContextChartOptionsRegion { get => ((PSDReportMainViewGrid)View).ChartOptionsRegion.Content; set { ((PSDReportMainViewGrid)View).ChartOptionsRegion.Content = value; OnPropertyChanged("ContextChartOptionsRegion"); } } public object ContextViewerSettingsRegion { get => ((PSDReportMainViewGrid)View).SettingsRegion.Content; set { ((PSDReportMainViewGrid)View).SettingsRegion.Content = value; OnPropertyChanged("ContextViewerSettingsRegion"); } } public object ContextReportDataSelectRegion { get => ((PSDReportMainViewGrid)View).DataSelectRegion.Content; set { ((PSDReportMainViewGrid)View).DataSelectRegion.Content = value; OnPropertyChanged("ContextReportDataSelectRegion"); } } public object ContextGraphRegion { get => ((PSDReportMainViewGrid)View).GraphRegion.Content; set { ((PSDReportMainViewGrid)View).GraphRegion.Content = value; OnPropertyChanged("ContextGraphRegion"); } } public object ContextReportChartOptionsRegion { get => ((PSDReportMainViewGrid)View).ReportChartOptionsRegion.Content; set { ((PSDReportMainViewGrid)View).ReportChartOptionsRegion.Content = value; OnPropertyChanged("ContextPSDChartOptionsRegion"); } } public object ContextReportResultsRegion { get => ((PSDReportMainViewGrid)View).ReportResultsRegion.Content; set { ((PSDReportMainViewGrid)View).ReportResultsRegion.Content = value; OnPropertyChanged("ContextPSDResultsRegion"); } } public string ConfigPath { get => throw new NotImplementedException(); set => throw new NotImplementedException(); } #region Test Title private string _titleTests = StringResources.TestsDefaultTitle; public string TitleTests { get => _titleTests; set { _titleTests = value; OnPropertyChanged("TitleTests"); } } private int _totalSelectedTests = 0; public int TotalSelectedTests { get => _totalSelectedTests; set { _totalSelectedTests = value; TitleTests = StringResources.TestsDefaultTitle + TotalSelectedTests + "/" + TotalLoadedTests; if (_totalSelectedTests == 0) { TitleGraphs = StringResources.GraphsDefaultTitle; } OnPropertyChanged("TotalSelectedTests"); } } private int _totalLoadedTests = 0; public int TotalLoadedTests { get => _totalLoadedTests; set { _totalLoadedTests = value; TitleTests = StringResources.TestsDefaultTitle + TotalSelectedTests + "/" + TotalLoadedTests; OnPropertyChanged("TotalLoadedTests"); } } #endregion Test Title #region Graph Title private string _titleGraphs = StringResources.GraphsDefaultTitle; public string TitleGraphs { get => _titleGraphs; set { _titleGraphs = value; OnPropertyChanged("TitleGraphs"); } } private int _totalSelectedGraphs = 0; public int TotalSelectedGraphs { get => _totalSelectedGraphs; set { _totalSelectedGraphs = value; TitleGraphs = StringResources.GraphsDefaultTitle + TotalSelectedGraphs + "/" + TotalLoadedGraphs; OnPropertyChanged("TotalSelectedGraphs"); } } private int _totalLoadedGraphs = 0; public int TotalLoadedGraphs { get => _totalLoadedGraphs; set { _totalLoadedGraphs = value; TitleGraphs = StringResources.GraphsDefaultTitle + TotalSelectedGraphs + "/" + TotalLoadedGraphs; OnPropertyChanged("TotalLoadedGraphs"); } } #endregion Graph Title private string _selectedDataFolder = string.Empty; public string SelectedDataFolder { get => _selectedDataFolder; set { if (string.IsNullOrEmpty(value)) return; _selectedDataFolder = value; _eventAggregator.GetEvent().Publish(new DataFolderSelectionArg { Path = _selectedDataFolder, File = string.Empty, SelectAll = false, ParentVM = this }); OnPropertyChanged("SelectedDataFolder"); } } public bool DoesUserHaveEditPermission { get; set; } = false; private string _selectedDataFile = string.Empty; public string SelectedDataFile { get => _selectedDataFile; set { if (string.IsNullOrEmpty(value)) return; _selectedDataFile = value; _eventAggregator.GetEvent().Publish(new DataFolderSelectionArg { Path = string.Empty, File = _selectedDataFile, SelectAll = false, ParentVM = this }); OnPropertyChanged("SelectedDataFile"); } } private IsoViewMode _channelCodeViewMode = IsoViewMode.ISOAndUserCode; public IsoViewMode ChannelCodeViewMode { get => _channelCodeViewMode; set { _channelCodeViewMode = value; _eventAggregator.GetEvent().Publish(value); OnPropertyChanged("ChannelCodeViewMode"); } } private CalibrationBehaviors _calibrationBehaviorSetting = CalibrationBehaviors.NonLinearIfAvailable; public CalibrationBehaviors CalibrationBehaviorSetting { get => _calibrationBehaviorSetting; set { _calibrationBehaviorSetting = value; _eventAggregator.GetEvent().Publish(value); OnPropertyChanged("CalibrationBehaviorSetting"); } } private bool _calibrationBehaviorSettableInViewer = true; public bool CalibrationBehaviorSettableInViewer { get => _calibrationBehaviorSettableInViewer; set { _calibrationBehaviorSettableInViewer = value; _eventAggregator.GetEvent().Publish(value); OnPropertyChanged("CalibrationBehaviorSettableInViewer"); //FB13946: Test tab should be the first if in Viewer Tile, graphs tab if in Run Test/Download //CalibrationSettableInViewer is a proxy here for which tile we're in ((PSDReportMainViewGrid)View).graphsTab.IsSelected = !value; ((PSDReportMainViewGrid)View).testsTab.IsSelected = value; //FB 14797 select first tab always to not switch when the graph is loading. ((PSDReportMainViewGrid)View).chartResultsTab.IsSelected = true; ((PSDReportMainViewGrid)View).chartResultsTab.Focusable = true; ((PSDReportMainViewGrid)View).chartResultsTab.Focus(); } } private Visibility _settingsVisibility = Visibility.Visible; public Visibility SettingsVisibility { get => _settingsVisibility; private set { _settingsVisibility = value; OnPropertyChanged("SettingsVisibility"); } } private bool _isBusy = false; /// /// Display or hide Busy Indicator /// public new bool IsBusy { get => _isBusy; set { _isBusy = value; OnPropertyChanged("IsBusy"); } } private string _isBusyMessage = string.Empty; /// /// Busy Message text /// public new string IsBusyMessage { get => _isBusyMessage; set { _isBusyMessage = value; OnPropertyChanged("IsBusyMessage"); } } private bool _isMenuIncluded = false; public new bool IsMenuIncluded { get => _isMenuIncluded; set { _isMenuIncluded = value; OnPropertyChanged("IsMenuIncluded"); } } private bool _isNavigationIncluded = false; public new bool IsNavigationIncluded { get => _isNavigationIncluded; set { _isNavigationIncluded = value; OnPropertyChanged("IsNavigationIncluded"); } } public bool IsDirty => throw new NotImplementedException(); #endregion #region methods public List GetRegions() { var items = new List(); Utils.GetChildrenByName(((PSDReportMainViewGrid)View).MainShell, "Region", ref items); return items; } /// /// Initialize without parameter /// public override void Initialize() { Subscribe(); } /// /// Initialize with parameter - Parent /// public override void Initialize(object parameter) { Parent = (IBaseWindowModel)parameter; Parent.IsMenuIncluded = _isMenuIncluded; Parent.IsNavigationIncluded = _isNavigationIncluded; Subscribe(); View.DataContext = this; } public void LeftKeyPress() { throw new NotImplementedException(); } public void RightKeyPress() { throw new NotImplementedException(); } public void ZoomReset() { _eventAggregator.GetEvent().Publish(true); } /// ///Occurs when a property value changes. /// public new event PropertyChangedEventHandler PropertyChanged; private new void OnPropertyChanged(string propertyName) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } private void OnDataFileSelected(DataFileSelectionArg arg) { if (this != arg.ParentVM) return; SelectAndIncludeDataFile(arg?.File); } public void SelectAndIncludeDataFile(string value) { if (string.IsNullOrEmpty(value)) return; _selectedDataFile = value; _eventAggregator.GetEvent().Publish(new DataFolderSelectionArg { Path = string.Empty, File = _selectedDataFile, SetSelected = true, SelectAll = false, ParentVM = this }); OnPropertyChanged("SelectedDataFile"); } private void Subscribe() { _eventAggregator.GetEvent().Subscribe(OnLoadViewModul, ThreadOption.PublisherThread, true); _eventAggregator.GetEvent().Subscribe(OnTestLoadedChanged); _eventAggregator.GetEvent().Subscribe(OnTestSelectedCountChanged); _eventAggregator.GetEvent().Subscribe(OnGraphLoadedCountChanged); _eventAggregator.GetEvent().Subscribe(OnGraphSelectedCountChanged); _eventAggregator.GetEvent().Subscribe(OnDataFileSelected); _eventAggregator.GetEvent().Subscribe(OnGraphChannelsReadCompleted, ThreadOption.UIThread); } private void OnLoadViewModul(LoadViewModulArg arg) { IsBusy = true; IsBusyMessage = "Loading view list"; if (null == ContextGraphRegion) ContextGraphRegion = GetGraphView(this, "Graph"); if (null == ContextReportDataSelectRegion) ContextReportDataSelectRegion = GetGraphView(this, "DataSelect"); if (null == ContextTestsRegion) ContextTestsRegion = GetTestDefinitionView(this); if (null == ContextGraphListRegion) ContextGraphListRegion = GetGraphListView(this); if (null == ContextChartOptionsRegion) ContextChartOptionsRegion = GetChartOptionsView(this, "DataSelect"); if (null == ContextReportChartOptionsRegion) ContextReportChartOptionsRegion = GetChartOptionsView(this, "Graph"); if (null == ContextReportResultsRegion) ContextReportResultsRegion = GetReportResultsView(this); if (null == ContextViewerSettingsRegion) ContextViewerSettingsRegion = GetReportSettingsView(this); IsBusy = false; IsBusyMessage = string.Empty; } private void OnGraphLoadedCountChanged(GraphLoadedCountNotificationArg arg) { if (this != arg?.ParentVM) return; TotalLoadedGraphs = arg.LoadedCount; } private void OnGraphSelectedCountChanged(GraphSelectedChannelCountNotificationArg arg) { if (this != arg?.ParentVM) return; TotalSelectedGraphs = arg.SelectedChannelCount; } private void OnTestLoadedChanged(TestLoadedCountNotificationArg arg) { if (this != arg?.ParentVM) return; TotalLoadedTests = arg.LoadedCount; } private void OnTestSelectedCountChanged(TestSummaryCountNotificationArg arg) { if (this != arg?.ParentVM) return; TotalSelectedTests = arg.SummaryCount; } int reads = 0; private void OnGraphChannelsReadCompleted(GraphChannelsReadCompletedNotificationArgs arg) { if (null == arg) { return; } if (((IGraphView)ContextGraphRegion).DataContext != arg?.GraphVM && (((IGraphView)ContextReportDataSelectRegion).DataContext != arg?.GraphVM)) return; if (arg.IsReadCompleted) { if (reads > 0) reads--; if (reads == 0) _eventAggregator.GetEvent().Publish(false); } else { reads++; _eventAggregator.GetEvent().Publish(true); } } #endregion #region subscription methods private void OnViewerSettingsVisibilityChanged(Visibility settingsVisibility) { SettingsVisibility = settingsVisibility; } #endregion #region views private INavigationView GetNavigationView(IBaseViewModel parent) { var view = _unityContainer.Resolve(); var viewModel = _unityContainer.Resolve(); view.DataContext = viewModel; viewModel.Initialize(parent); return view; } private INavigationViewModel GetNavigationViewModel(IBaseViewModel parent) { var view = _unityContainer.Resolve(); var viewModel = _unityContainer.Resolve(); view.DataContext = viewModel; viewModel.Initialize(parent); return viewModel; } private IPSDReportResultsView GetReportResultsView(IBaseViewModel parent) { var view = _unityContainer.Resolve(); var viewModel = _unityContainer.Resolve(); view.DataContext = viewModel; viewModel.Initialize(parent); return view; } private IPSDReportSettingsView GetReportSettingsView(IBaseViewModel parent) { var view = _unityContainer.Resolve(); var viewModel = _unityContainer.Resolve(); view.DataContext = viewModel; viewModel.Initialize(parent); return view; } private IChartOptionsView GetChartOptionsView(IBaseViewModel parent, string chartType) { var view = _unityContainer.Resolve(); var viewModel = _unityContainer.Resolve(); view.DataContext = viewModel; viewModel.Initialize(new Tuple(parent, chartType)); return view; } private IGraphView GetGraphView(IBaseViewModel parent, string graphType) { var view = _unityContainer.Resolve(); var viewModel = _unityContainer.Resolve(); view.DataContext = viewModel; viewModel.Initialize(new Tuple(parent, graphType)); return view; } private IGraphMainView GetGraphListView(IBaseViewModel parent) { var view = _unityContainer.Resolve(); var viewModel = _unityContainer.Resolve(); view.DataContext = viewModel; viewModel.Initialize(parent); return view; } private IViewerSettingsView GetViewerSettingsView(IBaseViewModel parent) { var view = _unityContainer.Resolve(); var viewModel = _unityContainer.Resolve(); view.DataContext = viewModel; viewModel.Initialize(parent); return view; } private ITestSummaryListView GetTestDefinitionView(IBaseViewModel parent) { var view = _unityContainer.Resolve(); var viewModel = _unityContainer.Resolve(); view.DataContext = viewModel; viewModel.Initialize(parent); return view; } #endregion } }