Skip to content

Commit 59249e4

Browse files
committed
MVVM
*actual* mvvm this time
1 parent 795f74e commit 59249e4

File tree

3 files changed

+91
-53
lines changed

3 files changed

+91
-53
lines changed

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

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the MIT License.
33

44
using Files.Shared.Helpers;
5+
using Microsoft.UI.Xaml.Controls;
56
using System.IO;
67
using System.Security.Cryptography;
78
using System.Windows.Input;
@@ -25,11 +26,41 @@ public HashInfoItem SelectedItem
2526
public Dictionary<string, bool> ShowHashes { get; private set; }
2627

2728
public ICommand ToggleIsEnabledCommand { get; private set; }
29+
public ICommand HashInputTextChangedCommand { get; private set; }
30+
public ICommand CompareFileCommand { get; private set; }
2831

2932
private ListedItem _item;
3033

3134
private CancellationTokenSource _cancellationTokenSource;
3235

36+
private string _hashInput;
37+
public string HashInput
38+
{
39+
get => _hashInput;
40+
set => SetProperty(ref _hashInput, value);
41+
}
42+
43+
private InfoBarSeverity _infoBarSeverity;
44+
public InfoBarSeverity InfoBarSeverity
45+
{
46+
get => _infoBarSeverity;
47+
set => SetProperty(ref _infoBarSeverity, value);
48+
}
49+
50+
private string _infoBarTitle;
51+
public string InfoBarTitle
52+
{
53+
get => _infoBarTitle;
54+
set => SetProperty(ref _infoBarTitle, value);
55+
}
56+
57+
private bool _isInfoBarOpen;
58+
public bool IsInfoBarOpen
59+
{
60+
get => _isInfoBarOpen;
61+
set => SetProperty(ref _isInfoBarOpen, value);
62+
}
63+
3364
public HashesViewModel(ListedItem item)
3465
{
3566
ToggleIsEnabledCommand = new RelayCommand<string>(ToggleIsEnabled);
@@ -57,6 +88,9 @@ public HashesViewModel(ListedItem item)
5788
ShowHashes.TryAdd("SHA512", false);
5889

5990
Hashes.Where(x => ShowHashes[x.Algorithm]).ForEach(x => ToggleIsEnabledCommand.Execute(x.Algorithm));
91+
92+
HashInputTextChangedCommand = new RelayCommand(OnHashInputTextChanged);
93+
CompareFileCommand = new RelayCommand(async () => await OnCompareFileAsync());
6094
}
6195

6296
private void ToggleIsEnabled(string? algorithm)
@@ -183,6 +217,51 @@ public async Task<string> CalculateFileHashAsync(string filePath)
183217
}
184218
}
185219

220+
private void OnHashInputTextChanged()
221+
{
222+
string? matchingAlgorithm = null;
223+
224+
try
225+
{
226+
matchingAlgorithm = FindMatchingAlgorithm(HashInput);
227+
}
228+
catch (ArgumentNullException)
229+
{
230+
return;
231+
}
232+
233+
if (string.IsNullOrEmpty(matchingAlgorithm))
234+
{
235+
InfoBarSeverity = InfoBarSeverity.Error;
236+
InfoBarTitle = Strings.HashesDoNotMatch.GetLocalizedResource();
237+
IsInfoBarOpen = true;
238+
}
239+
else
240+
{
241+
InfoBarSeverity = InfoBarSeverity.Success;
242+
InfoBarTitle = string.Format(Strings.HashesMatch.GetLocalizedResource(), matchingAlgorithm);
243+
IsInfoBarOpen = true;
244+
}
245+
}
246+
247+
private async Task OnCompareFileAsync()
248+
{
249+
var result = await CompareFileAsync();
250+
251+
if (result)
252+
{
253+
InfoBarSeverity = InfoBarSeverity.Success; // Check mark
254+
InfoBarTitle = Strings.HashesMatch.GetLocalizedResource();
255+
}
256+
else
257+
{
258+
InfoBarSeverity = InfoBarSeverity.Error; // Cross mark
259+
InfoBarTitle = Strings.HashesDoNotMatch.GetLocalizedResource();
260+
}
261+
262+
IsInfoBarOpen = true;
263+
}
264+
186265
public void Dispose()
187266
{
188267
_cancellationTokenSource.Cancel();

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

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
77
xmlns:datamodels="using:Files.App.Data.Models"
88
xmlns:helpers="using:Files.App.Helpers"
9+
xmlns:interactivity="using:Microsoft.Xaml.Interactivity"
910
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
1011
xmlns:toolkitconverters="using:CommunityToolkit.WinUI.Converters"
1112
xmlns:vm="using:Files.App.ViewModels.Properties"
@@ -57,7 +58,13 @@
5758
x:Name="HashInputTextBox"
5859
Grid.Column="0"
5960
PlaceholderText="{helpers:ResourceString Name=EnterHashToCompare}"
60-
TextChanged="HashInputTextBox_TextChanged" />
61+
Text="{x:Bind HashesViewModel.HashInput, Mode=TwoWay}">
62+
<interactivity:Interaction.Behaviors>
63+
<interactivity:EventTriggerBehavior EventName="TextChanged">
64+
<interactivity:InvokeCommandAction Command="{x:Bind HashesViewModel.HashInputTextChangedCommand}" />
65+
</interactivity:EventTriggerBehavior>
66+
</interactivity:Interaction.Behaviors>
67+
</TextBox>
6168
</Grid>
6269

6370
<!-- Compare File Button -->
@@ -66,7 +73,7 @@
6673
Grid.Row="0"
6774
Grid.Column="1"
6875
Margin="12"
69-
Click="CompareFileButton_Click"
76+
Command="{x:Bind HashesViewModel.CompareFileCommand}"
7077
Content="{helpers:ResourceString Name=CompareFile}" />
7178
</Grid>
7279

@@ -76,9 +83,9 @@
7683
Grid.Column="0"
7784
Margin="12,75,12,12"
7885
IsClosable="False"
79-
IsOpen="False"
80-
Message="No hashes match the provided hash."
81-
Severity="Informational" />
86+
IsOpen="{x:Bind HashesViewModel.IsInfoBarOpen, Mode=TwoWay}"
87+
Message="{x:Bind HashesViewModel.InfoBarTitle, Mode=TwoWay}"
88+
Severity="{x:Bind HashesViewModel.InfoBarSeverity, Mode=TwoWay}" />
8289

8390
<Grid
8491
x:Name="HashesListGrid"

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

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -48,54 +48,6 @@ private void ToggleMenuFlyoutItem_Click(object sender, RoutedEventArgs e)
4848
_cancel = true;
4949
}
5050

51-
private void HashInputTextBox_TextChanged(object sender, TextChangedEventArgs e)
52-
{
53-
string? matchingAlgorithm = null;
54-
55-
try
56-
{
57-
matchingAlgorithm = HashesViewModel.FindMatchingAlgorithm(HashInputTextBox.Text);
58-
}
59-
catch (ArgumentNullException)
60-
{
61-
return;
62-
}
63-
64-
if (string.IsNullOrEmpty(matchingAlgorithm))
65-
{
66-
HashMatchInfoBar.Severity = InfoBarSeverity.Error;
67-
HashMatchInfoBar.Title = Strings.HashesDoNotMatch.GetLocalizedResource();
68-
HashMatchInfoBar.IsOpen = true;
69-
return;
70-
}
71-
else
72-
{
73-
HashMatchInfoBar.Severity = InfoBarSeverity.Success;
74-
HashMatchInfoBar.Title = string.Format(Strings.HashesMatch.GetLocalizedResource(), matchingAlgorithm);
75-
HashMatchInfoBar.IsOpen = true;
76-
return;
77-
}
78-
}
79-
80-
81-
private async void CompareFileButton_Click(object sender, RoutedEventArgs e)
82-
{
83-
var result = await HashesViewModel.CompareFileAsync();
84-
85-
if (result)
86-
{
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";
94-
}
95-
96-
HashMatchInfoBar.IsOpen = true;
97-
}
98-
9951
private void MenuFlyout_Closing(FlyoutBase sender, FlyoutBaseClosingEventArgs e)
10052
{
10153
e.Cancel = _cancel;

0 commit comments

Comments
 (0)