using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Input; using Tango.Core.Commands; using Tango.FileSystem; using Tango.FSE.Common; using Tango.FSE.Common.Connection; using static Tango.SharedUI.Controls.NavigationControl; namespace Tango.FSE.PPCConsole.ViewModels { public class FileSystemViewVM : FSEViewModel, INavigationViewModel { private FileSystemItem _currentItem; public FileSystemItem CurrentItem { get { return _currentItem; } set { _currentItem = value; RaisePropertyChangedAuto(); OnCurrentItemChanged(); } } private String _currentPath; public String CurrentPath { get { return _currentPath; } set { _currentPath = value; RaisePropertyChangedAuto(); } } public RelayCommand NavigateCommand { get; set; } public RelayCommand OpenItemCommand { get; set; } public RelayCommand BackCommand { get; set; } public FileSystemViewVM() { NavigateCommand = new RelayCommand(NavigateToCurrentPath); OpenItemCommand = new RelayCommand(OpenFileSystemItem); BackCommand = new RelayCommand(NavigateBack, () => !(CurrentItem is FolderItem) || !(CurrentItem as FolderItem).IsRoot); } private async void NavigateBack() { if (CurrentItem.Path.Length == 3) { await Navigate(null); } else { String parent = Path.GetDirectoryName(CurrentItem.Path); await Navigate(parent); } } public override void OnApplicationStarted() { base.OnApplicationStarted(); MachineProvider.MachineConnected += MachineProvider_MachineConnected; } public override void OnApplicationReady() { base.OnApplicationReady(); } private async void MachineProvider_MachineConnected(object sender, MachineConnectedEventArgs e) { await Navigate(null); } private async void NavigateToCurrentPath() { await Navigate(CurrentPath); } private async void OpenFileSystemItem(FileSystemItem item) { if (item != null) { await Navigate(item.Path); } } private async Task Navigate(String path) { try { IsFree = false; Mouse.OverrideCursor = Cursors.AppStarting; if (path != null) { CurrentItem = await FileSystemProvider.GetFolder(path) as FileSystemItem; } else { CurrentItem = await FileSystemProvider.GetThisPC() as FileSystemItem; } } catch (Exception ex) { IsFree = true; Mouse.OverrideCursor = null; await NotificationProvider.ShowError($"Error navigating to the specified path.\n{ex.FlattenMessage()}"); } finally { IsFree = true; Mouse.OverrideCursor = null; } } private void OnCurrentItemChanged() { CurrentPath = CurrentItem.Path; InvalidateRelayCommands(); } } }