using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Threading; using Tango.Core.Commands; using Tango.Core.DI; using Tango.Integration.Operation; using Tango.PPC.Common; using Tango.PPC.Common.Modules; using Tango.PPC.Common.Navigation; using Tango.PPC.UI.Views; using Tango.PPC.UI.ViewsContracts; using Tango.SharedUI; namespace Tango.PPC.UI.ViewModels { /// /// Represents the layout view containing the main menu and all modules. /// /// public class LayoutViewVM : PPCViewModel { private JobHandler _jobHandler; private bool _resettingDevice; /// /// Gets or sets the module loader. /// [TangoInject] public IPPCModuleLoader ModuleLoader { get; set; } #region Properties private bool _isMenuOpened; /// /// Gets or sets a value indicating whether the side menu is opened. /// public bool IsMenuOpened { get { return _isMenuOpened; } set { _isMenuOpened = value; RaisePropertyChangedAuto(); if (!_isMenuOpened) { IsPowerOpened = false; } } } private bool _isNotificationsOpened; /// /// Gets or sets a value indicating whether to display all notifications. /// public bool IsNotificationsOpened { get { return _isNotificationsOpened; } set { _isNotificationsOpened = value; RaisePropertyChangedAuto(); } } private bool _isPowerOpened; /// /// Gets or sets a value indicating whether the power area is opened. /// public bool IsPowerOpened { get { return _isPowerOpened; } set { _isPowerOpened = value; RaisePropertyChangedAuto(); } } #endregion #region Commands /// /// Gets or sets the module navigation command. /// public RelayCommand ModuleNavigationCommand { get; set; } /// /// Gets or sets the menu or back command. /// public RelayCommand MenuOrBackCommand { get; set; } /// /// Gets or sets the home command. /// public RelayCommand HomeCommand { get; set; } /// /// Gets or sets the notifications area pressed command. /// public RelayCommand NotificationsAreaPressedCommand { get; set; } /// /// Gets or sets the stop printing command. /// public RelayCommand StopPrintingCommand { get; set; } /// /// Gets or sets the sign-out command. /// public RelayCommand SignOutCommand { get; set; } /// /// Gets or sets the update command. /// public RelayCommand UpdateCommand { get; set; } /// /// Gets or sets the power command. /// public RelayCommand PowerCommand { get; set; } /// /// Gets or sets the restart application command. /// public RelayCommand RestartApplicationCommand { get; set; } /// /// Gets or sets the power off command. /// public RelayCommand PowerOffCommand { get; set; } /// /// Gets or sets the reset command. /// public RelayCommand ResetCommand { get; set; } #endregion #region Constructors /// /// Initializes a new instance of the class. /// public LayoutViewVM() { ModuleNavigationCommand = new RelayCommand(NavigateToModule); HomeCommand = new RelayCommand(NavigateHome); MenuOrBackCommand = new RelayCommand(OpenMenuOrNavigateBack); NotificationsAreaPressedCommand = new RelayCommand(OpenFirstNotificationOrDisplayAll); StopPrintingCommand = new RelayCommand(StopPrinting); SignOutCommand = new RelayCommand(SignOut); UpdateCommand = new RelayCommand(() => { NavigationManager.NavigateTo(NavigationView.MachineUpdateView); TangoIOC.Default.GetInstance().CheckForUpdates(); IsMenuOpened = false; }); PowerCommand = new RelayCommand(() => IsPowerOpened = true); RestartApplicationCommand = new RelayCommand(RestartApplication); PowerOffCommand = new RelayCommand(PowerOffMachine, () => MachineProvider.MachineOperator.Status != MachineStatuses.Disconnected); ResetCommand = new RelayCommand(ResetMachine, () => MachineProvider.MachineOperator.Status != MachineStatuses.Disconnected); } #endregion #region Private Methods /// /// Stops the printing. /// private void StopPrinting() { LogManager.Log("Stop printing layout command pressed!"); if (_jobHandler != null) { _jobHandler.Cancel(); } } /// /// Opens the menu or navigate back. /// private void OpenMenuOrNavigateBack() { if (NavigationManager.CanNavigateBack) { LogManager.Log("Back command pressed."); NavigationManager.NavigateBack(); } else { LogManager.Log("Menu command pressed."); IsMenuOpened = true; } } /// /// Handles the module navigation command. /// /// Name of the module. private void NavigateToModule(string moduleName) { LogManager.Log("Navigate to module command pressed."); IsMenuOpened = false; NavigationManager.NavigateTo(moduleName); } /// /// Handles the home command. /// private void NavigateHome() { LogManager.Log("Navigate home command pressed."); IsMenuOpened = false; NavigationManager.NavigateTo(NavigationView.HomeModule); } /// /// Represents an event that is raised when the sign-out operation is complete. /// private void SignOut() { LogManager.Log("SignOut command pressed."); AuthenticationProvider.LogOut(); IsMenuOpened = false; } /// /// Opens the first notification or display all. /// private void OpenFirstNotificationOrDisplayAll() { if (NotificationProvider.NotificationItems.Count == 1) { //Open first } else { IsNotificationsOpened = true; } } /// /// Restarts the application. /// private async void RestartApplication() { if (await NotificationProvider.ShowQuestion("Are you sure you want to restart the application?")) { ApplicationManager.Restart(); } } /// /// Powers off the machine. /// private async void PowerOffMachine() { IsMenuOpened = false; if (await NotificationProvider.ShowQuestion("Are you sure you wish to turn off the machine?")) { try { await MachineProvider.MachineOperator.PowerDown(); } catch (Exception ex) { LogManager.Log(ex, "Error triggering power down."); await NotificationProvider.ShowError(ex.FlattenMessage()); } } } /// /// Resets the machine. /// private async void ResetMachine() { IsMenuOpened = false; if (!await NotificationProvider.ShowQuestion("Are you sure you want to reset the machine?")) return; try { _resettingDevice = true; ResetCommand.RaiseCanExecuteChanged(); await MachineProvider.MachineOperator.Reset(); await NotificationProvider.ShowInfo("Machine was successfully restarted."); } catch (Exception ex) { await NotificationProvider.ShowError(ex.FlattenMessage()); } finally { _resettingDevice = false; ResetCommand.RaiseCanExecuteChanged(); } } #endregion #region Override Methods /// /// Called when the application has been started. /// public override void OnApplicationStarted() { base.OnApplicationStarted(); MachineProvider.MachineOperator.PrintingStarted += MachineOperator_PrintingStarted; } /// /// Called when the instance of IPPCView is available. /// public override void OnViewAttached() { } public override void OnApplicationReady() { base.OnApplicationReady(); MachineProvider.MachineOperator.StatusChanged += MachineOperator_StatusChanged; } private void MachineOperator_StatusChanged(object sender, MachineStatuses e) { InvokeUI(() => { PowerOffCommand.RaiseCanExecuteChanged(); ResetCommand.RaiseCanExecuteChanged(); }); } #endregion #region Public Methods /// /// Toggles the application technician mode. /// public void ToggleTechnicianMode() { if (!ApplicationManager.IsInTechnicianMode) { ApplicationManager.EnterTechnicianMode(); } else { ApplicationManager.ExitTechnicianMode(); } } #endregion #region Event Handlers /// /// Handles the PrintingStarted event of the MachineOperator. /// /// The source of the event. /// The instance containing the event data. private void MachineOperator_PrintingStarted(object sender, PrintingEventArgs e) { _jobHandler = e.JobHandler; } #endregion } }