From 005ebd4cf12c16474b0025925fc3d3cbea846252 Mon Sep 17 00:00:00 2001 From: IHaidov <38441739+IHaidov@users.noreply.github.com> Date: Sun, 16 Feb 2025 01:56:04 +0100 Subject: [PATCH] Code Quality: Enhance cancellation handling in methods (#16739) Co-authored-by: ivan.haidov --- .../Data/Items/WidgetFileTagsContainerItem.cs | 2 +- .../Operations/FilesystemOperations.cs | 24 +++++++++---------- .../Utils/Storage/Search/FolderSearch.cs | 2 +- .../Storage/StorageItems/NativeStorageFile.cs | 8 +++---- .../StorageItems/StreamWithContentType.cs | 2 +- .../Storage/StorageItems/SystemStorageFile.cs | 16 ++++++------- .../StorageItems/VirtualStorageFile.cs | 4 ++-- 7 files changed, 29 insertions(+), 29 deletions(-) diff --git a/src/Files.App/Data/Items/WidgetFileTagsContainerItem.cs b/src/Files.App/Data/Items/WidgetFileTagsContainerItem.cs index e83a33ef8530..ed31af9d06f2 100644 --- a/src/Files.App/Data/Items/WidgetFileTagsContainerItem.cs +++ b/src/Files.App/Data/Items/WidgetFileTagsContainerItem.cs @@ -59,7 +59,7 @@ public WidgetFileTagsContainerItem(string tagUid) /// public async Task InitAsync(CancellationToken cancellationToken = default) { - await foreach (var item in FileTagsService.GetItemsForTagAsync(_tagUid)) + await foreach (var item in FileTagsService.GetItemsForTagAsync(_tagUid, cancellationToken)) { var icon = await ImageService.GetIconAsync(item.Storable, default); Tags.Add(new(item.Storable, icon)); diff --git a/src/Files.App/Utils/Storage/Operations/FilesystemOperations.cs b/src/Files.App/Utils/Storage/Operations/FilesystemOperations.cs index a1774b3a8689..56a064235335 100644 --- a/src/Files.App/Utils/Storage/Operations/FilesystemOperations.cs +++ b/src/Files.App/Utils/Storage/Operations/FilesystemOperations.cs @@ -166,7 +166,7 @@ await DialogDisplayHelper.ShowDialogAsync( { // CopyFileFromApp only works on file not directories var fsSourceFolder = await source.ToStorageItemResult(); - var fsDestinationFolder = await _associatedInstance.ShellViewModel.GetFolderFromPathAsync(PathNormalization.GetParentDir(destination)); + var fsDestinationFolder = await _associatedInstance.ShellViewModel.GetFolderFromPathAsync(PathNormalization.GetParentDir(destination), cancellationToken); var fsResult = (FilesystemResult)(fsSourceFolder.ErrorCode | fsDestinationFolder.ErrorCode); if (fsResult) @@ -219,7 +219,7 @@ await DialogDisplayHelper.ShowDialogAsync( { Debug.WriteLine(System.Runtime.InteropServices.Marshal.GetLastWin32Error()); - FilesystemResult destinationResult = await _associatedInstance.ShellViewModel.GetFolderFromPathAsync(PathNormalization.GetParentDir(destination)); + FilesystemResult destinationResult = await _associatedInstance.ShellViewModel.GetFolderFromPathAsync(PathNormalization.GetParentDir(destination), cancellationToken); var sourceResult = await source.ToStorageItemResult(); fsResult = sourceResult.ErrorCode | destinationResult.ErrorCode; @@ -373,7 +373,7 @@ await DialogDisplayHelper.ShowDialogAsync( Debug.WriteLine(System.Runtime.InteropServices.Marshal.GetLastWin32Error()); var fsSourceFolder = await source.ToStorageItemResult(); - var fsDestinationFolder = await _associatedInstance.ShellViewModel.GetFolderFromPathAsync(PathNormalization.GetParentDir(destination)); + var fsDestinationFolder = await _associatedInstance.ShellViewModel.GetFolderFromPathAsync(PathNormalization.GetParentDir(destination), cancellationToken); fsResult = fsSourceFolder.ErrorCode | fsDestinationFolder.ErrorCode; if (fsResult) @@ -432,7 +432,7 @@ await DialogDisplayHelper.ShowDialogAsync( { Debug.WriteLine(System.Runtime.InteropServices.Marshal.GetLastWin32Error()); - FilesystemResult destinationResult = await _associatedInstance.ShellViewModel.GetFolderFromPathAsync(PathNormalization.GetParentDir(destination)); + FilesystemResult destinationResult = await _associatedInstance.ShellViewModel.GetFolderFromPathAsync(PathNormalization.GetParentDir(destination), cancellationToken); var sourceResult = await source.ToStorageItemResult(); fsResult = sourceResult.ErrorCode | destinationResult.ErrorCode; @@ -512,12 +512,12 @@ public async Task DeleteAsync(IStorageItemWithPath source, IPro { if (source.ItemType == FilesystemItemType.File) { - fsResult = await _associatedInstance.ShellViewModel.GetFileFromPathAsync(source.Path) + fsResult = await _associatedInstance.ShellViewModel.GetFileFromPathAsync(source.Path, cancellationToken) .OnSuccess((t) => t.DeleteAsync(permanently ? StorageDeleteOption.PermanentDelete : StorageDeleteOption.Default).AsTask()); } else if (source.ItemType == FilesystemItemType.Directory) { - fsResult = await _associatedInstance.ShellViewModel.GetFolderFromPathAsync(source.Path) + fsResult = await _associatedInstance.ShellViewModel.GetFolderFromPathAsync(source.Path, cancellationToken) .OnSuccess((t) => t.DeleteAsync(permanently ? StorageDeleteOption.PermanentDelete : StorageDeleteOption.Default).AsTask()); } } @@ -539,7 +539,7 @@ public async Task DeleteAsync(IStorageItemWithPath source, IPro // Recycle bin also stores a file starting with $I for each item string iFilePath = Path.Combine(Path.GetDirectoryName(source.Path), Path.GetFileName(source.Path).Replace("$R", "$I", StringComparison.Ordinal)); - await _associatedInstance.ShellViewModel.GetFileFromPathAsync(iFilePath) + await _associatedInstance.ShellViewModel.GetFileFromPathAsync(iFilePath, cancellationToken) .OnSuccess(iFile => iFile.DeleteAsync(StorageDeleteOption.PermanentDelete).AsTask()); } fsProgress.ReportStatus(fsResult); @@ -738,8 +738,8 @@ public async Task RestoreFromTrashAsync(IStorageItemWithPath so { if (source.ItemType == FilesystemItemType.Directory) { - FilesystemResult sourceFolder = await _associatedInstance.ShellViewModel.GetFolderFromPathAsync(source.Path); - FilesystemResult destinationFolder = await _associatedInstance.ShellViewModel.GetFolderFromPathAsync(PathNormalization.GetParentDir(destination)); + FilesystemResult sourceFolder = await _associatedInstance.ShellViewModel.GetFolderFromPathAsync(source.Path, cancellationToken); + FilesystemResult destinationFolder = await _associatedInstance.ShellViewModel.GetFolderFromPathAsync(PathNormalization.GetParentDir(destination), cancellationToken); fsResult = sourceFolder.ErrorCode | destinationFolder.ErrorCode; fsProgress.ReportStatus(fsResult); @@ -759,8 +759,8 @@ public async Task RestoreFromTrashAsync(IStorageItemWithPath so } else { - FilesystemResult sourceFile = await _associatedInstance.ShellViewModel.GetFileFromPathAsync(source.Path); - FilesystemResult destinationFolder = await _associatedInstance.ShellViewModel.GetFolderFromPathAsync(PathNormalization.GetParentDir(destination)); + FilesystemResult sourceFile = await _associatedInstance.ShellViewModel.GetFileFromPathAsync(source.Path, cancellationToken); + FilesystemResult destinationFolder = await _associatedInstance.ShellViewModel.GetFolderFromPathAsync(PathNormalization.GetParentDir(destination), cancellationToken); fsResult = sourceFile.ErrorCode | destinationFolder.ErrorCode; fsProgress.ReportStatus(fsResult); @@ -782,7 +782,7 @@ public async Task RestoreFromTrashAsync(IStorageItemWithPath so // Recycle bin also stores a file starting with $I for each item string iFilePath = Path.Combine(Path.GetDirectoryName(source.Path), Path.GetFileName(source.Path).Replace("$R", "$I", StringComparison.Ordinal)); - await _associatedInstance.ShellViewModel.GetFileFromPathAsync(iFilePath) + await _associatedInstance.ShellViewModel.GetFileFromPathAsync(iFilePath, cancellationToken) .OnSuccess(iFile => iFile.DeleteAsync(StorageDeleteOption.PermanentDelete).AsTask()); } diff --git a/src/Files.App/Utils/Storage/Search/FolderSearch.cs b/src/Files.App/Utils/Storage/Search/FolderSearch.cs index 08df6e230def..29042ca0f959 100644 --- a/src/Files.App/Utils/Storage/Search/FolderSearch.cs +++ b/src/Files.App/Utils/Storage/Search/FolderSearch.cs @@ -336,7 +336,7 @@ await Task.Run(() => } while (hasNextFile); Win32PInvoke.FindClose(hFile); - }); + }, token); } } diff --git a/src/Files.App/Utils/Storage/StorageItems/NativeStorageFile.cs b/src/Files.App/Utils/Storage/StorageItems/NativeStorageFile.cs index a6b51aba9a8c..ba4f95fa61a2 100644 --- a/src/Files.App/Utils/Storage/StorageItems/NativeStorageFile.cs +++ b/src/Files.App/Utils/Storage/StorageItems/NativeStorageFile.cs @@ -86,8 +86,8 @@ public override IAsyncOperation CopyAsync(IStorageFolder destin await using (var inStream = await this.OpenStreamForReadAsync()) await using (var outStream = await destFile.OpenStreamForWriteAsync()) { - await inStream.CopyToAsync(outStream); - await outStream.FlushAsync(); + await inStream.CopyToAsync(outStream, cancellationToken); + await outStream.FlushAsync(cancellationToken); } } return destFile; @@ -251,8 +251,8 @@ public override IAsyncAction RenameAsync(string desiredName, NameCollisionOption await using (var inStream = await this.OpenStreamForReadAsync()) await using (var outStream = await destFile.OpenStreamForWriteAsync()) { - await inStream.CopyToAsync(outStream); - await outStream.FlushAsync(); + await inStream.CopyToAsync(outStream, cancellationToken); + await outStream.FlushAsync(cancellationToken); } await DeleteAsync(); } diff --git a/src/Files.App/Utils/Storage/StorageItems/StreamWithContentType.cs b/src/Files.App/Utils/Storage/StorageItems/StreamWithContentType.cs index 65a412840e63..e12d5f143a25 100644 --- a/src/Files.App/Utils/Storage/StorageItems/StreamWithContentType.cs +++ b/src/Files.App/Utils/Storage/StorageItems/StreamWithContentType.cs @@ -117,7 +117,7 @@ public IAsyncOperation FlushAsync() return AsyncInfo.Run(async (cancellationToken) => { - await stream.FlushAsync(); + await stream.FlushAsync(cancellationToken); return true; }); } diff --git a/src/Files.App/Utils/Storage/StorageItems/SystemStorageFile.cs b/src/Files.App/Utils/Storage/StorageItems/SystemStorageFile.cs index 744e055f34cd..f96f779c540f 100644 --- a/src/Files.App/Utils/Storage/StorageItems/SystemStorageFile.cs +++ b/src/Files.App/Utils/Storage/StorageItems/SystemStorageFile.cs @@ -78,8 +78,8 @@ public override IAsyncOperation CopyAsync(IStorageFolder destin await using (var inStream = await this.OpenStreamForReadAsync()) await using (var outStream = await destFile.OpenStreamForWriteAsync()) { - await inStream.CopyToAsync(outStream); - await outStream.FlushAsync(); + await inStream.CopyToAsync(outStream, cancellationToken); + await outStream.FlushAsync(cancellationToken); } return destFile; } @@ -96,8 +96,8 @@ public override IAsyncOperation CopyAsync(IStorageFolder destin await using (var inStream = await this.OpenStreamForReadAsync()) await using (var outStream = new FileStream(hFile, FileAccess.Write)) { - await inStream.CopyToAsync(outStream); - await outStream.FlushAsync(); + await inStream.CopyToAsync(outStream, cancellationToken); + await outStream.FlushAsync(cancellationToken); } return new NativeStorageFile(destination, desiredNewName, DateTime.Now); } @@ -143,8 +143,8 @@ public override IAsyncAction CopyAndReplaceAsync(IStorageFile fileToReplace) await using var inStream = await this.OpenStreamForReadAsync(); await using var outStream = await fileToReplace.OpenStreamForWriteAsync(); - await inStream.CopyToAsync(outStream); - await outStream.FlushAsync(); + await inStream.CopyToAsync(outStream, cancellationToken); + await outStream.FlushAsync(cancellationToken); }); } public override IAsyncAction MoveAndReplaceAsync(IStorageFile fileToReplace) @@ -154,8 +154,8 @@ public override IAsyncAction MoveAndReplaceAsync(IStorageFile fileToReplace) await using var inStream = await this.OpenStreamForReadAsync(); await using var outStream = await fileToReplace.OpenStreamForWriteAsync(); - await inStream.CopyToAsync(outStream); - await outStream.FlushAsync(); + await inStream.CopyToAsync(outStream, cancellationToken); + await outStream.FlushAsync(cancellationToken); // Move unsupported, copy but do not delete original }); } diff --git a/src/Files.App/Utils/Storage/StorageItems/VirtualStorageFile.cs b/src/Files.App/Utils/Storage/StorageItems/VirtualStorageFile.cs index 4e6c4c12caa9..56d2d194d0c2 100644 --- a/src/Files.App/Utils/Storage/StorageItems/VirtualStorageFile.cs +++ b/src/Files.App/Utils/Storage/StorageItems/VirtualStorageFile.cs @@ -128,8 +128,8 @@ public override IAsyncOperation CopyAsync(IStorageFolder destin await using (var inStream = await this.OpenStreamForReadAsync()) await using (var outStream = await destFile.OpenStreamForWriteAsync()) { - await inStream.CopyToAsync(outStream); - await outStream.FlushAsync(); + await inStream.CopyToAsync(outStream, cancellationToken); + await outStream.FlushAsync(cancellationToken); } return destFile; }