Skip to content

Commit 6de9ff0

Browse files
authored
Improved Properties Dialog codebase (#4468)
1 parent 184728c commit 6de9ff0

7 files changed

+68
-35
lines changed

Files/ViewModels/Properties/FileProperties.cs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,15 @@ namespace Files.ViewModels.Properties
2727
{
2828
public class FileProperties : BaseProperties
2929
{
30-
private ProgressBar ProgressBar;
30+
private IProgress<float> hashProgress;
3131

3232
public ListedItem Item { get; }
3333

34-
public FileProperties(SelectedItemsPropertiesViewModel viewModel, CancellationTokenSource tokenSource, CoreDispatcher coreDispatcher, ProgressBar progressBar, ListedItem item, IShellPage instance)
34+
public FileProperties(SelectedItemsPropertiesViewModel viewModel, CancellationTokenSource tokenSource, CoreDispatcher coreDispatcher, IProgress<float> hashProgress, ListedItem item, IShellPage instance)
3535
{
3636
ViewModel = viewModel;
3737
TokenSource = tokenSource;
38-
ProgressBar = progressBar;
38+
this.hashProgress = hashProgress;
3939
Dispatcher = coreDispatcher;
4040
Item = item;
4141
AppInstance = instance;
@@ -148,7 +148,7 @@ public override async void GetSpecialProperties()
148148
ViewModel.ItemMD5HashVisibility = Visibility.Visible;
149149
try
150150
{
151-
ViewModel.ItemMD5Hash = await GetHashForFileAsync(Item, hashAlgTypeName, TokenSource.Token, ProgressBar, AppInstance);
151+
ViewModel.ItemMD5Hash = await GetHashForFileAsync(Item, hashAlgTypeName, TokenSource.Token, hashProgress, AppInstance);
152152
}
153153
catch (Exception ex)
154154
{
@@ -341,7 +341,7 @@ private async void ViewModel_PropertyChanged(object sender, System.ComponentMode
341341
}
342342
}
343343

344-
private async Task<string> GetHashForFileAsync(ListedItem fileItem, string nameOfAlg, CancellationToken token, ProgressBar progress, IShellPage associatedInstance)
344+
private async Task<string> GetHashForFileAsync(ListedItem fileItem, string nameOfAlg, CancellationToken token, IProgress<float> progress, IShellPage associatedInstance)
345345
{
346346
HashAlgorithmProvider algorithmProvider = HashAlgorithmProvider.OpenAlgorithm(nameOfAlg);
347347
StorageFile file = await StorageItemHelpers.ToStorageItem<StorageFile>((fileItem as ShortcutItem)?.TargetPath ?? fileItem.ItemPath, associatedInstance);
@@ -382,10 +382,7 @@ private async Task<string> GetHashForFileAsync(ListedItem fileItem, string nameO
382382
{
383383
break;
384384
}
385-
if (progress != null)
386-
{
387-
progress.Value = (double)str.Position / str.Length * 100;
388-
}
385+
progress?.Report((float)str.Position / str.Length * 100.0f);
389386
}
390387
inputStream.Dispose();
391388
stream.Dispose();

Files/ViewModels/Properties/PropertiesTab.cs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
11
using Files.Filesystem;
2+
using System;
23
using System.Collections.Generic;
4+
using System.Threading.Tasks;
35
using Windows.Storage;
46
using Windows.UI.Xaml;
57
using Windows.UI.Xaml.Controls;
68
using Windows.UI.Xaml.Navigation;
79

810
namespace Files.ViewModels.Properties
911
{
10-
public abstract class PropertiesTab : Page
12+
public abstract class PropertiesTab : Page, IDisposable
1113
{
1214
public IShellPage AppInstance = null;
1315

1416
public BaseProperties BaseProperties { get; set; }
1517

1618
public SelectedItemsPropertiesViewModel ViewModel { get; set; }
1719

18-
protected Microsoft.UI.Xaml.Controls.ProgressBar ItemMD5HashProgress = null;
20+
protected IProgress<float> hashProgress;
1921

2022
protected virtual void Properties_Loaded(object sender, RoutedEventArgs e)
2123
{
@@ -40,7 +42,7 @@ protected override void OnNavigatedTo(NavigationEventArgs e)
4042
{
4143
if (item.PrimaryItemAttribute == StorageItemTypes.File)
4244
{
43-
BaseProperties = new FileProperties(ViewModel, np.tokenSource, Dispatcher, ItemMD5HashProgress, item, AppInstance);
45+
BaseProperties = new FileProperties(ViewModel, np.tokenSource, Dispatcher, hashProgress, item, AppInstance);
4446
}
4547
else if (item.PrimaryItemAttribute == StorageItemTypes.Folder)
4648
{
@@ -68,5 +70,13 @@ protected override void OnNavigatedFrom(NavigationEventArgs e)
6870

6971
base.OnNavigatedFrom(e);
7072
}
73+
74+
/// <summary>
75+
/// Tries to save changed properties to file.
76+
/// </summary>
77+
/// <returns>Returns true if properties have been saved successfully.</returns>
78+
public abstract Task<bool> SaveChangesAsync(ListedItem item);
79+
80+
public abstract void Dispose();
7181
}
7282
}

Files/Views/Pages/Properties.xaml.cs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using Files.Helpers;
33
using Files.Helpers.XamlHelpers;
44
using Files.ViewModels;
5+
using Files.ViewModels.Properties;
56
using Microsoft.Toolkit.Uwp.Helpers;
67
using System;
78
using System.Threading;
@@ -141,21 +142,15 @@ private async void OKButton_Click(object sender, RoutedEventArgs e)
141142
{
142143
await propertiesGeneral.SaveChangesAsync(listedItem);
143144
}
144-
else if (contentFrame.Content is PropertiesLibrary propertiesLibrary)
145-
{
146-
if (!await propertiesLibrary.SaveChangesAsync())
147-
{
148-
return;
149-
}
150-
}
151-
else if (contentFrame.Content is PropertiesDetails propertiesDetails)
145+
else
152146
{
153-
if (!await propertiesDetails.SaveChangesAsync())
147+
if (!await (contentFrame.Content as PropertiesTab).SaveChangesAsync(listedItem))
154148
{
155149
return;
156150
}
157151
}
158152

153+
(contentFrame.Content as PropertiesTab).Dispose();
159154
if (ApiInformation.IsApiContractPresent("Windows.Foundation.UniversalApiContract", 8))
160155
{
161156
await ApplicationView.GetForCurrentView().TryConsolidateAsync();

Files/Views/Pages/PropertiesDetails.xaml.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Files.Dialogs;
22
using Files.Enums;
3+
using Files.Filesystem;
34
using Files.Helpers;
45
using Files.ViewModels.Properties;
56
using System;
@@ -29,11 +30,7 @@ protected override void Properties_Loaded(object sender, RoutedEventArgs e)
2930
}
3031
}
3132

32-
/// <summary>
33-
/// Tries to save changed properties to file.
34-
/// </summary>
35-
/// <returns>Returns true if properties have been saved successfully.</returns>
36-
public async Task<bool> SaveChangesAsync()
33+
public override async Task<bool> SaveChangesAsync(ListedItem item)
3734
{
3835
while (true)
3936
{
@@ -72,5 +69,9 @@ private async void ClearPropertiesConfirmation_Click(object sender, RoutedEventA
7269
ClearPropertiesFlyout.Hide();
7370
await (BaseProperties as FileProperties).ClearPropertiesAsync();
7471
}
72+
73+
public override void Dispose()
74+
{
75+
}
7576
}
7677
}

Files/Views/Pages/PropertiesGeneral.xaml.cs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using Files.Helpers;
44
using Files.ViewModels.Properties;
55
using Microsoft.Toolkit.Uwp;
6+
using System;
67
using System.IO;
78
using System.Threading.Tasks;
89
using Windows.ApplicationModel.Core;
@@ -15,10 +16,17 @@ public sealed partial class PropertiesGeneral : PropertiesTab
1516
public PropertiesGeneral()
1617
{
1718
this.InitializeComponent();
18-
base.ItemMD5HashProgress = ItemMD5HashProgress;
19+
base.hashProgress = new Progress<float>();
20+
21+
(base.hashProgress as Progress<float>).ProgressChanged += PropertiesGeneral_ProgressChanged;
1922
}
2023

21-
public async Task SaveChangesAsync(ListedItem item)
24+
private void PropertiesGeneral_ProgressChanged(object sender, float e)
25+
{
26+
ItemMD5HashProgress.Value = (double)e;
27+
}
28+
29+
public override async Task<bool> SaveChangesAsync(ListedItem item)
2230
{
2331
if (BaseProperties is DriveProperties driveProps)
2432
{
@@ -38,6 +46,7 @@ public async Task SaveChangesAsync(ListedItem item)
3846
await drive.UpdateLabelAsync();
3947
await AppInstance.FilesystemViewModel?.SetWorkingDirectoryAsync(drive.Path);
4048
});
49+
return true;
4150
}
4251
}
4352
}
@@ -58,6 +67,7 @@ public async Task SaveChangesAsync(ListedItem item)
5867
{
5968
await AppInstance.FilesystemViewModel?.SetWorkingDirectoryAsync(newPath);
6069
});
70+
return true;
6171
}
6272
}
6373
}
@@ -66,7 +76,7 @@ public async Task SaveChangesAsync(ListedItem item)
6676
{
6777
if (!string.IsNullOrWhiteSpace(ViewModel.ItemName) && ViewModel.OriginalItemName != ViewModel.ItemName)
6878
{
69-
await CoreApplication.MainView.DispatcherQueue.EnqueueAsync(() => UIFilesystemHelpers.RenameFileItemAsync(item,
79+
return await CoreApplication.MainView.DispatcherQueue.EnqueueAsync(() => UIFilesystemHelpers.RenameFileItemAsync(item,
7080
ViewModel.OriginalItemName,
7181
ViewModel.ItemName,
7282
AppInstance));
@@ -80,13 +90,23 @@ await CoreApplication.MainView.DispatcherQueue.EnqueueAsync(() => UIFilesystemHe
8090
{
8191
await CoreApplication.MainView.DispatcherQueue.EnqueueAsync(() => UIFilesystemHelpers.SetHiddenAttributeItem(fileOrFolder, ViewModel.IsHidden, AppInstance.SlimContentPage.ItemManipulationModel));
8292
}
93+
return true;
8394
}
8495
else
8596
{
8697
// Handle the visibility attribute for a single file
8798
await CoreApplication.MainView.DispatcherQueue.EnqueueAsync(() => UIFilesystemHelpers.SetHiddenAttributeItem(item, ViewModel.IsHidden, AppInstance.SlimContentPage.ItemManipulationModel));
99+
100+
return true;
88101
}
89102
}
103+
104+
return false;
105+
}
106+
107+
public override void Dispose()
108+
{
109+
(base.hashProgress as Progress<float>).ProgressChanged -= PropertiesGeneral_ProgressChanged;
90110
}
91111
}
92112
}

Files/Views/Pages/PropertiesLibrary.xaml.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -170,11 +170,7 @@ private bool IsChanged(LibraryItem lib, out string newDefaultSaveFolder, out str
170170
return isChanged;
171171
}
172172

173-
/// <summary>
174-
/// Tries to save changed properties to file.
175-
/// </summary>
176-
/// <returns>Returns true if properties have been saved successfully.</returns>
177-
public async Task<bool> SaveChangesAsync()
173+
public override async Task<bool> SaveChangesAsync(ListedItem item)
178174
{
179175
if (BaseProperties is LibraryProperties props)
180176
{
@@ -227,6 +223,10 @@ public async Task<bool> SaveChangesAsync()
227223
return false;
228224
}
229225

226+
public override void Dispose()
227+
{
228+
}
229+
230230
public class LibraryFolder
231231
{
232232
public string Path { get; set; }

Files/Views/Pages/PropertiesShortcut.xaml.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
using Files.ViewModels.Properties;
1+
using Files.Filesystem;
2+
using Files.ViewModels.Properties;
3+
using System.Threading.Tasks;
24

35
namespace Files.Views
46
{
@@ -8,5 +10,13 @@ public PropertiesShortcut()
810
{
911
this.InitializeComponent();
1012
}
13+
14+
public async override Task<bool> SaveChangesAsync(ListedItem item)
15+
{
16+
return false;
17+
}
18+
public override void Dispose()
19+
{
20+
}
1121
}
1222
}

0 commit comments

Comments
 (0)