From 9e6d1ddfb42c4e8357bd75c2b1d6f84df1ea1966 Mon Sep 17 00:00:00 2001 From: Roy Ben-Shabat Date: Thu, 29 Nov 2018 13:15:09 +0200 Subject: Working on machine studio storage module. --- .../Models/StorageFileHandlerModel.cs | 26 ++ .../Models/StorageFileHandlerType.cs | 14 + .../Tango.MachineStudio.Storage.csproj | 2 + .../ViewModels/MainViewVM.cs | 151 +++++++++- .../Views/MainView.xaml | 306 +++++++++++++++++---- .../Views/MainView.xaml.cs | 21 ++ 6 files changed, 452 insertions(+), 68 deletions(-) create mode 100644 Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Storage/Models/StorageFileHandlerModel.cs create mode 100644 Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Storage/Models/StorageFileHandlerType.cs (limited to 'Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Storage') diff --git a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Storage/Models/StorageFileHandlerModel.cs b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Storage/Models/StorageFileHandlerModel.cs new file mode 100644 index 000000000..764512e77 --- /dev/null +++ b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Storage/Models/StorageFileHandlerModel.cs @@ -0,0 +1,26 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Tango.Core; +using Tango.Integration.Storage; + +namespace Tango.MachineStudio.Storage.Models +{ + public class StorageFileHandlerModel : ExtendedObject + { + public StorageFileHandler Handler { get; set; } + + public StorageFileHandlerType Type { get; set; } + + public String FilePath { get; set; } + + public StorageFileHandlerModel(StorageFileHandler handler, String filePath, StorageFileHandlerType type) + { + FilePath = filePath; + Handler = handler; + Type = type; + } + } +} diff --git a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Storage/Models/StorageFileHandlerType.cs b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Storage/Models/StorageFileHandlerType.cs new file mode 100644 index 000000000..548b0fc86 --- /dev/null +++ b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Storage/Models/StorageFileHandlerType.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Tango.MachineStudio.Storage.Models +{ + public enum StorageFileHandlerType + { + Download, + Upload + } +} diff --git a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Storage/Tango.MachineStudio.Storage.csproj b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Storage/Tango.MachineStudio.Storage.csproj index 0ce2a1cb4..991e9fa2e 100644 --- a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Storage/Tango.MachineStudio.Storage.csproj +++ b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Storage/Tango.MachineStudio.Storage.csproj @@ -66,6 +66,8 @@ + + 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 CancelFileHandlerCommand { get; set; } + + public RelayCommand OpenFileHandlerCommand { get; set; } + + public RelayCommand RemoveFileHandlerCommand { get; set; } + + public ObservableCollection FileHandlers { get; set; } + + public MainViewVM(IStudioApplicationManager applicationManager, INotificationProvider notificationProvider) { _applicationManager = applicationManager; + _notification = notificationProvider; _applicationManager.ConnectedMachineChanged += _applicationManager_ConnectedMachineChanged; - GoCommand = new RelayCommand(NavigateToCurrentPath); + FileHandlers = new ObservableCollection(); + + GoCommand = new RelayCommand(() => NavigateToPath(CurrentPath), () => StorageManager != null); + BackCommand = new RelayCommand(NavigateBack, () => StorageManager != null && StorageManager.CurrentFolder.Parent != null); + CancelFileHandlerCommand = new RelayCommand(CancelFileHandler); + OpenFileHandlerCommand = new RelayCommand(OpenFileHandler); + RefreshCommand = new RelayCommand(Refresh, () => StorageManager != null); + RemoveFileHandlerCommand = new RelayCommand(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)); + } + + /// + /// Shows the file in explorer. + /// + /// Name of the file/folder. + 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); } } } diff --git a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Storage/Views/MainView.xaml b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Storage/Views/MainView.xaml index 46ef31c6b..a4a5c2946 100644 --- a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Storage/Views/MainView.xaml +++ b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Storage/Views/MainView.xaml @@ -11,22 +11,76 @@ xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes" xmlns:local="clr-namespace:Tango.MachineStudio.Storage.Views" mc:Ignorable="d" - d:DesignHeight="1080" d:DesignWidth="1920" Background="Transparent" d:DataContext="{d:DesignInstance Type=vm:MainViewVM, IsDesignTimeCreatable=False}" DataContext="{x:Static global:ViewModelLocator.MainViewVM}"> + d:DesignHeight="1080" x:Name="control" d:DesignWidth="1920" Background="Transparent" d:DataContext="{d:DesignInstance Type=vm:MainViewVM, IsDesignTimeCreatable=False}" DataContext="{x:Static global:ViewModelLocator.MainViewVM}"> - + - + + + + - ACTIONS + ACTIONS - + + + + + + + + + + + + @@ -34,13 +88,16 @@ + + + - - @@ -55,21 +112,21 @@ Capacity: - + Free Space: - + - - + @@ -81,54 +138,185 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + FILE TRANSFERS + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Storage/Views/MainView.xaml.cs b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Storage/Views/MainView.xaml.cs index be7e82ca5..5da808b8c 100644 --- a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Storage/Views/MainView.xaml.cs +++ b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Storage/Views/MainView.xaml.cs @@ -12,6 +12,8 @@ using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; +using Tango.Integration.Storage; +using Tango.MachineStudio.Storage.ViewModels; namespace Tango.MachineStudio.Storage.Views { @@ -20,9 +22,28 @@ namespace Tango.MachineStudio.Storage.Views /// public partial class MainView : UserControl { + private MainViewVM _vm; + public MainView() { InitializeComponent(); + Loaded += (_, __) => + { + _vm = DataContext as MainViewVM; + _vm.CurrentFolderChanged += _vm_CurrentFolderChanged; + }; + + } + + private void _vm_CurrentFolderChanged(object sender, EventArgs e) + { + ScrollViewer scrollViewer = gridStorageItems.FindChild(); + scrollViewer.ScrollToVerticalOffset(0); + } + + private void DataGrid_MouseDoubleClick(object sender, MouseButtonEventArgs e) + { + _vm.OnStorageItemDoubleClicked(gridStorageItems.SelectedItem as StorageItem); } } } -- cgit v1.3.1