This repository has been archived by the owner on Oct 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathReportWindow.xaml.cs
291 lines (264 loc) · 10.3 KB
/
ReportWindow.xaml.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using Furtherance.Views;
using Microsoft.UI;
using Microsoft.UI.Windowing;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using static PInvoke.User32;
// To learn more about WinUI, the WinUI project structure,
// and more about our project templates, see: http://aka.ms/winui-project-info.
namespace Furtherance;
public class TaskReportGroup : List<FurTask>
{
public TaskReportGroup(IEnumerable<FurTask> items, string key, string organizer = "none") : base(items)
{
Key = key;
var totalTime = TimeSpan.Zero;
foreach (var item in items)
{
totalTime = totalTime.Add(MainPage.RoundSeconds(item.StopTime.Subtract(item.StartTime)));
}
TotalTime = totalTime.ToString(@"h\:mm\:ss");
var subGroup = new List<TaskReportGroup>();
if (organizer == "name")
{
TaskReportSubGroup = items
.GroupBy(g => g.Tags)
.Select(g => new TaskReportGroup(g, string.IsNullOrWhiteSpace(g.Key) ? "no tags" : g.Key))
.ToList();
// Don't show any tags if the task doesn't have any tags to show (including "no tags")
if (TaskReportSubGroup.Count == 1 && TaskReportSubGroup[0].Key == "no tags")
{
TaskReportSubGroup.Clear();
}
}
else if (organizer == "tag")
{
if (string.IsNullOrEmpty(items.First().Tags))
{
Key = "no tags";
}
TaskReportSubGroup = items
.GroupBy(g => g.Name)
.Select(g => new TaskReportGroup(g, g.Key))
.ToList();
}
if (key == "No Results")
{
TotalTime = "";
}
}
public string Key { get; set; }
public string TotalTime { get; set; }
public List<TaskReportGroup> TaskReportSubGroup { get; set; }
}
public sealed partial class ReportWindow : Window
{
public ReportWindow()
{
InitializeComponent();
Title = "Generate Report";
var hwnd = WinRT.Interop.WindowNative.GetWindowHandle(this);
SetWindowDetails(hwnd, 420, 700);
var windowId = Win32Interop.GetWindowIdFromWindow(hwnd);
var appWindow = AppWindow.GetFromWindowId(windowId);
appWindow.SetIcon("Assets/furtherance.ico");
endCalendar.MaxDate = DateTime.Today;
startCalendar.MaxDate = DateTime.Today;
}
private void Timeframe_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (pickADatePanel != null)
{
var comboBox = sender as ComboBox;
if (comboBox.SelectedIndex != 5)
{
pickADatePanel.Visibility = Visibility.Collapsed;
}
else
{
pickADatePanel.Visibility = Visibility.Visible;
}
}
}
private void Filter_Checked(object sender, RoutedEventArgs e)
{
var checkBox = sender as CheckBox;
if (checkBox.IsChecked == true)
{
filterPanel.Visibility = Visibility.Visible;
}
else
{
filterPanel.Visibility = Visibility.Collapsed;
}
}
private void Calendar_DateChanged(CalendarDatePicker sender, CalendarDatePickerDateChangedEventArgs args)
{
if (endCalendar.Date != null)
{
startCalendar.MaxDate = (DateTimeOffset)endCalendar.Date;
if (startCalendar.Date > endCalendar.Date)
{
startCalendar.Date = endCalendar.Date;
}
}
}
private void Refresh_Click(object sender, RoutedEventArgs e)
{
RefreshReport();
}
private void RefreshReport()
{
totalTimeText.Visibility = Visibility.Collapsed;
var activeRange = timeframeCombo.SelectedIndex;
var allTasks = Database.GetData();
// Get date range
var today = DateTime.Today;
DateTime rangeStartDate;
var rangeEndDate = today;
switch (activeRange) {
case 0:
rangeStartDate = today.AddDays(-6);
break;
case 1:
var dayOfMonth = today.Day - 1;
rangeStartDate = today.AddDays(-dayOfMonth);
break;
case 2:
rangeStartDate = today.AddDays(-30);
break;
case 3:
rangeStartDate = today.AddDays(-180);
break;
case 4:
rangeStartDate = today.AddDays(-365);
break;
default:
// Parse user inputted start and stop dates
rangeStartDate = startCalendar.Date.Value.DateTime;
rangeEndDate = endCalendar.Date.Value.DateTime;
break;
}
// Get tasks in date range
var tasksInRange = new List<FurTask>();
var tasksInRangeTotalTime = TimeSpan.Zero;
foreach (var task in allTasks)
{
var startDate = task.StartTime.Date;
if (startDate >= rangeStartDate && startDate <= rangeEndDate)
{
if (filterCheck.IsChecked == true && !string.IsNullOrWhiteSpace(filterTextBox.Text))
{
var chosenFilter = filterTextBox.Text.Trim().Split(',').Select(g => g.Trim()).ToList();
chosenFilter.RemoveAll(g => string.IsNullOrWhiteSpace(g));
chosenFilter = chosenFilter.Distinct().ToList();
chosenFilter = chosenFilter.ConvertAll(g => g.ToLower());
if (filterCombo.SelectedIndex == 0)
{
// Filter by tasks
if (chosenFilter.Contains(task.Name.ToLower()))
{
tasksInRange.Add(task);
tasksInRangeTotalTime = tasksInRangeTotalTime
.Add(MainPage.RoundSeconds(task.StopTime.Subtract(task.StartTime)));
}
}
else
{
// Filter by tags
if (!string.IsNullOrWhiteSpace(task.Tags))
{
// Get task's tags as a List
var tagList = task.Tags.Split('#').ToList();
tagList.RemoveAll(g => string.IsNullOrWhiteSpace(g));
if (chosenFilter.Intersect(tagList).Any())
{
tasksInRange.Add(task);
tasksInRangeTotalTime = tasksInRangeTotalTime
.Add(MainPage.RoundSeconds(task.StopTime.Subtract(task.StartTime)));
}
}
}
}
else
{
tasksInRange.Add(task);
tasksInRangeTotalTime = tasksInRangeTotalTime
.Add(MainPage.RoundSeconds(task.StopTime.Subtract(task.StartTime)));
}
}
}
if (tasksInRange.Any())
{
if (sortRadio.SelectedIndex == 0)
{
// Sort by task selected
TreeViewData.ItemsSource = GetTasksGrouped(tasksInRange, "name");
}
else if (sortRadio.SelectedIndex == 1)
{
// Sort by tag selected
TreeViewData.ItemsSource = GetTasksGrouped(tasksInRange, "tag");
}
// Set total time label
var totalTimeString = tasksInRangeTotalTime.ToString(@"h\:mm\:ss");
totalTimeText.Text = $"Total Time: {totalTimeString}";
totalTimeText.Visibility = Visibility.Visible;
}
else
{
// No tasks in range. Add a task report group with the name No Results.
var dateTime = Database.ToRfc3339String(DateTime.Now);
var noTasksTask = new FurTask("0", "No Results", dateTime, dateTime, "");
var noTasksTaskList = new List<FurTask>
{
noTasksTask
};
TreeViewData.ItemsSource = GetTasksGrouped(noTasksTaskList, "name");
}
}
private static ObservableCollection<TaskReportGroup> GetTasksGrouped(List<FurTask> tasksInRange, string organizer)
{
if (organizer == "name")
{
IEnumerable<TaskReportGroup> query;
query = from item in tasksInRange
group item by item.Name into g
orderby g.Key
select new TaskReportGroup(g, g.Key, organizer);
return new ObservableCollection<TaskReportGroup>(query.Reverse());
}
else
{
IEnumerable<TaskReportGroup> query;
query = from item in tasksInRange
group item by item.Tags into g
orderby g.Key
select new TaskReportGroup(g, g.Key, organizer);
return new ObservableCollection<TaskReportGroup>(query.Reverse());
}
}
private static void SetWindowDetails(IntPtr hwnd, int width, int height)
{
var dpi = GetDpiForWindow(hwnd);
var scalingFactor = (float)dpi / 96;
width = (int)(width * scalingFactor);
height = (int)(height * scalingFactor);
_ = SetWindowPos(hwnd, SpecialWindowHandles.HWND_TOP,
0, 0, width, height,
SetWindowPosFlags.SWP_NOMOVE);
_ = SetWindowLong(hwnd,
WindowLongIndexFlags.GWL_STYLE,
(SetWindowLongFlags)(GetWindowLong(hwnd,
WindowLongIndexFlags.GWL_STYLE) &
~(int)SetWindowLongFlags.WS_MAXIMIZEBOX));
}
private void Window_Closed(object sender, WindowEventArgs args)
{
MainPage.mainPage.reportWindow = null;
}
}