/// <summary> /// Moves cached items to new location /// </summary> /// <param name="asyncResources"></param> /// <param name="items"></param> /// <returns></returns> private async Task MoveCachedAsync(AsyncOperationResources <int> asyncResources, IEnumerable <IFileSystemItem> items = null) { var cachedItems = items ?? CachedItems; if (cachedItems == null) { _logger.Info("Cut called but no items where cached"); return; } int progress = 1; foreach (var fileSystemItem in CachedItems) { if (asyncResources.CancellationTokenSource.IsCancellationRequested) { return; } await Task.Run(() => Move(fileSystemItem, CurrentDirectory)).ConfigureAwait(false); asyncResources.Progress.Report(progress++); } // after move cached collection is not valid anymore CachedItems = null; }
public async Task MoveOrPasteAsync(AsyncOperationResources <int> asyncResources, IEnumerable <IFileSystemItem> items = null, bool inDepth = false) { if (_cacheToMove) { await MoveCachedAsync(asyncResources, items).ConfigureAwait(false); } else { await PasteAsync(asyncResources, items, inDepth).ConfigureAwait(false); } }
/// <summary> /// Pasts cached items to new location /// </summary> /// <param name="asyncResources"></param> /// <param name="fileSystemItemsToPast"></param> /// <param name="inDepth"></param> /// <returns></returns> private async Task PasteAsync(AsyncOperationResources <int> asyncResources, IEnumerable <IFileSystemItem> fileSystemItemsToPast = null, bool inDepth = false) { int progress = 1; var itemsToPast = fileSystemItemsToPast ?? CachedItems; var firstItem = itemsToPast.First(); var root = firstItem.FullName.Replace(firstItem.Name, ""); Queue <IFileSystemItem> stack = new Queue <IFileSystemItem>(itemsToPast); while (stack.Any()) { if (asyncResources.CancellationTokenSource.IsCancellationRequested) { return; } var fileSystemItem = stack.Dequeue(); var destinationPath = Path.Combine(CurrentDirectory, fileSystemItem.Path.Replace(root, "")); if (destinationPath == fileSystemItem.Path) { _logger.Info($"{destinationPath} file or directory exist in current directory"); //return; } if (fileSystemItem.IsDirectory) { if (inDepth) { foreach (var item in GetAllItemsUnderPath(fileSystemItem.Path)) { stack.Enqueue(item); } } try { await Task.Run(() => Directory.CreateDirectory(destinationPath)).ConfigureAwait(false); } catch (IOException exception) { _logger.Error("Error during directory creation", exception); throw new DirectoryCreationException(destinationPath, innerException: exception); } catch (Exception exception) { _logger.Error($"Given directory name: {destinationPath}, is invalid", exception); throw new InvalidPathException(destinationPath, innerException: exception); } } else { try { await Task.Run(() => File.Copy(fileSystemItem.Path, destinationPath, false)).ConfigureAwait(false); } catch (IOException exception) { _logger.Error("{}", exception); throw new FileCopyException(fileSystemItem.Path, destinationPath, innerException: exception); } catch (Exception exception) { _logger.Error($"Soruce path: {fileSystemItem.Path} or destination path: {destinationPath} is wrong", exception); throw new FileCopyException(fileSystemItem.Path, destinationPath, innerException: exception); } } asyncResources.Progress.Report(progress++); } }
private async void ResponseForUserActionAsync(ActionType action) { _cancellationTokenSource = new CancellationTokenSource(); AsyncOperationResources <int> asyncOperationResources = new AsyncOperationResources <int>(new Progress <int>(ReportProgress), _cancellationTokenSource); IDialogService dialogSrvice = DialogService; ; try { switch (action) { case ActionType.OpenFileSystemItem: LoadFileSystemItems(); break; case ActionType.Copy: _explorerModel.CacheSelectedItems(SelectedItems); break; case ActionType.MoveOrPaste: MessageBoxResult pastDecisionResult = MessageBoxResult.None; if (_explorerModel.IsAnyDirectoryCached) { pastDecisionResult = dialogSrvice.ShowDecisionMessage("", "", MessageBoxButton.YesNoCancel, DecisionType.DepthPaste).Result; } if (pastDecisionResult == MessageBoxResult.Yes) { dialogSrvice.ShowProgressMessage("", ""); await _explorerModel.MoveOrPasteAsync(asyncOperationResources, inDepth : true).ConfigureAwait(true); _messanger.Send(new AsyncOperationIndicatorMessage(true)); } else { dialogSrvice.ShowProgressMessage("", ""); await _explorerModel.MoveOrPasteAsync(asyncOperationResources).ConfigureAwait(true); _messanger.Send(new AsyncOperationIndicatorMessage(true)); } _messanger.Send(new ReloadFileSystemMessage(_explorerModel.CurrentDirectory)); break; case ActionType.Cut: _explorerModel.CacheSelectedItems(SelectedItems, true); break; case ActionType.Delete: var deleteDecisionResult = dialogSrvice.ShowDecisionMessage("", "", MessageBoxButton.YesNo, DecisionType.Delete).Result; if (deleteDecisionResult == MessageBoxResult.Yes) { _explorerModel.Delete(); _messanger.Send(new ReloadFileSystemMessage(_explorerModel.CurrentDirectory)); } break; case ActionType.Create: var createdResponse = dialogSrvice.ShowDecisionMessage("", "", MessageBoxButton.YesNo, DecisionType.Create); var newName = createdResponse.Data; _explorerModel.CreateDirectory(newName); _messanger.Send(new ReloadFileSystemMessage(_explorerModel.CurrentDirectory)); break; } } catch (FileSystemException exception) { _logger.Info($"Exception catched inside {MethodBase.GetCurrentMethod().Name}.", exception); _messanger.Send(new AsyncOperationIndicatorMessage(true)); DialogService.ShowError(exception.ToString(), Resources.Exception, MessageBoxButton.OK); } }