Skip to content

Commit 3e490d6

Browse files
authored
Codebase Quality: Made RecycleBinHelpers static (#11094)
1 parent 8825e63 commit 3e490d6

File tree

8 files changed

+32
-71
lines changed

8 files changed

+32
-71
lines changed

src/Files.App/BaseLayout.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1006,15 +1006,13 @@ protected void FileListItem_RightTapped(object sender, RightTappedRoutedEventArg
10061006
ItemManipulationModel.SetSelectedItem(rightClickedItem);
10071007
}
10081008

1009-
private readonly RecycleBinHelpers recycleBinHelpers = new();
1010-
10111009
protected void InitializeDrag(UIElement containter, ListedItem item)
10121010
{
10131011
if (item is null)
10141012
return;
10151013

10161014
UninitializeDrag(containter);
1017-
if ((item.PrimaryItemAttribute == StorageItemTypes.Folder && !recycleBinHelpers.IsPathUnderRecycleBin(item.ItemPath)) || item.IsExecutable)
1015+
if ((item.PrimaryItemAttribute == StorageItemTypes.Folder && !RecycleBinHelpers.IsPathUnderRecycleBin(item.ItemPath)) || item.IsExecutable)
10181016
{
10191017
containter.AllowDrop = true;
10201018
containter.DragOver += Item_DragOver;

src/Files.App/Filesystem/FilesystemOperations/FilesystemOperations.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,11 @@ public class FilesystemOperations : IFilesystemOperations
3333

3434
private IShellPage associatedInstance;
3535

36-
private RecycleBinHelpers recycleBinHelpers;
37-
3836
#region Constructor
3937

4038
public FilesystemOperations(IShellPage associatedInstance)
4139
{
4240
this.associatedInstance = associatedInstance;
43-
recycleBinHelpers = new RecycleBinHelpers();
4441
}
4542

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

488-
bool deleteFromRecycleBin = recycleBinHelpers.IsPathUnderRecycleBin(source.Path);
485+
bool deleteFromRecycleBin = RecycleBinHelpers.IsPathUnderRecycleBin(source.Path);
489486

490487
FilesystemResult fsResult = FileSystemStatusCode.InProgress;
491488

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

829826
public void Dispose()
830827
{
831-
recycleBinHelpers = null;
832828
associatedInstance = null;
833829
}
834830

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

942-
permanently = recycleBinHelpers.IsPathUnderRecycleBin(source[i].Path) || originalPermanently;
938+
permanently = RecycleBinHelpers.IsPathUnderRecycleBin(source[i].Path) || originalPermanently;
943939

944940
rawStorageHistory.Add(await DeleteAsync(source[i], null, permanently, token));
945941
fsProgress.ProcessedItemsCount++;

src/Files.App/Filesystem/FilesystemOperations/Helpers/FilesystemHelpers.cs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@ public class FilesystemHelpers : IFilesystemHelpers
3636

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

39-
private RecycleBinHelpers recycleBinHelpers;
40-
4139
private readonly CancellationToken cancellationToken;
4240

4341
#region Helpers Members
@@ -81,7 +79,6 @@ public FilesystemHelpers(IShellPage associatedInstance, CancellationToken cancel
8179
this.associatedInstance = associatedInstance;
8280
this.cancellationToken = cancellationToken;
8381
filesystemOperations = new ShellFilesystemOperations(this.associatedInstance);
84-
recycleBinHelpers = new RecycleBinHelpers();
8582
}
8683

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

126123
var returnStatus = ReturnResult.InProgress;
127124

128-
var deleteFromRecycleBin = source.Select(item => item.Path).Any(path => recycleBinHelpers.IsPathUnderRecycleBin(path));
129-
var canBeSentToBin = !deleteFromRecycleBin && await recycleBinHelpers.HasRecycleBin(source.FirstOrDefault()?.Path);
125+
var deleteFromRecycleBin = source.Select(item => item.Path).Any(path => RecycleBinHelpers.IsPathUnderRecycleBin(path));
126+
var canBeSentToBin = !deleteFromRecycleBin && await RecycleBinHelpers.HasRecycleBin(source.FirstOrDefault()?.Path);
130127

131128
if (showDialog && UserSettingsService.FoldersSettingsService.ShowConfirmDeleteDialog) // Check if the setting to show a confirmation dialog is on
132129
{
133130
var incomingItems = new List<BaseFileSystemDialogItemViewModel>();
134131
List<ShellFileItem>? binItems = null;
135132
foreach (var src in source)
136133
{
137-
if (recycleBinHelpers.IsPathUnderRecycleBin(src.Path))
134+
if (RecycleBinHelpers.IsPathUnderRecycleBin(src.Path))
138135
{
139136
binItems ??= await RecycleBinHelpers.EnumerateRecycleBin();
140137
if (!binItems.IsEmpty()) // Might still be null because we're deserializing the list from Json
@@ -374,7 +371,7 @@ public async Task<ReturnResult> CopyItemsFromClipboard(DataPackageView packageVi
374371
List<ShellFileItem> binItems = null;
375372
foreach (var item in source)
376373
{
377-
if (recycleBinHelpers.IsPathUnderRecycleBin(item.Path))
374+
if (RecycleBinHelpers.IsPathUnderRecycleBin(item.Path))
378375
{
379376
binItems ??= await RecycleBinHelpers.EnumerateRecycleBin();
380377
if (!binItems.IsEmpty()) // Might still be null because we're deserializing the list from Json
@@ -513,7 +510,7 @@ public async Task<ReturnResult> MoveItemsFromClipboard(DataPackageView packageVi
513510
List<ShellFileItem> binItems = null;
514511
foreach (var item in source)
515512
{
516-
if (recycleBinHelpers.IsPathUnderRecycleBin(item.Path))
513+
if (RecycleBinHelpers.IsPathUnderRecycleBin(item.Path))
517514
{
518515
binItems ??= await RecycleBinHelpers.EnumerateRecycleBin();
519516
if (!binItems.IsEmpty()) // Might still be null because we're deserializing the list from Json
@@ -640,7 +637,7 @@ public async Task<ReturnResult> RecycleItemsFromClipboard(DataPackageView packag
640637
var source = await GetDraggedStorageItems(packageView);
641638
ReturnResult returnStatus = ReturnResult.InProgress;
642639

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

646643
return returnStatus;
@@ -845,7 +842,6 @@ public void Dispose()
845842

846843
associatedInstance = null;
847844
filesystemOperations = null;
848-
recycleBinHelpers = null;
849845
}
850846

851847
#endregion IDisposable

src/Files.App/Filesystem/FilesystemOperations/ShellFilesystemOperations.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ public class ShellFilesystemOperations : IFilesystemOperations
2727

2828
private FilesystemOperations filesystemOperations;
2929

30-
private RecycleBinHelpers recycleBinHelpers;
31-
3230
private IDialogService DialogService { get; } = Ioc.Default.GetRequiredService<IDialogService>();
3331

3432
#endregion Private Members
@@ -39,7 +37,6 @@ public ShellFilesystemOperations(IShellPage associatedInstance)
3937
{
4038
this.associatedInstance = associatedInstance;
4139
filesystemOperations = new FilesystemOperations(associatedInstance);
42-
recycleBinHelpers = new RecycleBinHelpers();
4340
}
4441

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

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

783780
filesystemOperations = null;
784-
recycleBinHelpers = null;
785781
associatedInstance = null;
786782
}
787783

src/Files.App/Helpers/RecycleBinHelpers.cs

Lines changed: 14 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@
1414

1515
namespace Files.App.Helpers
1616
{
17-
public class RecycleBinHelpers
17+
public static class RecycleBinHelpers
1818
{
1919
#region Private Members
2020

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

2323
#endregion Private Members
2424

@@ -31,30 +31,25 @@ public static ulong GetSize()
3131
{
3232
return (ulong)Win32Shell.QueryRecycleBin().BinSize;
3333
}
34-
35-
public async Task<bool> IsRecycleBinItem(IStorageItem item)
34+
35+
public static async Task<bool> IsRecycleBinItem(IStorageItem item)
3636
{
3737
List<ShellFileItem> recycleBinItems = await EnumerateRecycleBin();
3838
return recycleBinItems.Any((shellItem) => shellItem.RecyclePath == item.Path);
3939
}
4040

41-
public async Task<bool> IsRecycleBinItem(string path)
41+
public static async Task<bool> IsRecycleBinItem(string path)
4242
{
4343
List<ShellFileItem> recycleBinItems = await EnumerateRecycleBin();
4444
return recycleBinItems.Any((shellItem) => shellItem.RecyclePath == path);
4545
}
4646

47-
public bool IsPathUnderRecycleBin(string path)
47+
public static bool IsPathUnderRecycleBin(string path)
4848
{
4949
return !string.IsNullOrWhiteSpace(path) && recycleBinPathRegex.IsMatch(path);
5050
}
5151

52-
public static async Task S_EmptyRecycleBin()
53-
{
54-
await new RecycleBinHelpers().EmptyRecycleBin();
55-
}
56-
57-
public async Task EmptyRecycleBin()
52+
public static async Task EmptyRecycleBin()
5853
{
5954
var ConfirmEmptyBinDialog = new ContentDialog()
6055
{
@@ -95,12 +90,7 @@ public async Task EmptyRecycleBin()
9590
}
9691
}
9792

98-
public static async Task S_RestoreRecycleBin(IShellPage associatedInstance)
99-
{
100-
await new RecycleBinHelpers().RestoreRecycleBin(associatedInstance);
101-
}
102-
103-
public async Task RestoreRecycleBin(IShellPage associatedInstance)
93+
public static async Task RestoreRecycleBin(IShellPage associatedInstance)
10494
{
10595
var ConfirmEmptyBinDialog = new ContentDialog()
10696
{
@@ -120,12 +110,7 @@ public async Task RestoreRecycleBin(IShellPage associatedInstance)
120110
}
121111
}
122112

123-
public static async Task S_RestoreSelectionRecycleBin(IShellPage associatedInstance)
124-
{
125-
await new RecycleBinHelpers().RestoreSelectionRecycleBin(associatedInstance);
126-
}
127-
128-
public async Task RestoreSelectionRecycleBin(IShellPage associatedInstance)
113+
public static async Task RestoreSelectionRecycleBin(IShellPage associatedInstance)
129114
{
130115
var ConfirmEmptyBinDialog = new ContentDialog()
131116
{
@@ -143,7 +128,7 @@ public async Task RestoreSelectionRecycleBin(IShellPage associatedInstance)
143128
}
144129

145130
//WINUI3
146-
private ContentDialog SetContentDialogRoot(ContentDialog contentDialog)
131+
private static ContentDialog SetContentDialogRoot(ContentDialog contentDialog)
147132
{
148133
if (Windows.Foundation.Metadata.ApiInformation.IsApiContractPresent("Windows.Foundation.UniversalApiContract", 8))
149134
{
@@ -152,7 +137,7 @@ private ContentDialog SetContentDialogRoot(ContentDialog contentDialog)
152137
return contentDialog;
153138
}
154139

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

165-
public bool RecycleBinHasItems()
150+
public static bool RecycleBinHasItems()
166151
{
167152
return Win32Shell.QueryRecycleBin().NumItems > 0;
168153
}
169154

170-
public static async Task S_RestoreItem(IShellPage associatedInstance)
171-
{
172-
await new RecycleBinHelpers().RestoreItem(associatedInstance);
173-
}
174-
175-
private async Task RestoreItem(IShellPage associatedInstance)
155+
public static async Task RestoreItem(IShellPage associatedInstance)
176156
{
177157
var items = associatedInstance.SlimContentPage.SelectedItems.ToList().Where(x => x is RecycleBinItem).Select((item) => new
178158
{
@@ -184,12 +164,7 @@ private async Task RestoreItem(IShellPage associatedInstance)
184164
await associatedInstance.FilesystemHelpers.RestoreItemsFromTrashAsync(items.Select(x => x.Source), items.Select(x => x.Dest), true);
185165
}
186166

187-
public static async Task S_DeleteItem(IShellPage associatedInstance)
188-
{
189-
await new RecycleBinHelpers().DeleteItem(associatedInstance);
190-
}
191-
192-
public virtual async Task DeleteItem(IShellPage associatedInstance)
167+
public static async Task DeleteItem(IShellPage associatedInstance)
193168
{
194169
var items = associatedInstance.SlimContentPage.SelectedItems.ToList().Select((item) => StorageHelpers.FromPathAndType(
195170
item.ItemPath,

src/Files.App/Interacts/BaseLayoutCommandImplementationModel.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -153,17 +153,17 @@ public virtual void UnpinDirectoryFromFavorites(RoutedEventArgs e)
153153

154154
public virtual async void EmptyRecycleBin(RoutedEventArgs e)
155155
{
156-
await RecycleBinHelpers.S_EmptyRecycleBin();
156+
await RecycleBinHelpers.EmptyRecycleBin();
157157
}
158158

159159
public virtual async void RestoreRecycleBin(RoutedEventArgs e)
160160
{
161-
await RecycleBinHelpers.S_RestoreRecycleBin(associatedInstance);
161+
await RecycleBinHelpers.RestoreRecycleBin(associatedInstance);
162162
}
163163

164164
public virtual async void RestoreSelectionRecycleBin(RoutedEventArgs e)
165165
{
166-
await RecycleBinHelpers.S_RestoreSelectionRecycleBin(associatedInstance);
166+
await RecycleBinHelpers.RestoreSelectionRecycleBin(associatedInstance);
167167
}
168168

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

184184
public virtual async void RestoreItem(RoutedEventArgs e)
185185
{
186-
await RecycleBinHelpers.S_RestoreItem(associatedInstance);
186+
await RecycleBinHelpers.RestoreItem(associatedInstance);
187187
}
188188

189189
public virtual async void DeleteItem(RoutedEventArgs e)
190190
{
191-
await RecycleBinHelpers.S_DeleteItem(associatedInstance);
191+
await RecycleBinHelpers.DeleteItem(associatedInstance);
192192
}
193193

194194
public virtual void ShowFolderProperties(RoutedEventArgs e) => ShowProperties(e);

src/Files.App/UserControls/SidebarControl.xaml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1045,7 +1045,7 @@ private async void LoadShellMenuItems(CommandBarFlyout itemContextMenuFlyout, Co
10451045
var emptyRecycleBinItem = itemContextMenuFlyout.SecondaryCommands.FirstOrDefault(x => x is AppBarButton appBarButton && (appBarButton.Tag as string) == "EmptyRecycleBin") as AppBarButton;
10461046
if (emptyRecycleBinItem is not null)
10471047
{
1048-
var binHasItems = new RecycleBinHelpers().RecycleBinHasItems();
1048+
var binHasItems = RecycleBinHelpers.RecycleBinHasItems();
10491049
emptyRecycleBinItem.IsEnabled = binHasItems;
10501050
}
10511051
}

src/Files.App/ViewModels/SidebarViewModel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,7 @@ public async void UpdateSectionVisibility(SectionType sectionType, bool show)
525525

526526
public async void EmptyRecycleBin(RoutedEventArgs e)
527527
{
528-
await RecycleBinHelpers.S_EmptyRecycleBin();
528+
await RecycleBinHelpers.EmptyRecycleBin();
529529
}
530530

531531
private void UserSettingsService_OnSettingChangedEvent(object sender, SettingChangedEventArgs e)

0 commit comments

Comments
 (0)