Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code Quality: Replace foreach with Task.WhenAll #14657

Merged
merged 5 commits into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ public BaseRotateAction()

public async Task ExecuteAsync()
{
foreach (var image in context.SelectedItems)
await BitmapHelper.RotateAsync(PathNormalization.NormalizePath(image.ItemPath), Rotation);
await Task.WhenAll(context.SelectedItems.Select(image => BitmapHelper.RotateAsync(PathNormalization.NormalizePath(image.ItemPath), Rotation)));

context.ShellPage?.SlimContentPage?.ItemManipulationModel?.RefreshItemsThumbnail();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ public InstallInfDriverAction()

public async Task ExecuteAsync()
{
foreach (ListedItem selectedItem in context.SelectedItems)
await Win32API.InstallInf(selectedItem.ItemPath);
await Task.WhenAll(context.SelectedItems.Select(selectedItem => Win32API.InstallInf(selectedItem.ItemPath)));
}

public void Context_PropertyChanged(object? sender, System.ComponentModel.PropertyChangedEventArgs e)
Expand Down
11 changes: 3 additions & 8 deletions src/Files.App/Data/Factories/ShellContextFlyoutHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using Vanara.PInvoke;
using Windows.System;
using Windows.UI.Core;
using static Vanara.PInvoke.Kernel32;

namespace Files.App.Helpers
{
Expand Down Expand Up @@ -180,17 +181,11 @@ async Task InvokeShellMenuItemAsync(ContextMenu contextMenu, object? tag)
switch (verb)
{
case "install" when isFont:
{
foreach (string path in contextMenu.ItemsPath)
await Win32API.InstallFont(path, false);
}
await Win32API.InstallFontsAsync(contextMenu.ItemsPath.ToArray(), false);
break;

case "installAllUsers" when isFont:
{
foreach (string path in contextMenu.ItemsPath)
await Win32API.InstallFont(path, true);
}
await Win32API.InstallFontsAsync(contextMenu.ItemsPath.ToArray(), true);
break;

case "mount":
Expand Down
17 changes: 1 addition & 16 deletions src/Files.App/Utils/Shell/Win32API.cs
Original file line number Diff line number Diff line change
Expand Up @@ -850,21 +850,6 @@ public static async Task<bool> InstallInf(string filePath)
}
}

public static Task InstallFont(string fontFilePath, bool forAllUsers)
{
string fontDirectory = forAllUsers
? Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Windows), "Fonts")
: Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Microsoft", "Windows", "Fonts");

string registryKey = forAllUsers
? "HKLM:\\Software\\Microsoft\\Windows NT\\CurrentVersion\\Fonts"
: "HKCU:\\Software\\Microsoft\\Windows NT\\CurrentVersion\\Fonts";

var destinationPath = Path.Combine(fontDirectory, Path.GetFileName(fontFilePath));

return RunPowershellCommandAsync($"-command \"Copy-Item '{fontFilePath}' '{fontDirectory}'; New-ItemProperty -Name '{Path.GetFileNameWithoutExtension(fontFilePath)}' -Path '{registryKey}' -PropertyType string -Value '{destinationPath}'\"", forAllUsers);
}

public static async Task InstallFontsAsync(string[] fontFilePaths, bool forAllUsers)
{
string fontDirectory = forAllUsers
Expand All @@ -885,7 +870,7 @@ public static async Task InstallFontsAsync(string[] fontFilePaths, bool forAllUs
if (psCommand.Length + appendCommand.Length > 32766)
{
// The command is too long to run at once, so run the command once up to this point.
await RunPowershellCommandAsync(psCommand.Append("\"").ToString(), forAllUsers);
await RunPowershellCommandAsync(psCommand.Append("\"").ToString(), true);
psCommand.Clear().Append("-command \"");
}

Expand Down
9 changes: 6 additions & 3 deletions src/Files.App/Views/Layouts/DetailsLayoutPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -431,12 +431,15 @@ private async Task ReloadItemIconsAsync()

ParentShellPageInstance.FilesystemViewModel.CancelExtendedPropertiesLoading();
var filesAndFolders = ParentShellPageInstance.FilesystemViewModel.FilesAndFolders.ToList();
foreach (ListedItem listedItem in filesAndFolders)

await Task.WhenAll(filesAndFolders.Select(listedItem =>
{
listedItem.ItemPropertiesInitialized = false;
if (FileList.ContainerFromItem(listedItem) is not null)
await ParentShellPageInstance.FilesystemViewModel.LoadExtendedItemPropertiesAsync(listedItem, currentIconSize);
}
return ParentShellPageInstance.FilesystemViewModel.LoadExtendedItemPropertiesAsync(listedItem, currentIconSize);
else
return Task.CompletedTask;
}));

if (ParentShellPageInstance.FilesystemViewModel.EnabledGitProperties is not GitProperties.None)
{
Expand Down
6 changes: 2 additions & 4 deletions src/Files.Core/Services/SizeProvider/DrivesSizeProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,12 @@ public async Task CleanAsync()
foreach (var oldDriveName in oldDriveNames)
providers.TryRemove(oldDriveName, out _);

foreach (var provider in providers.Values)
await provider.CleanAsync();
await Task.WhenAll(providers.Values.Select(provider => provider.CleanAsync()));
}

public async Task ClearAsync()
{
foreach (var provider in providers.Values)
await provider.ClearAsync();
await Task.WhenAll(providers.Values.Select(provider => provider.ClearAsync()));

providers.Clear();
}
Expand Down
Loading