using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
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.PPCConsole.Dialogs;
using Tango.PPC.Shared.Updates;
using static Tango.SharedUI.Controls.NavigationControl;
namespace Tango.FSE.PPCConsole.ViewModels
{
///
/// Represents the remote system software/firmware updates view model.
///
///
///
public class UpdatesViewVM : FSEViewModel, INavigationViewModel
{
private bool _loaded;
private ICollectionView _updatesView;
private ICollectionView _packagesView;
#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 List _packages;
///
/// Gets or sets the history of installed packages.
///
public List Packages
{
get { return _packages; }
set { _packages = 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();
LoadMachineUpdates();
}
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(() =>
{
LoadMachineUpdates();
});
}
}
}
#endregion
#region Private Methods
private void RefreshUpdates()
{
_loaded = false;
LoadMachineUpdates();
}
private async void OpenUpdate(TangoUpdate update)
{
await NotificationProvider.ShowDialog(new MachineUpdateViewVM() { Update = update });
}
private void OnFilterChanged()
{
_updatesView?.Refresh();
_packagesView?.Refresh();
}
private async void LoadMachineUpdates()
{
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).ToList();
Packages = result.Packages.ToList();
_filter = null;
RaisePropertyChanged(nameof(Filter));
_updatesView = CollectionViewSource.GetDefaultView(Updates);
_updatesView.Filter = OnUpdatesFilter;
_packagesView = CollectionViewSource.GetDefaultView(Packages);
_packagesView.Filter = OnPackagesFilter;
_loaded = true;
}
catch (Exception ex)
{
LogManager.Log(ex, "Error retrieving machine updates.");
NotificationProvider.PushErrorReportingSnackbar(ex, "PPC Module Error", "Error occurred while trying to retrieve the machine software updates history.");
}
finally
{
IsFree = true;
}
}
}
private bool OnPackagesFilter(object obj)
{
PackageInstallation package = obj as PackageInstallation;
return String.IsNullOrWhiteSpace(Filter) || package.PackageName.ToLower().Contains(Filter.ToLower());
}
private bool OnUpdatesFilter(object obj)
{
TangoUpdate update = obj as TangoUpdate;
return String.IsNullOrWhiteSpace(Filter) || update.UpdateStatus.ToDescription().ToLower().Contains(Filter.ToLower());
}
#endregion
}
}