-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComposeViewModel.cs
More file actions
104 lines (90 loc) · 2.88 KB
/
ComposeViewModel.cs
File metadata and controls
104 lines (90 loc) · 2.88 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
using System;
using System.Threading;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Threading;
using MVVM;
namespace Hackathon2020
{
public class ComposeViewModel : DialogViewModel
{
public ComposeViewModel(ViewModel viewModel, ChitterUser poster)
: base("Compose post")
{
PostCommand = new RelayCommand(canPost, onPost);
CancelCommand = new RelayCommand((x)=>true, onCancel);
_poster = poster;
_viewModel = viewModel;
Quality = Status.Unclassified;
}
public bool Result { get; private set; }
public string Message
{
get => _messageText;
set {
if (_messageText != value) {
_messageText = value;
OnPropertyChanged("Message");
if (!string.IsNullOrEmpty(value)) {
waitForIdle();
}
}
}
}
public Status Quality
{
get => _status;
set {
if (_status != value) {
_status = value;
OnPropertyChanged("QualityText");
OnPropertyChanged("QualityBrush");
}
}
}
public string QualityText => Utils.QualityText(_status);
public Brush QualityBrush => Utils.QualityBrush(_status);
public ICommand PostCommand { get; }
public ICommand CancelCommand { get; }
private void waitForIdle()
{
if (_idleTimer == null) {
_idleTimer = new DispatcherTimer();
_idleTimer.Interval = TimeSpan.FromMilliseconds(500);
_idleTimer.Tick += idleTimerOnTick;
}
_idleTimer.Stop();
_idleTimer.Start();
}
private void idleTimerOnTick(object sender, EventArgs e)
{
_idleTimer.Stop();
if (!string.IsNullOrEmpty(_messageText)) {
ThreadPool.QueueUserWorkItem((_) => { Quality = PostChecker.Run(_viewModel, _messageText); });
}
}
private bool canPost(object arg)
{
return !string.IsNullOrEmpty(_messageText) && (_status != Status.Unclassified);
}
private void onPost(object arg)
{
Result = true;
++_poster.PostCount;
if (_status == Status.Deplorable) {
++_poster.RedCount;
}
CloseDialog();
}
private void onCancel(object arg)
{
Result = false;
CloseDialog();
}
private string _messageText;
private volatile Status _status;
private readonly ChitterUser _poster;
private DispatcherTimer _idleTimer;
private readonly ViewModel _viewModel;
}
}