-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainWindow.xaml.cs
More file actions
296 lines (258 loc) · 9.6 KB
/
MainWindow.xaml.cs
File metadata and controls
296 lines (258 loc) · 9.6 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
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Security.AccessControl;
using System.Timers;
using System.Windows;
using System.Windows.Controls;
using MoonPdfLib.MuPdf;
using Newtonsoft.Json;
using RestSharp;
using System.Windows.Forms;
using System.Windows.Media;
using MoonPdfLib;
namespace eTracker
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window, INotifyPropertyChanged
{
private int _currentId;
//read from app.config
private readonly string _key;
private System.Timers.Timer _timer;
private DateTime _currentTime;
private DateTime _startTime;
private string _currentTimeSpan;
private byte[] _fileBytes;
private MoonPdfPanel _moonPanel;
private List<string> _currentSynoList;
private DetailRecord _currentDetailRecord;
public string CurrentTimeSpan
{
get => _currentTimeSpan;
set
{
if (_currentTimeSpan == value)
return;
_currentTimeSpan = value;
OnPropertyChanged(nameof(CurrentTimeSpan));
}
}
public MainWindow()
{
InitializeComponent();
_key = ConfigurationManager.AppSettings["apiKey"];
this.DataContext = this;
CurrentTimeSpan = "0:00:00";
CreatePdfVwr();
}
private void StartBtn_Click(object sender, RoutedEventArgs e)
{
var modal = new ModalWindow();
modal.Owner = this;
modal.ShowDialog();
if (modal.Started)
{
var l1 = modal.L1Combo.SelectedValue;
var age = modal.AgeCombo.SelectedValue;
var newRecord = new Record
{
Age = (int)age,
L1 = (string)l1,
StartDate = DateTime.Now,
PDFFile = _fileBytes
};
using (var db = new DatabaseEntities())
{
db.Records.Add(newRecord);
db.SaveChanges();
_currentId = newRecord.Id;
}
translateTxt.Text = string.Empty;
btnTranslate.IsEnabled = true;
translateTxt.IsEnabled = true;
StartBtn.IsEnabled = false;
LoadFile.IsEnabled = false;
StopBtn.IsEnabled = true;
Start();
}
}
private void StopBtn_Click(object sender, RoutedEventArgs e)
{
using (var db = new DatabaseEntities())
{
var mRecord = db.Records.First(f => f.Id == _currentId);
mRecord.EndDate = DateTime.Now;
mRecord.Duration = CurrentTimeSpan;
db.SaveChanges();
}
translateTxt.Text = string.Empty;
btnTranslate.IsEnabled = false;
translateTxt.IsEnabled = false;
StartBtn.IsEnabled = false;
LoadFile.IsEnabled = true;
StopBtn.IsEnabled = false;
translatedTxtBlock.Text = string.Empty;
synoLst.ItemsSource = null;
Stop();
LoadFile.IsEnabled = true;
mainGrid.Children.Remove(_moonPanel);
CreatePdfVwr();
CurrentTimeSpan = "0:00:00";
var resultWindow = new ResultsWindow {Owner = this};
resultWindow.ShowDialog();
}
private void btnTranslate_Click(object sender, RoutedEventArgs e)
{
Translate();
}
private void CreatePdfVwr()
{
_moonPanel = new MoonPdfPanel
{
ViewType = ViewType.SinglePage,
Background = Brushes.LightGray
};
Grid.SetColumn(_moonPanel, 0);
Grid.SetRow(_moonPanel, 1);
mainGrid.Children.Add(_moonPanel);
}
private void Translate()
{
using (var db = new DatabaseEntities())
{
var record = db.Records.First(f => f.Id == _currentId);
Translator t = new Translator();
try
{
var txtToTranslate = this.translateTxt.Text.Trim();
var trnsTxt = t.Translate(txtToTranslate, "English", record.L1).Trim();
if (!string.IsNullOrEmpty(trnsTxt))
{
this.translatedTxtBlock.Text = $"{txtToTranslate} => {trnsTxt}";
var client = new RestClient("http://thesaurus.altervista.org");
var request = new RestRequest("thesaurus/v1", Method.GET);
request.AddParameter("key", _key);
request.AddParameter("word", txtToTranslate);
request.AddParameter("language", "en_US");
request.AddParameter("output", "json");
IRestResponse response = client.Execute(request);
var content = response.Content;
var objects = JsonConvert.DeserializeObject<Rootobject>(content);
var synonyms = objects
?.response
?.FirstOrDefault()
?.list
.synonyms
.Split('|')
.ToList();
var synoList = new List<string>();
synonyms?.ForEach(f =>
{
var i = f.IndexOf('(');
if (i > 0)
synoList.Add(f.Substring(0, i).Trim());
});
if (!synoList.Any())
synoList.Add("Synonyms not found");
_currentSynoList = synoList;
this.synoLst.ItemsSource = synoList;
_currentDetailRecord = new DetailRecord()
{
CurrentPageNo = _moonPanel.GetCurrentPageNumber(),
SuggestedSynonisms = string.Join("|", synoList),
UnknownWord = txtToTranslate,
TranslatedWords = trnsTxt,
TimeStamp = DateTime.Now,
RecordId = record.Id,
SelectedSynonism = "Synonym not selected"
};
db.DetailRecords.Add(_currentDetailRecord);
db.SaveChanges();
translateTxt.Text = string.Empty;
synoLst.SelectedItem = null;
}
}
catch (Exception ex)
{
System.Windows.MessageBox.Show(ex.Message);
}
}
}
private void Start()
{
_timer = new System.Timers.Timer(1000);
_timer.Elapsed += Elapsed;
_timer.Enabled = true; // Enable it
_startTime = DateTime.Now;
_timer.Start();
}
private void Stop()
{
_timer.Stop();
}
private void Elapsed(object sender, ElapsedEventArgs e)
{
_currentTime = e.SignalTime;
var duration = (_currentTime - _startTime);
CurrentTimeSpan = duration.ToString("g").Split(',').First();
}
public class Rootobject
{
public Response[] response { get; set; }
}
public class Response
{
public List list { get; set; }
}
public class List
{
public string category { get; set; }
public string synonyms { get; set; }
}
protected void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
public event PropertyChangedEventHandler PropertyChanged;
private void LoadFile_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog fdlg = new OpenFileDialog
{
Title = "Open PDF file",
Filter = "PDF Files (*.pdf)|",
RestoreDirectory = true,
};
if (fdlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
var filePath = fdlg.FileName;
_fileBytes = File.ReadAllBytes(filePath);
var source = new MemorySource(_fileBytes);
_moonPanel.Open(source);
StartBtn.IsEnabled = true;
}
}
private void SynoLst_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (e.AddedItems != null && e.AddedItems.Count > 0)
{
var selected = (string)e.AddedItems[0];
//if (selected != "Synonyms not found")
// {
using (var db = new DatabaseEntities())
{
var cDr = db.DetailRecords.First(f => f.Id == _currentDetailRecord.Id);
cDr.SelectedSynonism = selected;
_currentDetailRecord.SelectedSynonism = selected;
db.SaveChanges();
}
//}
}
}
}
}