-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainWindowViewModel.cs
More file actions
129 lines (109 loc) · 3.34 KB
/
Copy pathMainWindowViewModel.cs
File metadata and controls
129 lines (109 loc) · 3.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
using Avalonia.Threading;
using HashComputer.Backend;
using HashComputer.Backend.Services;
using Hypocrite.Core.Container;
using Hypocrite.Core.Mvvm.Attributes;
using Hypocrite.Mvvm;
using Prism.Commands;
using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Threading;
using System.Windows.Input;
namespace HashComputer
{
public class MainWindowViewModel : ViewModelBase
{
public MainWindowViewModel()
{
ComputeHashCommand = new DelegateCommand(OnComputeHashCommand);
CancelCommand = new DelegateCommand(OnCancelCommand);
}
private async void OnComputeHashCommand()
{
var parameters = new Backend.ComputeParameters()
{
Path = FolderPath,
Version = VersionText,
TaskNumber = TaskNumber,
StableFilesPath = StableFilePath,
};
IsDoneVisible = false;
IsFailureVisible = false;
IsProgressVisible = true;
IsInteractionEnabled = false;
lock (_currentFilesLock)
CurrentFiles.AddRange(Enumerable.Repeat("", parameters.TaskNumber));
DiffText = string.Empty; // reset
_currentCancellationToken = new CancellationTokenSource();
var result = await ComputerService.ComputeHash(
parameters,
(val) =>
{
Dispatcher.UIThread.Invoke(() =>
{
CurrentProgress = val.Progress;
UpdateCurrentFileStatus(val.ThreadNumber, val.Message);
});
},
_currentCancellationToken.Token
);
IsDoneVisible = result.Item1;
IsFailureVisible = !result.Item1;
bool isDiffTextVisible = !string.IsNullOrWhiteSpace(result.Item2) && result.Item1;
// if done correctly and not empty - Item2 is diff
if (isDiffTextVisible)
DiffText = "There was already a hash file. Here are the files that changed or not presented yet:\n";
else if (IsDoneVisible)
DiffText = "Done!"; // just write "Done" on success
DiffText += result.Item2;
IsProgressVisible = false;
IsInteractionEnabled = true;
lock (_currentFilesLock)
CurrentFiles.Clear();
}
private void UpdateCurrentFileStatus(int num, string message)
{
lock (_currentFilesLock)
{
if (CurrentFiles.Count > 0 && num > 0)
CurrentFiles[num - 1] = $"Task {num}: {message}";
}
}
private void OnCancelCommand()
{
_currentCancellationToken?.Cancel();
}
private CancellationTokenSource _currentCancellationToken;
[Injection]
IComputerService ComputerService { get; set; }
[Notify]
public string FolderPath { get; set; }
[Notify]
public string VersionText { get; set; } = ComputeParameters.DEFAULT_VERSION;
[Notify]
public int TaskNumber { get; set; } = ComputeParameters.DEFAULT_TASK_NUMBER;
[Notify]
public string OutFileName { get; set; } = ComputeParameters.DEFAULT_HASH_FILENAME;
[Notify]
public string StableFilePath { get; set; }
[Notify]
public ICommand ComputeHashCommand { get; set; }
[Notify]
public ICommand CancelCommand { get; set; }
[Notify]
public bool IsInteractionEnabled { get; set; } = true;
[Notify]
public int CurrentProgress { get; set; }
[Notify]
public bool IsProgressVisible { get; set; }
[Notify]
public bool IsDoneVisible { get; set; }
[Notify]
public bool IsFailureVisible { get; set; }
[Notify]
public string DiffText { get; set; }
private object _currentFilesLock = new object();
public ObservableCollection<string> CurrentFiles { get; set; } = new ObservableCollection<string>();
}
}