using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Linq.Expressions; using System.Runtime.CompilerServices; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Input; using System.Windows.Media; using Tango.BL; using Tango.Core.DI; using Tango.FSE.BL; using Tango.FSE.Common.Authentication; using Tango.FSE.Common.Connection; using Tango.FSE.Common.Diagnostics; using Tango.FSE.Common.FileSystem; using Tango.FSE.Common.FSEApplication; using Tango.FSE.Common.Logging; using Tango.FSE.Common.Navigation; using Tango.FSE.Common.Notifications; using Tango.FSE.Common.Performance; using Tango.FSE.Common.MachineUpdates; using Tango.FSE.Common.RemoteDesktop; using Tango.FSE.Common.Resolution; using Tango.FSE.Common.Storage; using Tango.FSE.Common.SystemInfo; using Tango.Settings; using Tango.SharedUI; using static Tango.SharedUI.Controls.NavigationControl; using Tango.FSE.BL.Connectivity; using Tango.FSE.Common.BugReporting; using Tango.FSE.Common.RemoteUpgrade; using Tango.FSE.Common.AutoComplete; using Tango.BL.Entities; using Tango.FSE.Common.Firmware; using Tango.FSE.Common.Threading; using Tango.FSE.Common.Events; using Tango.FSE.Common.Tiles; using Tango.FSE.Common.RemoteJob; using Tango.FSE.Common.WindowsManager; using Tango.FSE.Common.DemoMode; using Tango.FSE.Common.FileAssociation; using Tango.FSE.Common.Insights; using Tango.FSE.Common.Build; using Tango.FSE.Common.Statistics; namespace Tango.FSE.Common { /// /// Represents a FSE view model base class. /// /// public abstract class FSEViewModel : ViewModel, INavigationBlocker { /// /// Gets the static observable entities adapter. /// public ObservablesStaticCollections Adapter { get { return ObservablesStaticCollections.Instance; } } /// /// Gets or sets the application manager. /// [TangoInject] public IFSEApplicationManager ApplicationManager { get; set; } private IAuthenticationProvider _authenticationProvider; /// /// Gets or sets the authentication provider. /// [TangoInject] public IAuthenticationProvider AuthenticationProvider { get { return _authenticationProvider; } set { _authenticationProvider = value; RaisePropertyChangedAuto(); if (_authenticationProvider != null) { _authenticationProvider.CurrentUserChanged += OnCurrentUserChanged; } } } /// /// Gets or sets the navigation manager. /// [TangoInject] public INavigationManager NavigationManager { get; set; } private INotificationProvider _notificationProvider; /// /// Gets or sets the notification provider. /// [TangoInject] public INotificationProvider NotificationProvider { get { return _notificationProvider; } set { _notificationProvider = value; RaisePropertyChangedAuto(); } } /// /// Gets or sets the machine provider. /// [TangoInject] public IMachineProvider MachineProvider { get; set; } /// /// Gets or sets the diagnostics provider. /// [TangoInject] public IDiagnosticsProvider DiagnosticsProvider { get; set; } /// /// Gets or sets the remote desktop provider. /// [TangoInject] public IRemoteDesktopProvider RemoteDesktopProvider { get; set; } /// /// Gets or sets the performance provider. /// [TangoInject] public IPerformanceProvider PerformanceProvider { get; set; } /// /// Gets or sets the system information provider. /// [TangoInject] public ISystemInfoProvider SystemInfoProvider { get; set; } /// /// Gets or sets the logging provider. /// [TangoInject] public ILoggingProvider LoggingProvider { get; set; } private IResolutionService _resolutionService; /// /// Gets or sets the resolution service. /// [TangoInject] public IResolutionService ResolutionService { get { return _resolutionService; } set { _resolutionService = value; RaisePropertyChangedAuto(); } } /// /// Gets or sets the file system provider. /// [TangoInject] public IFileSystemProvider FileSystemProvider { get; set; } /// /// Gets or sets the storage provider. /// [TangoInject] public IStorageProvider StorageProvider { get; set; } /// /// Gets or sets the machine updates provider. /// [TangoInject] public IMachineUpdatesProvider MachineUpdatesProvider { get; set; } /// /// Gets or sets the connectivity provider. /// [TangoInject] public IConnectivityProvider ConnectivityProvider { get; set; } /// /// Gets or sets the bug reporter. /// [TangoInject] public IBugReporter BugReporter { get; set; } /// /// Gets or sets the remote upgrade manager. /// [TangoInject] public IRemoteUpgradeManager RemoteUpgradeManager { get; set; } /// /// Gets or sets the firmware storage provider. /// [TangoInject] public IFirmwareStorageProvider FirmwareStorageProvider { get; set; } /// /// Gets or sets the UI dispatcher provider. /// [TangoInject] public IDispatcherProvider DispatcherProvider { get; set; } /// /// Gets or sets the events provider. /// [TangoInject] public IEventsProvider EventsProvider { get; set; } /// /// Gets or sets the remote job provider. /// [TangoInject] public IRemoteJobProvider RemoteJobProvider { get; set; } /// /// Gets or sets the dashboard manager. /// [TangoInject] public IDashboardManager DashboardManager { get; set; } /// /// Gets or sets the windows manager. /// [TangoInject] public IWindowsManager WindowsManager { get; set; } /// /// Gets or sets the demo mode manager. /// [TangoInject] public IDemoModeManager DemoModeManager { get; set; } /// /// Gets or sets the file association provider. /// [TangoInject] public IFileAssociationProvider FileAssociationProvider { get; set; } /// /// Gets or sets the insights provider. /// [TangoInject] public IInsightsProvider InsightsProvider { get; set; } /// /// Gets or sets the cryptography provider. /// [TangoInject] public ICryptographyProvider CryptographyProvider { get; set; } /// /// Gets or sets the current build provider. /// [TangoInject] public IBuildProvider BuildProvider { get; set; } /// /// Gets or sets the statistics data provider. /// [TangoInject] public IStatisticsProvider StatisticsProvider { get; set; } /// /// Gets or sets the FSE service. /// [TangoInject] public FSEServicesContainer Services { get; set; } /// /// Gets or sets the machines automatic complete provider. /// public AutoCompleteSource MachinesAutoCompleteProvider { get; set; } private FSESettings _settings; /// /// Gets the main FSE settings. /// public FSESettings Settings { get { if (_settings == null) { _settings = SettingsManager.Default.GetOrCreate(); } return _settings; } private set { _settings = value; } } /// /// Gets the current user. /// public User CurrentUser { get { return AuthenticationProvider.CurrentUser; } } private bool _isVisible; /// /// Gets or sets a value indicating whether this view is visible. /// public bool IsVisible { get { return _isVisible; } private set { _isVisible = value; RaisePropertyChangedAuto(); } } /// /// Initializes a new instance of the class. /// public FSEViewModel() { MachinesAutoCompleteProvider = new AutoCompleteSource(AutoCompleteMachines); } private List AutoCompleteMachines(string key) { key = key ?? String.Empty; try { return Services.MachinesService.GetAllMachines().Result.Where(x => x.SerialNumber.ToLower().StartsWith(key.ToLower())).Take(8).ToList(); } catch (Exception ex) { LogManager.Log(ex, "Error on auto complete machine filter."); return new List(); } } /// /// Called when the application has been started. /// public virtual void OnApplicationStarted() { } /// /// Called when the application is shutting down. /// public virtual void OnApplicationShuttingDown() { } /// /// Called when the navigation system has navigated to this VM view. /// public virtual void OnNavigatedTo() { IsVisible = true; } /// /// Called when the navigation system has navigated to this VM view. /// /// The view model instance of the previous view model public virtual void OnNavigatedTo(FSEViewModel fromVM) { } /// /// Called when the navigation system has navigated from this VM view. /// public virtual void OnNavigatedFrom() { IsVisible = false; } /// /// Called before the navigation system has navigated to this VM view. /// public virtual void OnBeforeNavigatedTo() { } /// /// Called before the navigation system has navigated from this VM view. /// public virtual void OnBeforeNavigatedFrom() { IsVisible = false; } /// /// Raises the specified message using the default . /// /// /// The message. protected void RaiseMessage(T message) where T : class { TangoMessenger.Default.Send(message); } /// /// Raises the specified message using the default . /// /// protected void RaiseMessage() where T : class { TangoMessenger.Default.Send(Activator.CreateInstance()); } /// /// Registers a message handle for the specified message type T. /// /// /// The handler. protected void RegisterForMessage(Action handler) { TangoMessenger.Default.Register(handler); } /// /// Called before the navigation system navigates from this object. /// Return false to abort the navigation. /// /// public virtual Task OnNavigateOutRequest() { return Task.FromResult(true); } /// /// Called before the navigation system navigates back from this object. /// Return false to abort the navigation. /// /// public virtual Task OnNavigateBackRequest() { return Task.FromResult(true); } /// /// Called when the application is ready and all modules views are loaded. /// public virtual void OnApplicationReady() { } /// /// Called when the current user has logged out of the application. /// public virtual Task OnApplicationLogout() { return Task.FromResult(true); } /// /// Raises the property changed event. /// /// protected override void RaisePropertyChangedAuto([CallerMemberName] string caller = null) { base.RaisePropertyChangedAuto(caller); } /// /// Raises the property changed event. /// /// Name of the property. protected override void RaisePropertyChanged(string propName) { base.RaisePropertyChanged(propName); if (propName == nameof(IsFree)) { if (IsFree) { Mouse.OverrideCursor = null; } else { Mouse.OverrideCursor = Cursors.AppStarting; } } } /// /// Called on . /// /// The sender. /// The user. protected virtual void OnCurrentUserChanged(object sender, User user) { InvalidateRelayCommands(); } } /// /// Represents an FSE view model with a auto ModuleSettings property. /// /// /// public abstract class FSEViewModelWithModuleSettings : FSEViewModel where T : SettingsBase { /// /// Gets or sets the module settings. /// public T ModuleSettings { get; private set; } /// /// Initializes a new instance of the class. /// public FSEViewModelWithModuleSettings() { ModuleSettings = SettingsManager.Default.GetOrCreate(); } } /// /// Represents a FSE view model base class a View property as an instance of a contract. /// /// /// public abstract class FSEViewModel : FSEViewModel where T : IFSEView { private T _view; /// /// Gets the IFSEView instance. /// [TangoInject(TangoInjectMode.WhenAvailable)] public T View { get { return _view; } set { _view = value; ViewAttached = true; OnViewAttached(); } } /// /// Gets a value indicating whether the instance of IFSEView is available. /// public bool ViewAttached { get; private set; } /// /// Called when the application has been started. /// public override void OnApplicationStarted() { } /// /// Called when the instance of IFSEView is available. /// public virtual void OnViewAttached() { } } public abstract class FSEViewModelWithModuleSettings : FSEViewModel where TView : IFSEView where TSettings : SettingsBase { /// /// Gets or sets the module settings. /// public TSettings ModuleSettings { get; private set; } /// /// Initializes a new instance of the class. /// public FSEViewModelWithModuleSettings() { ModuleSettings = SettingsManager.Default.GetOrCreate(); } } }