22// Licensed under the MIT License.
33
44using Files . Shared . Helpers ;
5+ using Microsoft . UI . Xaml . Controls ;
56using System . IO ;
67using System . Security . Cryptography ;
78using 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 ( ) ;
0 commit comments