using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using Tango.Core.Commands; using Tango.Explorer; using Tango.PPC.Common; using Tango.PPC.Common.Navigation; using Tango.PPC.Storage.Models; using Tango.PPC.Storage.TaskBarItems; using Tango.PPC.Storage.ViewContracts; namespace Tango.PPC.Storage.ViewModels { /// /// Represents the main view VM and entry point for . /// /// public class MainViewVM : PPCViewModel, INavigationResultProvider { private bool _allow_exit; private ExplorerFileItem _selectedItem; private String _currentPath; public String CurrentPath { get { return _currentPath; } set { _currentPath = value; RaisePropertyChangedAuto(); } } private StorageNavigationRequest _request; public StorageNavigationRequest Request { get { return _request; } set { _request = value; RaisePropertyChangedAuto(); if (_request != null) { FileName = _request.DefaultFileName; } } } private String _fileName; public String FileName { get { return _fileName; } set { if (_fileName != value) { _fileName = value; OnFileNameChanged(); } RaisePropertyChangedAuto(); SaveCommand.RaiseCanExecuteChanged(); } } private bool _displayItems; public bool DisplayItems { get { return _displayItems; } set { _displayItems = value; RaisePropertyChangedAuto(); } } public RelayCommand FileSelectedCommand { get; set; } public ObservableCollection SelectedItems { get; set; } public RelayCommand SaveCommand { get; set; } public RelayCommand OpenCommand { get; set; } public RelayCommand OpenDirectoryCommand { get; set; } public MainViewVM() { SelectedItems = new ObservableCollection(); FileSelectedCommand = new RelayCommand(OnFileSelected); SaveCommand = new RelayCommand(OnSaveCommand, (x) => !String.IsNullOrWhiteSpace(FileName) || Request.Intent == StorageNavigationIntent.SaveFiles); Request = new StorageNavigationRequest() { Intent = StorageNavigationIntent.LoadFiles }; OpenCommand = new RelayCommand(OnOpenCommand, () => Request.Intent == StorageNavigationIntent.LoadFiles); OpenDirectoryCommand = new RelayCommand(OnOpenFileDialogCommand); } public override void OnApplicationStarted() { } public override void OnApplicationReady() { base.OnApplicationReady(); NotificationProvider.PushTaskBarItem(); StorageProvider.StorageConnected += StorageProvider_StorageConnected; StorageProvider.StorageDisconnected += StorageProvider_StorageDisconnected; } public override async void OnNavigatedTo() { base.OnNavigatedTo(); _allow_exit = false; _selectedItem = null; if (BuildProvider.IsEureka) { if (Settings.StorageRootPath == null) { Settings.StorageRootPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "Twine", "Storage"); Directory.CreateDirectory(Settings.StorageRootPath); } } if ((StorageProvider.IsConnected && StorageProvider.Drive != null) || Settings.StorageRootPath != null) { CurrentPath = null; if (Settings.StorageRootPath != null && Directory.Exists(Settings.StorageRootPath)) { CurrentPath = Settings.StorageRootPath; } else { CurrentPath = StorageProvider.Drive.RootDirectory.FullName; } if (Request.Intent == StorageNavigationIntent.SaveFile) { View.EditFileName(); } DisplayItems = true; } else { _allow_exit = true; await NotificationProvider.ShowError("No storage device connected."); await NavigationManager.NavigateBack(); } } public override void OnNavigatedFrom() { base.OnNavigatedFrom(); DisplayItems = false; Request = null; Request = new StorageNavigationRequest() { Intent = StorageNavigationIntent.LoadFiles }; } /// /// Handles the storage connected event. /// /// The sender. /// The drive info. private void StorageProvider_StorageConnected(object sender, System.IO.DriveInfo e) { if (!BuildProvider.IsEureka) { InvokeUI(async () => { if (await NotificationProvider.ShowQuestion("Storage device inserted. Do you want to browse?")) { await NavigationManager.NavigateTo(); } }); } } /// /// Handles the storage disconnected event. /// /// The sender. /// The drive info. private void StorageProvider_StorageDisconnected(object sender, System.IO.DriveInfo e) { if (!BuildProvider.IsEureka) { InvokeUI(async () => { if (IsVisible) { _allow_exit = true; await NotificationProvider.ShowError("Storage device disconnected."); await NavigationManager.NavigateBack(); CurrentPath = null; } }); } } public override Task OnNavigateBackRequest() { if (_allow_exit || (Settings.StorageRootPath != null && Settings.StorageRootPath == CurrentPath) || (StorageProvider.Drive != null && StorageProvider.Drive.RootDirectory != null && CurrentPath == StorageProvider.Drive.RootDirectory.FullName)) { return Task.FromResult(true); } else { View.NavigateBack(); return Task.FromResult(false); } } private async void OnFileSelected(ExplorerFileItem fileItem) { _selectedItem = fileItem; _allow_exit = true; await NavigationManager.NavigateBack(); StorageProvider.SubmitFileSelection(new List() { fileItem }); } private async void OnOpenCommand() { _allow_exit = true; await NavigationManager.NavigateBack(); StorageProvider.SubmitFileSelection(SelectedItems.ToList()); } public ExplorerFileItem GetNavigationResult() { return _selectedItem; } public void OnNavigationObjectReceived(StorageNavigationRequest request) { Request = request; } private void OnSaveCommand() { _allow_exit = true; if (Request.Intent == StorageNavigationIntent.SaveFile) { _selectedItem = new ExplorerFileItem() { Path = CurrentPath + "\\" + FileName, }; } else if (Request.Intent == StorageNavigationIntent.SaveFiles) { _selectedItem = new ExplorerFileItem() { Path = CurrentPath, }; } NavigationManager.NavigateBack(); } private void OnFileNameChanged() { String text = FileName; if (text != null) { text = text.ToValidFileName(); _fileName = text; RaisePropertyChanged(nameof(FileName)); } } private void OnOpenFileDialogCommand() { if(Directory.Exists(CurrentPath) ) { Microsoft.Win32.OpenFileDialog openFileDialog = new Microsoft.Win32.OpenFileDialog { InitialDirectory = CurrentPath, Title = "Browse Job Files", CheckFileExists = true, CheckPathExists = true, DefaultExt = "job", Filter = "job files (*.job)|*.job", ReadOnlyChecked = true, ShowReadOnly = true }; if (openFileDialog.ShowDialog().Value) { StorageProvider.SubmitFileSelection(new List() { ExplorerFileItem.LoadFromPath(openFileDialog.FileName) }); } } } } }