Skip to content

Commit 681d920

Browse files
committed
Refactor file picker method
(partially) it's not working
1 parent 1720298 commit 681d920

File tree

2 files changed

+35
-22
lines changed

2 files changed

+35
-22
lines changed

src/Files.App/ViewModels/Properties/HashesViewModel.cs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) Files Community
1+
// Copyright (c) Files Community
22
// Licensed under the MIT License.
33

44
using Files.Shared.Helpers;
@@ -10,6 +10,7 @@ namespace Files.App.ViewModels.Properties
1010
{
1111
public sealed partial class HashesViewModel : ObservableObject, IDisposable
1212
{
13+
private ICommonDialogService CommonDialogService { get; } = Ioc.Default.GetRequiredService<ICommonDialogService>();
1314
private IUserSettingsService UserSettingsService { get; } = Ioc.Default.GetService<IUserSettingsService>()!;
1415

1516
private HashInfoItem _selectedItem;
@@ -132,6 +133,28 @@ public bool CompareHash(string algorithm, string hashToCompare)
132133
return hashInfoItem.HashValue.Equals(hashToCompare, StringComparison.OrdinalIgnoreCase);
133134
}
134135

136+
public async Task<bool> CompareFileAsync()
137+
{
138+
var result = CommonDialogService.Open_FileOpenDialog(MainWindow.Instance.WindowHandle, false, [], Environment.SpecialFolder.Desktop, out var toCompare);
139+
140+
if (!result)
141+
{
142+
return false;
143+
}
144+
145+
var file = await StorageHelpers.ToStorageItem<BaseStorageFolder>(toCompare);
146+
147+
if (file is not null)
148+
{
149+
var selectedFileHash = await CalculateSHA384HashAsync(file.Path);
150+
var compare = CompareHash("SHA384", selectedFileHash);
151+
152+
return compare;
153+
}
154+
155+
return false;
156+
}
157+
135158
public string FindMatchingAlgorithm(string hash)
136159
{
137160
if (string.IsNullOrEmpty(hash))

src/Files.App/Views/Properties/HashesPage.xaml.cs

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -80,30 +80,20 @@ private void HashInputTextBox_TextChanged(object sender, TextChangedEventArgs e)
8080

8181
private async void CompareFileButton_Click(object sender, RoutedEventArgs e)
8282
{
83-
var picker = new Windows.Storage.Pickers.FileOpenPicker();
84-
picker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.Desktop;
85-
picker.FileTypeFilter.Add("*");
86-
WinRT.Interop.InitializeWithWindow.Initialize(picker, MainWindow.Instance.WindowHandle);
83+
var result = await HashesViewModel.CompareFileAsync();
8784

88-
var file = await picker.PickSingleFileAsync();
89-
if (file != null)
85+
if (result)
9086
{
91-
var selectedFileHash = await HashesViewModel.CalculateSHA384HashAsync(file.Path);
92-
var currentFileHash = HashesViewModel.Hashes.FirstOrDefault(h => h.Algorithm == "SHA384")?.HashValue;
93-
HashInputTextBox.Text = selectedFileHash;
94-
if (selectedFileHash == currentFileHash)
95-
{
96-
HashMatchInfoBar.Severity = InfoBarSeverity.Success; // Check mark
97-
HashMatchInfoBar.Title = Strings.HashesMatch.GetLocalizedResource();
98-
}
99-
else
100-
{
101-
HashMatchInfoBar.Severity = InfoBarSeverity.Error; // Cross mark
102-
HashMatchInfoBar.Title = Strings.HashesMatch.GetLocalizedResource();
103-
}
104-
105-
HashMatchInfoBar.IsOpen = true;
87+
HashMatchInfoBar.Severity = InfoBarSeverity.Success; // Check mark
88+
HashMatchInfoBar.Title = Strings.HashesMatch.GetLocalizedResource();
89+
}
90+
else
91+
{
92+
HashMatchInfoBar.Severity = InfoBarSeverity.Error; // Cross mark
93+
HashMatchInfoBar.Title = "no";
10694
}
95+
96+
HashMatchInfoBar.IsOpen = true;
10797
}
10898

10999
private void MenuFlyout_Closing(FlyoutBase sender, FlyoutBaseClosingEventArgs e)

0 commit comments

Comments
 (0)