-
Notifications
You must be signed in to change notification settings - Fork 0
/
AkahaGestureModel.cs
277 lines (257 loc) · 9.96 KB
/
AkahaGestureModel.cs
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
using System;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Input;
using System.Windows.Threading;
using System.IO;
using System.Linq;
using Akaha_Gesture.Stats;
namespace Akaha_Gesture
{
public class AkahaGestureModel : INotifyPropertyChanged {
public List<string> fileNames { get; private set; }
private int _secondsPerImage;
public int secondsPerImage {
get => _secondsPerImage;
set {
_secondsPerImage = value;
onPropertyChanged("secondsPerImage");
savePref("secondsPerImage", value);
}
}
private int _imageCount;
public int imageCount {
get => _imageCount;
set {
_imageCount = value;
if(value != 0) {
onPropertyChanged("imageCount");
savePref("imageCount", value);
}
}
}
private bool _started;
private bool started {
get => _started;
set {
_started = value;
onPropertyChanged("isRunning");
onPropertyChanged("isStarted");
onPropertyChanged("notStarted");
}
}
private bool _isCountdown = false;
public bool isCountdown {
get => _isCountdown;
private set {
_isCountdown = value;
onPropertyChanged("isCountdown");
onPropertyChanged("isRunning");
}
}
public bool isRunning {
get => started && !isCountdown;
}
private bool _autoMode = true;
public bool autoMode {
get => _autoMode;
set {
_autoMode = value;
onPropertyChanged("autoMode");
onPropertyChanged("manualMode");
onPropertyChanged("selectTimeEnabled");
onPropertyChanged("nextPrevEnabled");
savePref("autoMode", value ? 1 : 0);
}
}
public bool manualMode => !autoMode;
public bool selectTimeEnabled => !manualMode && !started;
public bool nextPrevEnabled => manualMode && isRunning;
public bool notStarted => !started;
public bool isStarted => started;
public ICommand selectFilesCommand { get; private set; }
public ICommand startCommand { get; private set; }
public ICommand stopCommand { get; private set; }
public ICommand nextImageCommand { get; private set; }
public ICommand prevImageCommand { get; private set; }
public ObservableCollection<string> sessionImages { get; private set; }
private int? m_currentImageIndex;
private int? currentImageIndex {
get => m_currentImageIndex;
set {
m_currentImageIndex = value;
onPropertyChanged("currentImage");
onPropertyChanged("currentImageStr");
}
}
public string currentImageStr {
get {
return string.Format("{0} / {1}", this.currentImageIndex + 1, this.imageCount);
}
}
public string currentImage {
get {
if (currentImageIndex.HasValue) {
return sessionImages[currentImageIndex.Value];
}
return "";
}
}
private DateTime currentImageStarted;
public double currentImageProgress {
get {
return (DateTime.UtcNow.Subtract(currentImageStarted).TotalSeconds / secondsPerImage) * 100.0;
}
}
DispatcherTimer timer = null;
DispatcherTimer countdownTimer = null;
private int m_countdown;
private int countdown {
get => m_countdown;
set {
m_countdown = value;
onPropertyChanged("countdownText");
}
}
public string countdownText {
get {
return countdown.ToString();
}
}
public event PropertyChangedEventHandler PropertyChanged;
readonly static string appDataPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "AkahaGesture");
readonly static string lastImagesFilePath = Path.Combine(appDataPath, "lastImages.txt");
private SessionRepository sessionRepository = new SessionRepository();
private Session lastSession = null;
public AkahaGestureModel() {
this.fileNames = new List<string>();
this.sessionImages = new ObservableCollection<string>();
this.selectFilesCommand = new SelectFilesComamnd(this);
this.startCommand = new StartCommand(this);
this.stopCommand = new StopCommand(this);
this.nextImageCommand = new NextImageCommand(this);
this.prevImageCommand = new PrevImageCommand(this);
loadPreferences();
}
private void savePref(string pref, int value) {
try {
var brandKey = Microsoft.Win32.Registry.CurrentUser.CreateSubKey(@"SOFTWARE\AkahaSoftware");
var key = brandKey.CreateSubKey("AkahaGesture");
key.SetValue(pref, value, Microsoft.Win32.RegistryValueKind.DWord);
key.Close();
brandKey.Close();
} catch(Exception e) {
Console.Error.WriteLine(e);
}
}
public void saveLastImages() {
try {
Directory.CreateDirectory(appDataPath);
File.WriteAllLines(lastImagesFilePath, this.fileNames);
} catch(Exception e) {
Console.Error.WriteLine(e);
}
}
private void loadPreferences() {
try {
var key = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(@"SOFTWARE\AkahaSoftware\AkahaGesture");
this.secondsPerImage = (int)key.GetValue("secondsPerImage", 60);
this.imageCount = (int)key.GetValue("imageCount", 10);
this.autoMode = (int)key.GetValue("autoMode", 1) == 1;
key.Close();
} catch(Exception e) {
Console.Error.WriteLine(e);
}
string lastImagesFilePath = Path.Combine(appDataPath, "lastImages.txt");
if (File.Exists(lastImagesFilePath)) {
string[] lines = File.ReadAllLines(lastImagesFilePath);
foreach (string line in lines) {
if (File.Exists(line)) {
this.fileNames.Add(line);
}
}
}
}
public void start() {
if(this.sessionImages.Count <= 0) return;
stop();
this.started = true;
this.isCountdown = true;
lastSession = null;
countdown = 3;
countdownTimer = new DispatcherTimer();
countdownTimer.Interval = new TimeSpan(0, 0, 0, 1, 0);
countdownTimer.Tick += new EventHandler(onCountdownTick);
countdownTimer.Start();
}
private void onCountdownTick(object sender, EventArgs e) {
countdown--;
if (countdownTimer == null) return;
if(countdown <= 0) {
isCountdown = false;
countdownTimer.Stop();
countdownTimer = null;
if (started) {
this.currentImageIndex = 0;
currentImageStarted = DateTime.UtcNow;
lastSession = new Session(DateTime.UtcNow, sessionImages.Count, autoMode ? secondsPerImage : 0);
lastSession.AddImage(new Image { path = sessionImages[this.currentImageIndex.Value] });
if (autoMode) {
timer = new DispatcherTimer();
timer.Interval = new TimeSpan(0, 0, 0, 0, 10);
timer.Tick += new EventHandler(onTick);
timer.Start();
}
}
}
}
private void onTick(object sender, EventArgs e) {
Console.WriteLine(currentImageProgress);
if(currentImageProgress >= 100) {
this.nextImage();
}
this.onPropertyChanged("currentImageProgress");
}
public void stop() {
this.started = false;
int? lastIdx = this.currentImageIndex;
this.currentImageIndex = null;
isCountdown = false;
countdownTimer?.Stop();
countdownTimer = null;
if(timer != null) {
timer.Stop();
timer = null;
}
if(lastIdx.HasValue) {
lastSession.end = DateTime.UtcNow;
sessionRepository.addSession(lastSession);
showSummary(this.sessionImages.ToList().GetRange(0, lastIdx.Value+1));
}
}
public void nextImage() {
if(!this.currentImageIndex.HasValue) return;
if(this.currentImageIndex >= this.sessionImages.Count-1) {
stop();
} else {
this.currentImageStarted = DateTime.UtcNow;
this.currentImageIndex++;
lastSession.AddImage(new Image { path = sessionImages[this.currentImageIndex.Value] });
}
}
public void prevImage() {
if(!this.currentImageIndex.HasValue) return;
if(this.currentImageIndex > 0) {
this.currentImageIndex--;
}
}
public void showSummary(List<string> images) {
var summary = new SummaryWindow(images);
summary.Show();
}
private void onPropertyChanged(string propertyName = null) {
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}