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

Improved Properties Dialog codebase #4468

Merged
merged 6 commits into from
Apr 14, 2021
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
15 changes: 6 additions & 9 deletions Files/ViewModels/Properties/FileProperties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ namespace Files.ViewModels.Properties
{
public class FileProperties : BaseProperties
{
private ProgressBar ProgressBar;
private IProgress<float> 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<float> hashProgress, ListedItem item, IShellPage instance)
{
ViewModel = viewModel;
TokenSource = tokenSource;
ProgressBar = progressBar;
this.hashProgress = hashProgress;
Dispatcher = coreDispatcher;
Item = item;
AppInstance = instance;
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -341,7 +341,7 @@ private async void ViewModel_PropertyChanged(object sender, System.ComponentMode
}
}

private async Task<string> GetHashForFileAsync(ListedItem fileItem, string nameOfAlg, CancellationToken token, ProgressBar progress, IShellPage associatedInstance)
private async Task<string> GetHashForFileAsync(ListedItem fileItem, string nameOfAlg, CancellationToken token, IProgress<float> progress, IShellPage associatedInstance)
{
HashAlgorithmProvider algorithmProvider = HashAlgorithmProvider.OpenAlgorithm(nameOfAlg);
StorageFile file = await StorageItemHelpers.ToStorageItem<StorageFile>((fileItem as ShortcutItem)?.TargetPath ?? fileItem.ItemPath, associatedInstance);
Expand Down Expand Up @@ -382,10 +382,7 @@ private async Task<string> 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();
Expand Down
16 changes: 13 additions & 3 deletions Files/ViewModels/Properties/PropertiesTab.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
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;
using Windows.UI.Xaml.Navigation;

namespace Files.ViewModels.Properties
{
public abstract class PropertiesTab : Page
public abstract class PropertiesTab : Page, IDisposable
{
public IShellPage AppInstance = null;

public BaseProperties BaseProperties { get; set; }

public SelectedItemsPropertiesViewModel ViewModel { get; set; }

protected Microsoft.UI.Xaml.Controls.ProgressBar ItemMD5HashProgress = null;
protected IProgress<float> hashProgress;

protected virtual void Properties_Loaded(object sender, RoutedEventArgs e)
{
Expand All @@ -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)
{
Expand Down Expand Up @@ -68,5 +70,13 @@ protected override void OnNavigatedFrom(NavigationEventArgs e)

base.OnNavigatedFrom(e);
}

/// <summary>
/// Tries to save changed properties to file.
/// </summary>
/// <returns>Returns true if properties have been saved successfully.</returns>
public abstract Task<bool> SaveChangesAsync(ListedItem item);

public abstract void Dispose();
}
}
13 changes: 4 additions & 9 deletions Files/Views/Pages/Properties.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
11 changes: 6 additions & 5 deletions Files/Views/Pages/PropertiesDetails.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Files.Dialogs;
using Files.Enums;
using Files.Filesystem;
using Files.Helpers;
using Files.ViewModels.Properties;
using System;
Expand Down Expand Up @@ -29,11 +30,7 @@ protected override void Properties_Loaded(object sender, RoutedEventArgs e)
}
}

/// <summary>
/// Tries to save changed properties to file.
/// </summary>
/// <returns>Returns true if properties have been saved successfully.</returns>
public async Task<bool> SaveChangesAsync()
public override async Task<bool> SaveChangesAsync(ListedItem item)
{
while (true)
{
Expand Down Expand Up @@ -72,5 +69,9 @@ private async void ClearPropertiesConfirmation_Click(object sender, RoutedEventA
ClearPropertiesFlyout.Hide();
await (BaseProperties as FileProperties).ClearPropertiesAsync();
}

public override void Dispose()
{
}
}
}
26 changes: 23 additions & 3 deletions Files/Views/Pages/PropertiesGeneral.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -15,10 +16,17 @@ public sealed partial class PropertiesGeneral : PropertiesTab
public PropertiesGeneral()
{
this.InitializeComponent();
base.ItemMD5HashProgress = ItemMD5HashProgress;
base.hashProgress = new Progress<float>();

(base.hashProgress as Progress<float>).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<bool> SaveChangesAsync(ListedItem item)
{
if (BaseProperties is DriveProperties driveProps)
{
Expand All @@ -38,6 +46,7 @@ public async Task SaveChangesAsync(ListedItem item)
await drive.UpdateLabelAsync();
await AppInstance.FilesystemViewModel?.SetWorkingDirectoryAsync(drive.Path);
});
return true;
}
}
}
Expand All @@ -58,6 +67,7 @@ public async Task SaveChangesAsync(ListedItem item)
{
await AppInstance.FilesystemViewModel?.SetWorkingDirectoryAsync(newPath);
});
return true;
}
}
}
Expand All @@ -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));
Expand All @@ -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<float>).ProgressChanged -= PropertiesGeneral_ProgressChanged;
}
}
}
10 changes: 5 additions & 5 deletions Files/Views/Pages/PropertiesLibrary.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,11 +170,7 @@ private bool IsChanged(LibraryItem lib, out string newDefaultSaveFolder, out str
return isChanged;
}

/// <summary>
/// Tries to save changed properties to file.
/// </summary>
/// <returns>Returns true if properties have been saved successfully.</returns>
public async Task<bool> SaveChangesAsync()
public override async Task<bool> SaveChangesAsync(ListedItem item)
{
if (BaseProperties is LibraryProperties props)
{
Expand Down Expand Up @@ -227,6 +223,10 @@ public async Task<bool> SaveChangesAsync()
return false;
}

public override void Dispose()
{
}

public class LibraryFolder
{
public string Path { get; set; }
Expand Down
12 changes: 11 additions & 1 deletion Files/Views/Pages/PropertiesShortcut.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using Files.ViewModels.Properties;
using Files.Filesystem;
using Files.ViewModels.Properties;
using System.Threading.Tasks;

namespace Files.Views
{
Expand All @@ -8,5 +10,13 @@ public PropertiesShortcut()
{
this.InitializeComponent();
}

public async override Task<bool> SaveChangesAsync(ListedItem item)
{
return false;
}
public override void Dispose()
{
}
}
}