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

Codebase Quality: Made RecycleBinHelpers static #11094

Merged
merged 3 commits into from
Feb 1, 2023
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
4 changes: 1 addition & 3 deletions src/Files.App/BaseLayout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1006,15 +1006,13 @@ protected void FileListItem_RightTapped(object sender, RightTappedRoutedEventArg
ItemManipulationModel.SetSelectedItem(rightClickedItem);
}

private readonly RecycleBinHelpers recycleBinHelpers = new();

protected void InitializeDrag(UIElement containter, ListedItem item)
{
if (item is null)
return;

UninitializeDrag(containter);
if ((item.PrimaryItemAttribute == StorageItemTypes.Folder && !recycleBinHelpers.IsPathUnderRecycleBin(item.ItemPath)) || item.IsExecutable)
if ((item.PrimaryItemAttribute == StorageItemTypes.Folder && !RecycleBinHelpers.IsPathUnderRecycleBin(item.ItemPath)) || item.IsExecutable)
{
containter.AllowDrop = true;
containter.DragOver += Item_DragOver;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,11 @@ public class FilesystemOperations : IFilesystemOperations

private IShellPage associatedInstance;

private RecycleBinHelpers recycleBinHelpers;

#region Constructor

public FilesystemOperations(IShellPage associatedInstance)
{
this.associatedInstance = associatedInstance;
recycleBinHelpers = new RecycleBinHelpers();
}

#endregion Constructor
Expand Down Expand Up @@ -485,7 +482,7 @@ public async Task<IStorageHistory> DeleteAsync(IStorageItemWithPath source,
FileSystemProgress fsProgress = new(progress, true, FileSystemStatusCode.InProgress);
fsProgress.Report();

bool deleteFromRecycleBin = recycleBinHelpers.IsPathUnderRecycleBin(source.Path);
bool deleteFromRecycleBin = RecycleBinHelpers.IsPathUnderRecycleBin(source.Path);

FilesystemResult fsResult = FileSystemStatusCode.InProgress;

Expand Down Expand Up @@ -828,7 +825,6 @@ private async static Task<BaseStorageFolder> CloneDirectoryAsync(BaseStorageFold

public void Dispose()
{
recycleBinHelpers = null;
associatedInstance = null;
}

Expand Down Expand Up @@ -939,7 +935,7 @@ public async Task<IStorageHistory> DeleteItemsAsync(IList<IStorageItemWithPath>
if (token.IsCancellationRequested)
break;

permanently = recycleBinHelpers.IsPathUnderRecycleBin(source[i].Path) || originalPermanently;
permanently = RecycleBinHelpers.IsPathUnderRecycleBin(source[i].Path) || originalPermanently;

rawStorageHistory.Add(await DeleteAsync(source[i], null, permanently, token));
fsProgress.ProcessedItemsCount++;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ public class FilesystemHelpers : IFilesystemHelpers

private ItemManipulationModel itemManipulationModel => associatedInstance.SlimContentPage?.ItemManipulationModel;

private RecycleBinHelpers recycleBinHelpers;

private readonly CancellationToken cancellationToken;

#region Helpers Members
Expand Down Expand Up @@ -81,7 +79,6 @@ public FilesystemHelpers(IShellPage associatedInstance, CancellationToken cancel
this.associatedInstance = associatedInstance;
this.cancellationToken = cancellationToken;
filesystemOperations = new ShellFilesystemOperations(this.associatedInstance);
recycleBinHelpers = new RecycleBinHelpers();
}

#endregion Constructor
Expand Down Expand Up @@ -125,16 +122,16 @@ public async Task<ReturnResult> DeleteItemsAsync(IEnumerable<IStorageItemWithPat

var returnStatus = ReturnResult.InProgress;

var deleteFromRecycleBin = source.Select(item => item.Path).Any(path => recycleBinHelpers.IsPathUnderRecycleBin(path));
var canBeSentToBin = !deleteFromRecycleBin && await recycleBinHelpers.HasRecycleBin(source.FirstOrDefault()?.Path);
var deleteFromRecycleBin = source.Select(item => item.Path).Any(path => RecycleBinHelpers.IsPathUnderRecycleBin(path));
var canBeSentToBin = !deleteFromRecycleBin && await RecycleBinHelpers.HasRecycleBin(source.FirstOrDefault()?.Path);

if (showDialog && UserSettingsService.FoldersSettingsService.ShowConfirmDeleteDialog) // Check if the setting to show a confirmation dialog is on
{
var incomingItems = new List<BaseFileSystemDialogItemViewModel>();
List<ShellFileItem>? binItems = null;
foreach (var src in source)
{
if (recycleBinHelpers.IsPathUnderRecycleBin(src.Path))
if (RecycleBinHelpers.IsPathUnderRecycleBin(src.Path))
{
binItems ??= await RecycleBinHelpers.EnumerateRecycleBin();
if (!binItems.IsEmpty()) // Might still be null because we're deserializing the list from Json
Expand Down Expand Up @@ -374,7 +371,7 @@ public async Task<ReturnResult> CopyItemsFromClipboard(DataPackageView packageVi
List<ShellFileItem> binItems = null;
foreach (var item in source)
{
if (recycleBinHelpers.IsPathUnderRecycleBin(item.Path))
if (RecycleBinHelpers.IsPathUnderRecycleBin(item.Path))
{
binItems ??= await RecycleBinHelpers.EnumerateRecycleBin();
if (!binItems.IsEmpty()) // Might still be null because we're deserializing the list from Json
Expand Down Expand Up @@ -513,7 +510,7 @@ public async Task<ReturnResult> MoveItemsFromClipboard(DataPackageView packageVi
List<ShellFileItem> binItems = null;
foreach (var item in source)
{
if (recycleBinHelpers.IsPathUnderRecycleBin(item.Path))
if (RecycleBinHelpers.IsPathUnderRecycleBin(item.Path))
{
binItems ??= await RecycleBinHelpers.EnumerateRecycleBin();
if (!binItems.IsEmpty()) // Might still be null because we're deserializing the list from Json
Expand Down Expand Up @@ -640,7 +637,7 @@ public async Task<ReturnResult> RecycleItemsFromClipboard(DataPackageView packag
var source = await GetDraggedStorageItems(packageView);
ReturnResult returnStatus = ReturnResult.InProgress;

source = source.Where(x => !recycleBinHelpers.IsPathUnderRecycleBin(x.Path)); // Can't recycle items already in recyclebin
source = source.Where(x => !RecycleBinHelpers.IsPathUnderRecycleBin(x.Path)); // Can't recycle items already in recyclebin
returnStatus = await DeleteItemsAsync(source, showDialog, false, registerHistory);

return returnStatus;
Expand Down Expand Up @@ -845,7 +842,6 @@ public void Dispose()

associatedInstance = null;
filesystemOperations = null;
recycleBinHelpers = null;
}

#endregion IDisposable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ public class ShellFilesystemOperations : IFilesystemOperations

private FilesystemOperations filesystemOperations;

private RecycleBinHelpers recycleBinHelpers;

private IDialogService DialogService { get; } = Ioc.Default.GetRequiredService<IDialogService>();

#endregion Private Members
Expand All @@ -39,7 +37,6 @@ public ShellFilesystemOperations(IShellPage associatedInstance)
{
this.associatedInstance = associatedInstance;
filesystemOperations = new FilesystemOperations(associatedInstance);
recycleBinHelpers = new RecycleBinHelpers();
}

#endregion Constructor
Expand Down Expand Up @@ -321,7 +318,7 @@ public async Task<IStorageHistory> DeleteItemsAsync(IList<IStorageItemWithPath>
FileSystemProgress fsProgress = new(progress, true, FileSystemStatusCode.InProgress);
fsProgress.Report();
var deleleFilePaths = source.Select(s => s.Path).Distinct();
var deleteFromRecycleBin = source.Any() && recycleBinHelpers.IsPathUnderRecycleBin(source.ElementAt(0).Path);
var deleteFromRecycleBin = source.Any() && RecycleBinHelpers.IsPathUnderRecycleBin(source.ElementAt(0).Path);
permanently |= deleteFromRecycleBin;

if (deleteFromRecycleBin)
Expand Down Expand Up @@ -748,7 +745,7 @@ private async Task<DialogResult> GetFileListDialog(IEnumerable<string> source, s
List<ShellFileItem> binItems = null;
foreach (var src in source)
{
if (recycleBinHelpers.IsPathUnderRecycleBin(src))
if (RecycleBinHelpers.IsPathUnderRecycleBin(src))
{
binItems ??= await RecycleBinHelpers.EnumerateRecycleBin();
if (!binItems.IsEmpty()) // Might still be null because we're deserializing the list from Json
Expand Down Expand Up @@ -781,7 +778,6 @@ public void Dispose()
filesystemOperations?.Dispose();

filesystemOperations = null;
recycleBinHelpers = null;
associatedInstance = null;
}

Expand Down
53 changes: 14 additions & 39 deletions src/Files.App/Helpers/RecycleBinHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@

namespace Files.App.Helpers
{
public class RecycleBinHelpers
public static class RecycleBinHelpers
{
#region Private Members

private static readonly Regex recycleBinPathRegex = new Regex(@"^[A-Z]:\\\$Recycle\.Bin\\", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant);
private static readonly Regex recycleBinPathRegex = new(@"^[A-Z]:\\\$Recycle\.Bin\\", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant);

#endregion Private Members

Expand All @@ -31,30 +31,25 @@ public static ulong GetSize()
{
return (ulong)Win32Shell.QueryRecycleBin().BinSize;
}

public async Task<bool> IsRecycleBinItem(IStorageItem item)
public static async Task<bool> IsRecycleBinItem(IStorageItem item)
{
List<ShellFileItem> recycleBinItems = await EnumerateRecycleBin();
return recycleBinItems.Any((shellItem) => shellItem.RecyclePath == item.Path);
}

public async Task<bool> IsRecycleBinItem(string path)
public static async Task<bool> IsRecycleBinItem(string path)
{
List<ShellFileItem> recycleBinItems = await EnumerateRecycleBin();
return recycleBinItems.Any((shellItem) => shellItem.RecyclePath == path);
}

public bool IsPathUnderRecycleBin(string path)
public static bool IsPathUnderRecycleBin(string path)
{
return !string.IsNullOrWhiteSpace(path) && recycleBinPathRegex.IsMatch(path);
}

public static async Task S_EmptyRecycleBin()
{
await new RecycleBinHelpers().EmptyRecycleBin();
}

public async Task EmptyRecycleBin()
public static async Task EmptyRecycleBin()
{
var ConfirmEmptyBinDialog = new ContentDialog()
{
Expand Down Expand Up @@ -95,12 +90,7 @@ public async Task EmptyRecycleBin()
}
}

public static async Task S_RestoreRecycleBin(IShellPage associatedInstance)
{
await new RecycleBinHelpers().RestoreRecycleBin(associatedInstance);
}

public async Task RestoreRecycleBin(IShellPage associatedInstance)
public static async Task RestoreRecycleBin(IShellPage associatedInstance)
{
var ConfirmEmptyBinDialog = new ContentDialog()
{
Expand All @@ -120,12 +110,7 @@ public async Task RestoreRecycleBin(IShellPage associatedInstance)
}
}

public static async Task S_RestoreSelectionRecycleBin(IShellPage associatedInstance)
{
await new RecycleBinHelpers().RestoreSelectionRecycleBin(associatedInstance);
}

public async Task RestoreSelectionRecycleBin(IShellPage associatedInstance)
public static async Task RestoreSelectionRecycleBin(IShellPage associatedInstance)
{
var ConfirmEmptyBinDialog = new ContentDialog()
{
Expand All @@ -143,7 +128,7 @@ public async Task RestoreSelectionRecycleBin(IShellPage associatedInstance)
}

//WINUI3
private ContentDialog SetContentDialogRoot(ContentDialog contentDialog)
private static ContentDialog SetContentDialogRoot(ContentDialog contentDialog)
{
if (Windows.Foundation.Metadata.ApiInformation.IsApiContractPresent("Windows.Foundation.UniversalApiContract", 8))
{
Expand All @@ -152,7 +137,7 @@ private ContentDialog SetContentDialogRoot(ContentDialog contentDialog)
return contentDialog;
}

public async Task<bool> HasRecycleBin(string? path)
public static async Task<bool> HasRecycleBin(string? path)
{
if (string.IsNullOrEmpty(path) || path.StartsWith(@"\\?\", StringComparison.Ordinal))
return false;
Expand All @@ -162,17 +147,12 @@ public async Task<bool> HasRecycleBin(string? path)
return result.Item1 &= result.Item2 is not null && result.Item2.Items.All(x => x.Succeeded);
}

public bool RecycleBinHasItems()
public static bool RecycleBinHasItems()
{
return Win32Shell.QueryRecycleBin().NumItems > 0;
}

public static async Task S_RestoreItem(IShellPage associatedInstance)
{
await new RecycleBinHelpers().RestoreItem(associatedInstance);
}

private async Task RestoreItem(IShellPage associatedInstance)
public static async Task RestoreItem(IShellPage associatedInstance)
{
var items = associatedInstance.SlimContentPage.SelectedItems.ToList().Where(x => x is RecycleBinItem).Select((item) => new
{
Expand All @@ -184,12 +164,7 @@ private async Task RestoreItem(IShellPage associatedInstance)
await associatedInstance.FilesystemHelpers.RestoreItemsFromTrashAsync(items.Select(x => x.Source), items.Select(x => x.Dest), true);
}

public static async Task S_DeleteItem(IShellPage associatedInstance)
{
await new RecycleBinHelpers().DeleteItem(associatedInstance);
}

public virtual async Task DeleteItem(IShellPage associatedInstance)
public static async Task DeleteItem(IShellPage associatedInstance)
{
var items = associatedInstance.SlimContentPage.SelectedItems.ToList().Select((item) => StorageHelpers.FromPathAndType(
item.ItemPath,
Expand Down
10 changes: 5 additions & 5 deletions src/Files.App/Interacts/BaseLayoutCommandImplementationModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,17 +153,17 @@ public virtual void UnpinDirectoryFromFavorites(RoutedEventArgs e)

public virtual async void EmptyRecycleBin(RoutedEventArgs e)
{
await RecycleBinHelpers.S_EmptyRecycleBin();
await RecycleBinHelpers.EmptyRecycleBin();
}

public virtual async void RestoreRecycleBin(RoutedEventArgs e)
{
await RecycleBinHelpers.S_RestoreRecycleBin(associatedInstance);
await RecycleBinHelpers.RestoreRecycleBin(associatedInstance);
}

public virtual async void RestoreSelectionRecycleBin(RoutedEventArgs e)
{
await RecycleBinHelpers.S_RestoreSelectionRecycleBin(associatedInstance);
await RecycleBinHelpers.RestoreSelectionRecycleBin(associatedInstance);
}

public virtual async void QuickLook(RoutedEventArgs e)
Expand All @@ -183,12 +183,12 @@ public virtual void CutItem(RoutedEventArgs e)

public virtual async void RestoreItem(RoutedEventArgs e)
{
await RecycleBinHelpers.S_RestoreItem(associatedInstance);
await RecycleBinHelpers.RestoreItem(associatedInstance);
}

public virtual async void DeleteItem(RoutedEventArgs e)
{
await RecycleBinHelpers.S_DeleteItem(associatedInstance);
await RecycleBinHelpers.DeleteItem(associatedInstance);
}

public virtual void ShowFolderProperties(RoutedEventArgs e) => ShowProperties(e);
Expand Down
2 changes: 1 addition & 1 deletion src/Files.App/UserControls/SidebarControl.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1045,7 +1045,7 @@ private async void LoadShellMenuItems(CommandBarFlyout itemContextMenuFlyout, Co
var emptyRecycleBinItem = itemContextMenuFlyout.SecondaryCommands.FirstOrDefault(x => x is AppBarButton appBarButton && (appBarButton.Tag as string) == "EmptyRecycleBin") as AppBarButton;
if (emptyRecycleBinItem is not null)
{
var binHasItems = new RecycleBinHelpers().RecycleBinHasItems();
var binHasItems = RecycleBinHelpers.RecycleBinHasItems();
emptyRecycleBinItem.IsEnabled = binHasItems;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Files.App/ViewModels/SidebarViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,7 @@ public async void UpdateSectionVisibility(SectionType sectionType, bool show)

public async void EmptyRecycleBin(RoutedEventArgs e)
{
await RecycleBinHelpers.S_EmptyRecycleBin();
await RecycleBinHelpers.EmptyRecycleBin();
}

private void UserSettingsService_OnSettingChangedEvent(object sender, SettingChangedEventArgs e)
Expand Down