Skip to content

Commit f83f534

Browse files
yaira2Lamparter
authored andcommitted
Simplify code
1 parent 696c288 commit f83f534

File tree

1 file changed

+29
-61
lines changed

1 file changed

+29
-61
lines changed

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

Lines changed: 29 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ private void ToggleIsEnabled(string? algorithm)
107107
// Don't calculate hashes for online files
108108
if (_item.SyncStatusUI.SyncStatus is CloudDriveSyncStatus.FileOnline or CloudDriveSyncStatus.FolderOnline)
109109
{
110-
hashInfoItem.HashValue = "CalculationOnlineFileHashError".GetLocalizedResource();
110+
hashInfoItem.HashValue = Strings.CalculationOnlineFileHashError.GetLocalizedResource();
111111
return;
112112
}
113113

@@ -142,11 +142,11 @@ private void ToggleIsEnabled(string? algorithm)
142142
catch (IOException)
143143
{
144144
// File is currently open
145-
hashInfoItem.HashValue = "CalculationErrorFileIsOpen".GetLocalizedResource();
145+
hashInfoItem.HashValue = Strings.CalculationErrorFileIsOpen.GetLocalizedResource();
146146
}
147147
catch (Exception)
148148
{
149-
hashInfoItem.HashValue = "CalculationError".GetLocalizedResource();
149+
hashInfoItem.HashValue = Strings.CalculationError.GetLocalizedResource();
150150
}
151151
finally
152152
{
@@ -156,82 +156,50 @@ private void ToggleIsEnabled(string? algorithm)
156156
}
157157
}
158158

159-
public bool CompareHash(string algorithm, string hashToCompare)
160-
{
161-
var hashInfoItem = Hashes.FirstOrDefault(x => x.Algorithm == algorithm);
162-
if (hashInfoItem == null || hashInfoItem.HashValue == null)
163-
{
164-
return false;
165-
}
166-
167-
return hashInfoItem.HashValue.Equals(hashToCompare, StringComparison.OrdinalIgnoreCase);
168-
}
169-
170-
171159
public string FindMatchingAlgorithm(string hash)
172160
{
173161
if (string.IsNullOrEmpty(hash))
174-
throw new ArgumentNullException($"Invalid hash '{hash}' provided.");
162+
return string.Empty;
175163

176-
foreach (var hashInfo in Hashes)
177-
{
178-
if (hashInfo.HashValue != null && hashInfo.HashValue.Equals(hash, StringComparison.OrdinalIgnoreCase))
179-
{
180-
return hashInfo.Algorithm;
181-
}
182-
}
183-
184-
return string.Empty;
164+
return Hashes
165+
.FirstOrDefault(h => h.HashValue?.Equals(hash, StringComparison.OrdinalIgnoreCase) == true)
166+
?.Algorithm ?? string.Empty;
185167
}
186168

187169
public async Task<string> CalculateFileHashAsync(string filePath)
188170
{
189-
using (var stream = File.OpenRead(filePath))
190-
{
191-
using (var md5 = MD5.Create())
192-
{
193-
var hash = await Task.Run(() => md5.ComputeHash(stream));
194-
return BitConverter.ToString(hash).Replace("-", "").ToLowerInvariant();
195-
}
196-
}
171+
using var stream = File.OpenRead(filePath);
172+
using var md5 = MD5.Create();
173+
var hash = await Task.Run(() => md5.ComputeHash(stream));
174+
return BitConverter.ToString(hash).Replace("-", "").ToLowerInvariant();
197175
}
198176

199177
private void OnHashInputTextChanged()
200178
{
201-
string? matchingAlgorithm = null;
179+
string matchingAlgorithm = FindMatchingAlgorithm(HashInput);
202180

203-
try
204-
{
205-
matchingAlgorithm = FindMatchingAlgorithm(HashInput);
206-
}
207-
catch (ArgumentNullException)
208-
{
209-
return;
210-
}
181+
InfoBarSeverity = string.IsNullOrEmpty(matchingAlgorithm)
182+
? InfoBarSeverity.Error
183+
: InfoBarSeverity.Success;
211184

212-
if (string.IsNullOrEmpty(matchingAlgorithm))
213-
{
214-
InfoBarSeverity = InfoBarSeverity.Error;
215-
InfoBarTitle = Strings.HashesDoNotMatch.GetLocalizedResource();
216-
}
217-
else
218-
{
219-
InfoBarSeverity = InfoBarSeverity.Success;
220-
InfoBarTitle = string.Format(Strings.HashesMatch.GetLocalizedResource(), matchingAlgorithm);
221-
}
185+
InfoBarTitle = string.IsNullOrEmpty(matchingAlgorithm)
186+
? Strings.HashesDoNotMatch.GetLocalizedResource()
187+
: string.Format(Strings.HashesMatch.GetLocalizedResource(), matchingAlgorithm);
222188
}
223189

224190
private async Task OnCompareFileAsync()
225191
{
226-
var result = CommonDialogService.Open_FileOpenDialog(MainWindow.Instance.WindowHandle, false, [], Environment.SpecialFolder.Desktop, out var toCompare);
227-
228-
if (result && toCompare is not null)
229-
{
230-
var selectedFileHash = await CalculateFileHashAsync(toCompare);
231-
HashInput = selectedFileHash;
232-
}
233-
else
234-
HashInput = string.Empty;
192+
var result = CommonDialogService.Open_FileOpenDialog(
193+
MainWindow.Instance.WindowHandle,
194+
false,
195+
[],
196+
Environment.SpecialFolder.Desktop,
197+
out var filePath
198+
);
199+
200+
HashInput = result && filePath != null
201+
? await CalculateFileHashAsync(filePath)
202+
: string.Empty;
235203
}
236204

237205
public void Dispose()

0 commit comments

Comments
 (0)