diff --git a/Files/ViewModels/Properties/FileProperties.cs b/Files/ViewModels/Properties/FileProperties.cs index bc03d51cd372..e63457dd6845 100644 --- a/Files/ViewModels/Properties/FileProperties.cs +++ b/Files/ViewModels/Properties/FileProperties.cs @@ -27,15 +27,15 @@ namespace Files.ViewModels.Properties { public class FileProperties : BaseProperties { - private ProgressBar ProgressBar; + private IProgress hashProgress; public ListedItem Item { get; } - public FileProperties(SelectedItemsPropertiesViewModel viewModel, CancellationTokenSource tokenSource, CoreDispatcher coreDispatcher, ProgressBar progressBar, ListedItem item, IShellPage instance) + public FileProperties(SelectedItemsPropertiesViewModel viewModel, CancellationTokenSource tokenSource, CoreDispatcher coreDispatcher, IProgress hashProgress, ListedItem item, IShellPage instance) { ViewModel = viewModel; TokenSource = tokenSource; - ProgressBar = progressBar; + this.hashProgress = hashProgress; Dispatcher = coreDispatcher; Item = item; AppInstance = instance; @@ -148,7 +148,7 @@ public override async void GetSpecialProperties() ViewModel.ItemMD5HashVisibility = Visibility.Visible; try { - ViewModel.ItemMD5Hash = await GetHashForFileAsync(Item, hashAlgTypeName, TokenSource.Token, ProgressBar, AppInstance); + ViewModel.ItemMD5Hash = await GetHashForFileAsync(Item, hashAlgTypeName, TokenSource.Token, hashProgress, AppInstance); } catch (Exception ex) { @@ -341,7 +341,7 @@ private async void ViewModel_PropertyChanged(object sender, System.ComponentMode } } - private async Task GetHashForFileAsync(ListedItem fileItem, string nameOfAlg, CancellationToken token, ProgressBar progress, IShellPage associatedInstance) + private async Task GetHashForFileAsync(ListedItem fileItem, string nameOfAlg, CancellationToken token, IProgress progress, IShellPage associatedInstance) { HashAlgorithmProvider algorithmProvider = HashAlgorithmProvider.OpenAlgorithm(nameOfAlg); StorageFile file = await StorageItemHelpers.ToStorageItem((fileItem as ShortcutItem)?.TargetPath ?? fileItem.ItemPath, associatedInstance); @@ -382,10 +382,7 @@ private async Task GetHashForFileAsync(ListedItem fileItem, string nameO { break; } - if (progress != null) - { - progress.Value = (double)str.Position / str.Length * 100; - } + progress?.Report((float)str.Position / str.Length * 100.0f); } inputStream.Dispose(); stream.Dispose(); diff --git a/Files/ViewModels/Properties/PropertiesTab.cs b/Files/ViewModels/Properties/PropertiesTab.cs index b6bb09dee568..1666703ac421 100644 --- a/Files/ViewModels/Properties/PropertiesTab.cs +++ b/Files/ViewModels/Properties/PropertiesTab.cs @@ -1,5 +1,7 @@ using Files.Filesystem; +using System; using System.Collections.Generic; +using System.Threading.Tasks; using Windows.Storage; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; @@ -7,7 +9,7 @@ namespace Files.ViewModels.Properties { - public abstract class PropertiesTab : Page + public abstract class PropertiesTab : Page, IDisposable { public IShellPage AppInstance = null; @@ -15,7 +17,7 @@ public abstract class PropertiesTab : Page public SelectedItemsPropertiesViewModel ViewModel { get; set; } - protected Microsoft.UI.Xaml.Controls.ProgressBar ItemMD5HashProgress = null; + protected IProgress hashProgress; protected virtual void Properties_Loaded(object sender, RoutedEventArgs e) { @@ -40,7 +42,7 @@ protected override void OnNavigatedTo(NavigationEventArgs e) { if (item.PrimaryItemAttribute == StorageItemTypes.File) { - BaseProperties = new FileProperties(ViewModel, np.tokenSource, Dispatcher, ItemMD5HashProgress, item, AppInstance); + BaseProperties = new FileProperties(ViewModel, np.tokenSource, Dispatcher, hashProgress, item, AppInstance); } else if (item.PrimaryItemAttribute == StorageItemTypes.Folder) { @@ -68,5 +70,13 @@ protected override void OnNavigatedFrom(NavigationEventArgs e) base.OnNavigatedFrom(e); } + + /// + /// Tries to save changed properties to file. + /// + /// Returns true if properties have been saved successfully. + public abstract Task SaveChangesAsync(ListedItem item); + + public abstract void Dispose(); } } \ No newline at end of file diff --git a/Files/Views/Pages/Properties.xaml.cs b/Files/Views/Pages/Properties.xaml.cs index 4abd65c431b6..c873eec9022d 100644 --- a/Files/Views/Pages/Properties.xaml.cs +++ b/Files/Views/Pages/Properties.xaml.cs @@ -2,6 +2,7 @@ using Files.Helpers; using Files.Helpers.XamlHelpers; using Files.ViewModels; +using Files.ViewModels.Properties; using Microsoft.Toolkit.Uwp.Helpers; using System; using System.Threading; @@ -141,21 +142,15 @@ private async void OKButton_Click(object sender, RoutedEventArgs e) { await propertiesGeneral.SaveChangesAsync(listedItem); } - else if (contentFrame.Content is PropertiesLibrary propertiesLibrary) - { - if (!await propertiesLibrary.SaveChangesAsync()) - { - return; - } - } - else if (contentFrame.Content is PropertiesDetails propertiesDetails) + else { - if (!await propertiesDetails.SaveChangesAsync()) + if (!await (contentFrame.Content as PropertiesTab).SaveChangesAsync(listedItem)) { return; } } + (contentFrame.Content as PropertiesTab).Dispose(); if (ApiInformation.IsApiContractPresent("Windows.Foundation.UniversalApiContract", 8)) { await ApplicationView.GetForCurrentView().TryConsolidateAsync(); diff --git a/Files/Views/Pages/PropertiesDetails.xaml.cs b/Files/Views/Pages/PropertiesDetails.xaml.cs index b0e928aead15..386570fc44c8 100644 --- a/Files/Views/Pages/PropertiesDetails.xaml.cs +++ b/Files/Views/Pages/PropertiesDetails.xaml.cs @@ -1,5 +1,6 @@ using Files.Dialogs; using Files.Enums; +using Files.Filesystem; using Files.Helpers; using Files.ViewModels.Properties; using System; @@ -29,11 +30,7 @@ protected override void Properties_Loaded(object sender, RoutedEventArgs e) } } - /// - /// Tries to save changed properties to file. - /// - /// Returns true if properties have been saved successfully. - public async Task SaveChangesAsync() + public override async Task SaveChangesAsync(ListedItem item) { while (true) { @@ -72,5 +69,9 @@ private async void ClearPropertiesConfirmation_Click(object sender, RoutedEventA ClearPropertiesFlyout.Hide(); await (BaseProperties as FileProperties).ClearPropertiesAsync(); } + + public override void Dispose() + { + } } } \ No newline at end of file diff --git a/Files/Views/Pages/PropertiesGeneral.xaml.cs b/Files/Views/Pages/PropertiesGeneral.xaml.cs index 0b1095f20546..e76de87f3aab 100644 --- a/Files/Views/Pages/PropertiesGeneral.xaml.cs +++ b/Files/Views/Pages/PropertiesGeneral.xaml.cs @@ -3,6 +3,7 @@ using Files.Helpers; using Files.ViewModels.Properties; using Microsoft.Toolkit.Uwp; +using System; using System.IO; using System.Threading.Tasks; using Windows.ApplicationModel.Core; @@ -15,10 +16,17 @@ public sealed partial class PropertiesGeneral : PropertiesTab public PropertiesGeneral() { this.InitializeComponent(); - base.ItemMD5HashProgress = ItemMD5HashProgress; + base.hashProgress = new Progress(); + + (base.hashProgress as Progress).ProgressChanged += PropertiesGeneral_ProgressChanged; } - public async Task SaveChangesAsync(ListedItem item) + private void PropertiesGeneral_ProgressChanged(object sender, float e) + { + ItemMD5HashProgress.Value = (double)e; + } + + public override async Task SaveChangesAsync(ListedItem item) { if (BaseProperties is DriveProperties driveProps) { @@ -38,6 +46,7 @@ public async Task SaveChangesAsync(ListedItem item) await drive.UpdateLabelAsync(); await AppInstance.FilesystemViewModel?.SetWorkingDirectoryAsync(drive.Path); }); + return true; } } } @@ -58,6 +67,7 @@ public async Task SaveChangesAsync(ListedItem item) { await AppInstance.FilesystemViewModel?.SetWorkingDirectoryAsync(newPath); }); + return true; } } } @@ -66,7 +76,7 @@ public async Task SaveChangesAsync(ListedItem item) { if (!string.IsNullOrWhiteSpace(ViewModel.ItemName) && ViewModel.OriginalItemName != ViewModel.ItemName) { - await CoreApplication.MainView.DispatcherQueue.EnqueueAsync(() => UIFilesystemHelpers.RenameFileItemAsync(item, + return await CoreApplication.MainView.DispatcherQueue.EnqueueAsync(() => UIFilesystemHelpers.RenameFileItemAsync(item, ViewModel.OriginalItemName, ViewModel.ItemName, AppInstance)); @@ -80,13 +90,23 @@ await CoreApplication.MainView.DispatcherQueue.EnqueueAsync(() => UIFilesystemHe { await CoreApplication.MainView.DispatcherQueue.EnqueueAsync(() => UIFilesystemHelpers.SetHiddenAttributeItem(fileOrFolder, ViewModel.IsHidden, AppInstance.SlimContentPage.ItemManipulationModel)); } + return true; } else { // Handle the visibility attribute for a single file await CoreApplication.MainView.DispatcherQueue.EnqueueAsync(() => UIFilesystemHelpers.SetHiddenAttributeItem(item, ViewModel.IsHidden, AppInstance.SlimContentPage.ItemManipulationModel)); + + return true; } } + + return false; + } + + public override void Dispose() + { + (base.hashProgress as Progress).ProgressChanged -= PropertiesGeneral_ProgressChanged; } } } \ No newline at end of file diff --git a/Files/Views/Pages/PropertiesLibrary.xaml.cs b/Files/Views/Pages/PropertiesLibrary.xaml.cs index 6a5ec85801c2..9185f390599d 100644 --- a/Files/Views/Pages/PropertiesLibrary.xaml.cs +++ b/Files/Views/Pages/PropertiesLibrary.xaml.cs @@ -170,11 +170,7 @@ private bool IsChanged(LibraryItem lib, out string newDefaultSaveFolder, out str return isChanged; } - /// - /// Tries to save changed properties to file. - /// - /// Returns true if properties have been saved successfully. - public async Task SaveChangesAsync() + public override async Task SaveChangesAsync(ListedItem item) { if (BaseProperties is LibraryProperties props) { @@ -227,6 +223,10 @@ public async Task SaveChangesAsync() return false; } + public override void Dispose() + { + } + public class LibraryFolder { public string Path { get; set; } diff --git a/Files/Views/Pages/PropertiesShortcut.xaml.cs b/Files/Views/Pages/PropertiesShortcut.xaml.cs index 1f4e81bd1ca5..aeebd79e86dc 100644 --- a/Files/Views/Pages/PropertiesShortcut.xaml.cs +++ b/Files/Views/Pages/PropertiesShortcut.xaml.cs @@ -1,4 +1,6 @@ -using Files.ViewModels.Properties; +using Files.Filesystem; +using Files.ViewModels.Properties; +using System.Threading.Tasks; namespace Files.Views { @@ -8,5 +10,13 @@ public PropertiesShortcut() { this.InitializeComponent(); } + + public async override Task SaveChangesAsync(ListedItem item) + { + return false; + } + public override void Dispose() + { + } } } \ No newline at end of file