using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Tango.BL.Entities; using Tango.Core.Commands; using Tango.FSE.Common; using Tango.FSE.Common.AutoComplete; using Tango.FSE.Upgrade.Views; namespace Tango.FSE.Upgrade.ViewModels { public class WelcomeViewVM : RemoteUpgradeViewModel { private Machine _selectedMachine; /// /// Gets or sets the selected machine. /// public Machine SelectedMachine { get { return _selectedMachine; } set { _selectedMachine = value; RaisePropertyChangedAuto(); InvalidateRelayCommands(); } } private bool _isApplicationUpdate; /// /// Gets or sets a value indicating whether to update application and firmware, otherwise only firmware. /// public bool IsApplicationUpdate { get { return _isApplicationUpdate; } set { _isApplicationUpdate = value; RaisePropertyChangedAuto(); InvalidateRelayCommands(); } } private bool _isUpdateConnectedMachine; /// /// Gets or sets a value indicating whether to update the currently connected machine (relevant only on application update). /// public bool IsUpdateConnectedMachine { get { return _isUpdateConnectedMachine; } set { _isUpdateConnectedMachine = value; RaisePropertyChangedAuto(); InvalidateRelayCommands(); } } /// /// Gets or sets the start upgrade command. /// public RelayCommand StartUpgradeCommand { get; set; } /// /// Initializes a new instance of the class. /// public WelcomeViewVM() { IsApplicationUpdate = true; IsUpdateConnectedMachine = true; StartUpgradeCommand = new RelayCommand(StartUpgrade, () => !IsApplicationUpdate || IsUpdateConnectedMachine || SelectedMachine != null); } public override void OnApplicationStarted() { base.OnApplicationStarted(); MachineProvider.MachineConnected += (_, __) => { if (IsVisible) InvalidateIsUpdateConnectedMachine(); }; MachineProvider.MachineDisconnected += (_, __) => { if (IsVisible) InvalidateIsUpdateConnectedMachine(); }; } private void InvalidateIsUpdateConnectedMachine() { if (IsUpdateConnectedMachine) { IsUpdateConnectedMachine = MachineProvider.IsConnected && MachineProvider.ConnectionType.IsRemote(); } } /// /// Called before the navigation system has navigated to this VM view. /// public override void OnBeforeNavigatedTo() { base.OnBeforeNavigatedTo(); InvalidateIsUpdateConnectedMachine(); } private async void StartUpgrade() { if (IsApplicationUpdate) { await ModularNavigationManager.NavigateTo(Navigation.RemoteUpgradeView.ApplicationUpgradeView, new ApplicationUpgradeViewVM.NavigationObject() { ApplicationUpgradeMode = IsUpdateConnectedMachine ? ApplicationUpgradeViewVM.ApplicationUpgradeMode.ConnectedMachine : ApplicationUpgradeViewVM.ApplicationUpgradeMode.OtherMachine, SelectedMachine = IsUpdateConnectedMachine ? MachineProvider.Machine : SelectedMachine }); } else { await ModularNavigationManager.NavigateTo(Navigation.RemoteUpgradeView.FirmwareUpgradeView); } } } }