-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStatsForm.cs
More file actions
240 lines (203 loc) · 9.63 KB
/
Copy pathStatsForm.cs
File metadata and controls
240 lines (203 loc) · 9.63 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
using System;
using System.Globalization;
using Eto.Drawing;
using Eto.Forms;
namespace devtime
{
public class StatsForm : Dialog
{
private readonly string _context;
private readonly DateTimePicker _fromPicker;
private readonly DateTimePicker _toPicker;
private readonly Label _hoursLabel;
private readonly Label _minutesLabel;
private readonly Label _secondsLabel;
private readonly Label _msLabel;
private readonly DropDown _formatDropDown;
private readonly CheckBox _exportDateCheck;
private readonly CheckBox _exportTimeCheck;
private readonly CheckBox _exportLogCheck;
private readonly DropDown _timeFormatDropDown;
private readonly DropDown _timeApproxDropDown;
public StatsForm(string context)
{
Title = "Stats";
Icon = IconLoader.LoadIcon("export.png");
Resizable = false;
_context = context;
_fromPicker = new DateTimePicker { Mode = DateTimePickerMode.Date, Width = 110 };
_toPicker = new DateTimePicker { Mode = DateTimePickerMode.Date, Width = 110 };
_fromPicker.ValueChanged += (s, e) => OnRangeChanged();
_toPicker.ValueChanged += (s, e) => OnRangeChanged();
string? oldest = DB.SelectOldestDate(context);
string? newest = DB.SelectNewestDate(context);
if (oldest == null || newest == null)
{
oldest = DateTime.Today.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture);
newest = oldest;
}
if (DateTime.TryParseExact(oldest, "yyyy-MM-dd", CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTime oDate))
{
_fromPicker.Value = oDate;
}
if (DateTime.TryParseExact(newest, "yyyy-MM-dd", CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTime nDate))
{
_toPicker.Value = nDate;
}
_hoursLabel = new Label();
_minutesLabel = new Label();
_secondsLabel = new Label();
_msLabel = new Label();
var workedGroup = new GroupBox { Text = "Worked Time", Width = 270, Height = 85 };
workedGroup.Content = new StackLayout
{
Padding = new Padding(8, 3),
Spacing = 2,
Items = { _hoursLabel, _minutesLabel, _secondsLabel, _msLabel }
};
_formatDropDown = new DropDown { Width = 110 };
foreach (var val in Enum.GetValues(typeof(Exporter.ExportFormat)))
{
_formatDropDown.Items.Add(val.ToString() ?? "");
}
_formatDropDown.SelectedIndex = 0;
_exportDateCheck = new CheckBox { Text = "Date", Checked = true };
_exportTimeCheck = new CheckBox { Text = "Logged time", Checked = true };
_exportLogCheck = new CheckBox { Text = "Log", Checked = true };
_timeFormatDropDown = new DropDown { Width = 110 };
foreach (var val in Enum.GetValues(typeof(Exporter.TimeFormat)))
{
_timeFormatDropDown.Items.Add(val.ToString() ?? "");
}
_timeFormatDropDown.SelectedIndex = 0;
_timeApproxDropDown = new DropDown { Width = 110 };
foreach (var val in Enum.GetValues(typeof(Exporter.TimeApproximation)))
{
_timeApproxDropDown.Items.Add(val.ToString() ?? "");
}
_timeApproxDropDown.SelectedIndex = 0;
_exportTimeCheck.CheckedChanged += (s, e) =>
{
_timeFormatDropDown.Enabled = _exportTimeCheck.Checked ?? false;
_timeApproxDropDown.Enabled = _exportTimeCheck.Checked ?? false;
};
var exportButton = new Button { Text = "Export", Width = 80, Height = 23 };
exportButton.Click += (s, e) => ExportClick();
var closeButton = new Button { Text = "Close", Width = 80, Height = 23 };
closeButton.Click += (s, e) => Close();
var dateRangeRow = new TableLayout
{
Spacing = new Size(5, 3),
Rows =
{
new TableRow(new Label { Text = "From:" }, _fromPicker),
new TableRow(new Label { Text = "To:" }, _toPicker)
}
};
var exportOptions = new TableLayout
{
Spacing = new Size(5, 3),
Rows =
{
new TableRow(new Label { Text = "Format:" }, _formatDropDown),
new TableRow(new Label { Text = "Fields:" }, new StackLayout { Orientation = Orientation.Horizontal, Spacing = 5, Items = { _exportDateCheck, _exportTimeCheck, _exportLogCheck } }),
new TableRow(new Label { Text = "Time unit:" }, _timeFormatDropDown),
new TableRow(new Label { Text = "Rounding:" }, _timeApproxDropDown)
}
};
var exportGroup = new GroupBox { Text = "Export" };
exportGroup.Content = new StackLayout
{
Padding = new Padding(8, 3),
Spacing = 5,
Items = { exportOptions, exportButton }
};
Content = new StackLayout
{
Padding = new Padding(8, 5),
Spacing = 5,
HorizontalContentAlignment = HorizontalAlignment.Stretch,
Items =
{
dateRangeRow,
workedGroup,
exportGroup,
new StackLayout { HorizontalContentAlignment = HorizontalAlignment.Right, Items = { closeButton } }
}
};
OnRangeChanged();
}
private void OnRangeChanged()
{
if (_fromPicker.Value == null || _toPicker.Value == null) return;
string fromStr = _fromPicker.Value.Value.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture);
string toStr = _toPicker.Value.Value.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture);
long time = 0;
try
{
time = DB.SelectTotalLoggedTime(_context, fromStr, toStr);
}
catch { }
long remaining = time;
long hours = remaining / 3600000;
remaining %= 3600000;
long minutes = remaining / 60000;
remaining %= 60000;
long seconds = remaining / 1000;
remaining %= 1000;
long ms = remaining;
_hoursLabel.Text = $"{hours} hour{(hours != 1 ? "s" : "")}";
_minutesLabel.Text = $"{minutes} minute{(minutes != 1 ? "s" : "")}";
_secondsLabel.Text = $"{seconds} second{(seconds != 1 ? "s" : "")}";
_msLabel.Text = $"{ms} millisecond{(ms != 1 ? "s" : "")}";
}
private void ExportClick()
{
if (!(_exportDateCheck.Checked ?? false) && !(_exportTimeCheck.Checked ?? false) && !(_exportLogCheck.Checked ?? false))
{
MessageBox.Show(this, "At least one field needs to be exported (date, logged time, log).", "devtime error", MessageBoxButtons.OK, MessageBoxType.Error);
return;
}
var sfd = new SaveFileDialog();
var format = (Exporter.ExportFormat)Enum.Parse(typeof(Exporter.ExportFormat), _formatDropDown.SelectedKey ?? nameof(Exporter.ExportFormat.CSV));
if (format == Exporter.ExportFormat.CSV)
{
sfd.Filters.Add(new FileFilter("CSV Files (*.csv)", new[] { "*.csv" }));
sfd.FileName = "devtime_export.csv";
}
else
{
sfd.Filters.Add(new FileFilter("JSON Files (*.json)", new[] { "*.json" }));
sfd.FileName = "devtime_export.json";
}
if (sfd.ShowDialog(this) == DialogResult.Ok)
{
if (_fromPicker.Value == null || _toPicker.Value == null) return;
string fromStr = _fromPicker.Value.Value.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture);
string toStr = _toPicker.Value.Value.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture);
var exportForm = new ExportForm(
sfd.FileName,
_context,
fromStr,
toStr,
format,
_exportDateCheck.Checked ?? false,
_exportTimeCheck.Checked ?? false,
_exportLogCheck.Checked ?? false,
(Exporter.TimeFormat)Enum.Parse(typeof(Exporter.TimeFormat), _timeFormatDropDown.SelectedKey ?? nameof(Exporter.TimeFormat.Milliseconds)),
(Exporter.TimeApproximation)Enum.Parse(typeof(Exporter.TimeApproximation), _timeApproxDropDown.SelectedKey ?? nameof(Exporter.TimeApproximation.None))
);
var result = exportForm.ShowModal(this);
switch (result)
{
case DialogResult.Ok:
MessageBox.Show(this, "Export completed successfully.", "devtime", MessageBoxButtons.OK, MessageBoxType.Information);
break;
case DialogResult.Retry:
MessageBox.Show(this, "Export completed successfully.\nHowever, there are no entries in the specified time range. Therefore, the exported file is empty.", "devtime", MessageBoxButtons.OK, MessageBoxType.Warning);
break;
}
}
}
}
}