using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Data; using Tango.BL.Entities; using Tango.Core.Commands; using Tango.FSE.Common; using Tango.FSE.Firmware.Dialogs; using static Tango.SharedUI.Controls.NavigationControl; namespace Tango.FSE.Firmware.ViewModels { public class UpdatesViewVM : FSEViewModel, INavigationViewModel { private bool _loaded; private ICollectionView _updatesView; #region Properties private List _updates; /// /// Gets or sets the list of history software updates. /// public List Updates { get { return _updates; } set { _updates = value; RaisePropertyChangedAuto(); } } private String _filter; /// /// Gets or sets the search filter. /// public String Filter { get { return _filter; } set { _filter = value; RaisePropertyChangedAuto(); OnFilterChanged(); } } #endregion #region Commands /// /// Reloads the updates from the remote machine. /// public RelayCommand RefreshCommand { get; set; } /// /// Invokes a detailed dialog of the selected update. /// public RelayCommand OpenUpdateCommand { get; set; } #endregion #region Constructors /// /// Initializes a new instance of the class. /// public UpdatesViewVM() { OpenUpdateCommand = new RelayCommand(OpenUpdate); RefreshCommand = new RelayCommand(RefreshUpdates); } #endregion #region Override Methods public override void OnNavigatedTo() { base.OnNavigatedTo(); LoadFirmwareUpdates(); } public override void OnApplicationStarted() { base.OnApplicationStarted(); MachineProvider.MachineConnected += MachineProvider_MachineConnected; } #endregion #region Event Handlers private void MachineProvider_MachineConnected(object sender, Common.Connection.MachineConnectedEventArgs e) { _loaded = false; if (MachineProvider.ConnectionType.IsRemote()) { if (IsVisible) { InvokeUI(() => { LoadFirmwareUpdates(); }); } } } #endregion #region Private Methods private void RefreshUpdates() { _loaded = false; LoadFirmwareUpdates(); } private async void OpenUpdate(TangoUpdate update) { await NotificationProvider.ShowDialog(new FirmwareUpdateViewVM() { Update = update }); } private void OnFilterChanged() { _updatesView?.Refresh(); } private async void LoadFirmwareUpdates() { if (!MachineProvider.IsConnected || !MachineProvider.ConnectionType.IsRemote()) return; if (!_loaded) { try { IsFree = false; await Task.Delay(1000); var result = await MachineUpdatesProvider.GetUpdates(); Updates = result.Updates.Where(x => x.IsOfflineFirmwareUpgrade || x.IsUpdate || x.IsOfflineUpdate || x.IsSetup).ToList(); _filter = null; RaisePropertyChanged(nameof(Filter)); _updatesView = CollectionViewSource.GetDefaultView(Updates); _updatesView.Filter = OnUpdatesFilter; _loaded = true; } catch (Exception ex) { LogManager.Log(ex, "Error retrieving machine firmware updates."); NotificationProvider.PushErrorReportingSnackbar(ex, "Firmware Module Error", "Error occurred while trying to retrieve the machine firmware updates history."); } finally { IsFree = true; } } } private bool OnUpdatesFilter(object obj) { TangoUpdate update = obj as TangoUpdate; return String.IsNullOrWhiteSpace(Filter) || update.UpdateStatus.ToDescription().ToLower().Contains(Filter.ToLower()); } #endregion } }