public async Task <ReturnResult> Undo(IStorageHistory history) { ReturnResult returnStatus = ReturnResult.InProgress; Progress <FileSystemStatusCode> errorCode = new Progress <FileSystemStatusCode>(); errorCode.ProgressChanged += (s, e) => returnStatus = e.ToStatus(); switch (history.OperationType) { case FileOperationType.CreateNew: // CreateNew PASS { // Opposite: Delete created items if (IsHistoryNull(history.Source)) { break; } return(await filesystemHelpers.DeleteItemsAsync(history.Source, false, true, false)); } case FileOperationType.Rename: // Rename PASS { // Opposite: Restore original item names if (IsHistoryNull(history)) { break; } NameCollisionOption collision = NameCollisionOption.GenerateUniqueName; for (int i = 0; i < history.Destination.Count(); i++) { await filesystemOperations.RenameAsync( history.Destination.ElementAt(i), Path.GetFileName(history.Source.ElementAt(i).Path), collision, errorCode, cancellationToken); } break; } case FileOperationType.Copy: // Copy PASS { // Opposite: Delete copied items if (IsHistoryNull(history.Destination)) { break; } return(await filesystemHelpers.DeleteItemsAsync(history.Destination, false, true, false)); } case FileOperationType.Move: // Move PASS { // Opposite: Move the items to original directory if (IsHistoryNull(history)) { break; } return(await filesystemHelpers.MoveItemsAsync(history.Destination, history.Source.Select((item) => item.Path), false, false)); } case FileOperationType.Extract: // Extract PASS { // Opposite: No opposite for archive extraction returnStatus = ReturnResult.Success; Debugger.Break(); break; } case FileOperationType.Recycle: // Recycle PASS { // Opposite: Restore recycled items if (IsHistoryNull(history)) { break; } for (int i = 0; i < history.Destination.Count(); i++) { returnStatus = await filesystemHelpers.RestoreFromTrashAsync( history.Destination.ElementAt(i), history.Source.ElementAt(i).Path, false); } if (returnStatus == ReturnResult.IntegrityCheckFailed) // Not found, corrupted { App.HistoryWrapper.RemoveHistory(history, false); } break; } case FileOperationType.Restore: // Restore PASS { // Opposite: Move restored items to Recycle Bin if (IsHistoryNull(history.Destination)) { break; } List <IStorageHistory> rawStorageHistory = new List <IStorageHistory>(); for (int i = 0; i < history.Destination.Count(); i++) { rawStorageHistory.Add(await filesystemOperations.DeleteAsync( history.Destination.ElementAt(i), null, errorCode, false, cancellationToken)); } if (rawStorageHistory.TrueForAll((item) => item != null)) { IStorageHistory newHistory = new StorageHistory( FileOperationType.Restore, rawStorageHistory.SelectMany((item) => item?.Destination).ToList(), rawStorageHistory.SelectMany((item) => item?.Source).ToList()); // We need to change the recycled item paths (since IDs are different) - for Redo() to work App.HistoryWrapper.ModifyCurrentHistory(newHistory); } else { App.HistoryWrapper.RemoveHistory(history, false); } break; } case FileOperationType.Delete: // Delete PASS { // Opposite: No opposite for pernament deletion returnStatus = ReturnResult.Success; break; } } return(returnStatus); }
public async Task <ReturnResult> Undo(IStorageHistory history) { ReturnResult returnStatus = ReturnResult.InProgress; Progress <FileSystemStatusCode> errorCode = new(); errorCode.ProgressChanged += (s, e) => returnStatus = e.ToStatus(); switch (history.OperationType) { case FileOperationType.CreateNew: // Opposite: Delete created items if (!IsHistoryNull(history.Source)) { return(await helpers.DeleteItemsAsync(history.Source, false, true, false)); } break; case FileOperationType.CreateLink: // Opposite: Delete created items if (!IsHistoryNull(history.Destination)) { return(await helpers.DeleteItemsAsync(history.Destination, false, true, false)); } break; case FileOperationType.Rename: // Opposite: Restore original item names if (!IsHistoryNull(history)) { NameCollisionOption collision = NameCollisionOption.GenerateUniqueName; for (int i = 0; i < history.Destination.Count(); i++) { string name = Path.GetFileName(history.Source[i].Path); await operations.RenameAsync(history.Destination[i], name, collision, errorCode, cancellationToken); } } break; case FileOperationType.Copy: // Opposite: Delete copied items if (!IsHistoryNull(history.Destination)) { return(await helpers.DeleteItemsAsync(history.Destination, false, true, false)); } break; case FileOperationType.Move: // Opposite: Move the items to original directory if (!IsHistoryNull(history)) { return(await helpers.MoveItemsAsync(history.Destination, history.Source.Select(item => item.Path), false, false)); } break; case FileOperationType.Extract: // Opposite: No opposite for archive extraction returnStatus = ReturnResult.Success; Debugger.Break(); break; case FileOperationType.Recycle: // Opposite: Restore recycled items if (!IsHistoryNull(history)) { returnStatus = await helpers.RestoreItemsFromTrashAsync(history.Destination, history.Source.Select(item => item.Path), false); if (returnStatus is ReturnResult.IntegrityCheckFailed) // Not found, corrupted { App.HistoryWrapper.RemoveHistory(history, false); } } break; case FileOperationType.Restore: // Opposite: Move restored items to Recycle Bin if (!IsHistoryNull(history.Destination)) { var newHistory = await operations.DeleteItemsAsync(history.Destination, null, errorCode, false, cancellationToken); if (newHistory is null) { App.HistoryWrapper.RemoveHistory(history, false); } else { // We need to change the recycled item paths (since IDs are different) - for Redo() to work App.HistoryWrapper.ModifyCurrentHistory(newHistory); } } break; case FileOperationType.Delete: // Opposite: No opposite for pernament deletion returnStatus = ReturnResult.Success; break; } return(returnStatus); }