diff options
Diffstat (limited to 'Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Storage/ViewModels')
| -rw-r--r-- | Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Storage/ViewModels/MainViewVM.cs | 151 |
1 files changed, 142 insertions, 9 deletions
diff --git a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Storage/ViewModels/MainViewVM.cs b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Storage/ViewModels/MainViewVM.cs index f28783590..1c6371f43 100644 --- a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Storage/ViewModels/MainViewVM.cs +++ b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Storage/ViewModels/MainViewVM.cs @@ -1,19 +1,28 @@ using System; using System.Collections.Generic; +using System.Collections.ObjectModel; +using System.Diagnostics; +using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using Tango.Core.Commands; +using Tango.Core.IO; using Tango.Integration.ExternalBridge; using Tango.Integration.Storage; using Tango.MachineStudio.Common; +using Tango.MachineStudio.Common.Notifications; using Tango.MachineStudio.Common.StudioApplication; +using Tango.MachineStudio.Storage.Models; namespace Tango.MachineStudio.Storage.ViewModels { public class MainViewVM : StudioViewModel { + public event EventHandler CurrentFolderChanged; + private IStudioApplicationManager _applicationManager; + private INotificationProvider _notification; private bool _machine_operator_changed = true; private StorageManager _storageManager; @@ -43,12 +52,38 @@ namespace Tango.MachineStudio.Storage.ViewModels public RelayCommand GoCommand { get; set; } - public MainViewVM(IStudioApplicationManager applicationManager) + public RelayCommand<StorageFileHandlerModel> CancelFileHandlerCommand { get; set; } + + public RelayCommand<StorageFileHandlerModel> OpenFileHandlerCommand { get; set; } + + public RelayCommand<StorageFileHandlerModel> RemoveFileHandlerCommand { get; set; } + + public ObservableCollection<StorageFileHandlerModel> FileHandlers { get; set; } + + public MainViewVM(IStudioApplicationManager applicationManager, INotificationProvider notificationProvider) { _applicationManager = applicationManager; + _notification = notificationProvider; _applicationManager.ConnectedMachineChanged += _applicationManager_ConnectedMachineChanged; - GoCommand = new RelayCommand(NavigateToCurrentPath); + FileHandlers = new ObservableCollection<StorageFileHandlerModel>(); + + GoCommand = new RelayCommand(() => NavigateToPath(CurrentPath), () => StorageManager != null); + BackCommand = new RelayCommand(NavigateBack, () => StorageManager != null && StorageManager.CurrentFolder.Parent != null); + CancelFileHandlerCommand = new RelayCommand<StorageFileHandlerModel>(CancelFileHandler); + OpenFileHandlerCommand = new RelayCommand<StorageFileHandlerModel>(OpenFileHandler); + RefreshCommand = new RelayCommand(Refresh, () => StorageManager != null); + RemoveFileHandlerCommand = new RelayCommand<StorageFileHandlerModel>(RemoveFileHandler); + } + + private void OpenFileHandler(StorageFileHandlerModel handler) + { + ShowInExplorer(handler.FilePath); + } + + private async void CancelFileHandler(StorageFileHandlerModel handler) + { + await handler.Handler.Cancel(); } private void _applicationManager_ConnectedMachineChanged(object sender, IExternalBridgeClient e) @@ -63,7 +98,7 @@ namespace Tango.MachineStudio.Storage.ViewModels public override void OnApplicationReady() { - + } public override void OnNavigatedTo() @@ -77,19 +112,117 @@ namespace Tango.MachineStudio.Storage.ViewModels } } - private async void NavigateToCurrentPath() + private async void NavigateToPath(String path) { - await StorageManager.GetFolder(CurrentPath); + IsFree = false; + + try + { + await StorageManager.GetFolder(path); + CurrentPath = StorageManager.CurrentPath; + CurrentFolderChanged?.Invoke(this, new EventArgs()); + } + catch (Exception ex) + { + _notification.ShowError($"Error navigating to the specified path.\n{ex.Message}"); + } + + IsFree = true; + + InvalidateRelayCommands(); } private async void Initialize() { if (_applicationManager.ConnectedMachine != null) { - StorageManager = _applicationManager.ConnectedMachine.CreateStorageManager(); - await StorageManager.GetStorageDrive(); - await StorageManager.GetRootFolder(); - CurrentPath = StorageManager.StorageDrive.Root; + try + { + StorageManager = _applicationManager.ConnectedMachine.CreateStorageManager(); + await StorageManager.GetStorageDrive(); + await StorageManager.GetRootFolder(); + CurrentPath = StorageManager.StorageDrive.Root; + } + catch (Exception ex) + { + _notification.ShowError($"An error occurred while trying to initialize the storage manager.\n{ex.Message}"); + } + + InvalidateRelayCommands(); + } + } + + public void OnStorageItemDoubleClicked(StorageItem storageItem) + { + if (storageItem is StorageFolder && storageItem != null) + { + NavigateToPath(storageItem.Path); + } + else + { + DownloadStorageItem(storageItem as StorageFile); + } + } + + private void NavigateBack() + { + NavigateToPath(StorageManager.CurrentFolder.Parent); + } + + private void Refresh() + { + NavigateToPath(StorageManager.CurrentFolder.Path); + } + + private async void DownloadStorageItem(StorageFile storageFile) + { + var downloadsFolder = KnownFolders.GetPath(KnownFolder.Downloads); + var file = Path.Combine(downloadsFolder, storageFile.Name); + + FileStream fs = new FileStream(Path.Combine(downloadsFolder, storageFile.Name), FileMode.Create); + var handler = await StorageManager.DownloadFile(storageFile, fs); + handler.Completed += (_, __) => + { + fs.Dispose(); + }; + + handler.Canceled += (_, __) => + { + fs.Dispose(); + File.Delete(file); + }; + + handler.Failed += (_, __) => + { + fs.Dispose(); + File.Delete(file); + }; + + FileHandlers.Insert(0, new StorageFileHandlerModel(handler, file, StorageFileHandlerType.Download)); + } + + /// <summary> + /// Shows the file in explorer. + /// </summary> + /// <param name="path">Name of the file/folder.</param> + public static void ShowInExplorer(String path) + { + Process.Start("explorer.exe", string.Format("/select,\"{0}\"", path)); + } + + private void RemoveFileHandler(StorageFileHandlerModel handler) + { + if (handler.Handler.Status == StorageFileHandlerStatus.Active) + { + if (_notification.ShowQuestion("Are you sure you want to cancel this file operation?")) + { + handler.Handler.Cancel(); + FileHandlers.Remove(handler); + } + } + else + { + FileHandlers.Remove(handler); } } } |
