-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainPage.xaml.cs
368 lines (313 loc) · 14.4 KB
/
MainPage.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Diagnostics;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Security.Cryptography;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using Windows.Storage;
// The Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409
namespace SmartScheduler
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainPage : Page
{
// Global MainPage variables
public SmartSchedule schedule;
public uint currentNumTasksInSchedule = 0;
public uint nextEventID = 1;
public Random rng = new Random();
// Storage system variables
public ApplicationDataContainer localSettings = ApplicationData.Current.LocalSettings;
public ApplicationDataContainer startupSettings;
public ApplicationDataContainer scheduleData;
public StorageFolder taskDataContainer = ApplicationData.Current.LocalFolder;
public string scheduleStorageID;
private DateTime _selectedDate;
public DateTime selectedDate {
get { return _selectedDate; }
set
{
_selectedDate = value;
TB_DaySchedule.Text = "Schedule: " + selectedDate.ToString("d");
//syncScheduleViewer();
}
}
public MainPage()
{
bool opened = true;
this.InitializeComponent();
// TEMP: delete settings - for debugging only
//localSettings.DeleteContainer("startup");
// Try to read composite value "startup" from settings to either load or create data
try
{
startupSettings = localSettings.CreateContainer("startup", ApplicationDataCreateDisposition.Existing);
}
catch //(Exception e)
{
// Startup settings have never been created! Create a new Schedule data structure
System.Diagnostics.Debug.WriteLine("Creating ApplicationData in localSettings for new schedule");
opened = false;
startupSettings = localSettings.CreateContainer("startup", ApplicationDataCreateDisposition.Always);
// Create new scjedule object
nextEventID = 1; // TODO: read from global configuration file (permanent storage)
uint calID = (uint)rng.Next(int.MinValue, int.MaxValue);
schedule = new SmartSchedule(startupSettings, calID, "Default", new SolidColorBrush(Color.FromArgb(0xFF, 0x10, 0xEE, 0xEE)));
scheduleStorageID = schedule.StorageID();
// Store these values into the startup settings
// startupSettings = new ApplicationDataCompositeValue();
startupSettings.Values["nextEventID"] = nextEventID; // Events always start at 1
startupSettings.Values["scheduleStorageID"] = scheduleStorageID;
scheduleData = startupSettings.CreateContainer(scheduleStorageID, ApplicationDataCreateDisposition.Always);
//startupSettings.Values[scheduleStorageID] = scheduleData;
//localSettings.Values["startup"] = startupSettings;
}
if (opened)
{
// Startup settings already exist! Load previous values into usable data structure
System.Diagnostics.Debug.WriteLine("Loading schedule data from application storage");
nextEventID = (uint)startupSettings.Values["nextEventID"];
scheduleStorageID = (string)startupSettings.Values["scheduleStorageID"];
scheduleData = startupSettings.CreateContainer(scheduleStorageID, ApplicationDataCreateDisposition.Existing);
schedule = new SmartSchedule(startupSettings, scheduleStorageID);
}
// Initialize XAML components
CB_TypePicker.ItemsSource = schedule.taskTypeList;
CB_RequiredPicker.ItemsSource = schedule.ynList;
CB_RepeatPicker.ItemsSource = schedule.repeatTypeList;
CB_DurHoursPicker.ItemsSource = SmartTask.hours;
CB_DurMinsPicker.ItemsSource = SmartTask.mins;
LV_Schedule.ItemsSource = schedule.GetTastsAt(selectedDate.Date);
selectedDate = DateTime.Now.Date;
RefreshScheduleView();
}
private void CV_MainCalendar_SelectedDatesChanged(CalendarView sender, CalendarViewSelectedDatesChangedEventArgs args)
{
if (args.AddedDates.Count > 0)
{
selectedDate = args.AddedDates.First().Date;
CDP_NewItemDate.Date = args.AddedDates.First().Date;
}
LV_Schedule.ItemsSource = schedule.GetTastsAt(selectedDate);
}
private void PB_AddToSchedule_Click(object sender, RoutedEventArgs e)
{
bool filled = true;
// Check to see if required fields are filled in:
// Title
if (TB_NewTitle.Text.Length == 0)
{
filled = false;
TB_AsteriskTitle.Text = "*";
}
else
{
TB_AsteriskTitle.Text = "";
}
// Date
if (!CDP_NewItemDate.Date.HasValue)
{
filled = false;
TB_AsteriskDate.Text = "*";
} else
{
TB_AsteriskDate.Text = "";
}
// Task type
if (CB_TypePicker.SelectedItem == null)
{
filled = false;
TB_AsteriskType.Text = "*";
}
else
{
TB_AsteriskType.Text = "";
}
// Duration
if (CB_DurHoursPicker.SelectedItem == null &&
CB_TypePicker.SelectedItem != null && (TaskType)CB_TypePicker.SelectedItem != TaskType.FullDay) // duration is not required for a FullDay event
{
filled = false;
TB_AsteriskDuration.Text = "*";
}
else
{
TB_AsteriskDuration.Text = "";
}
// Duration is not required for a FullDay event
if (CB_DurMinsPicker.SelectedItem == null &&
CB_TypePicker.SelectedItem != null && (TaskType)CB_TypePicker.SelectedItem != TaskType.FullDay)
{
filled = false;
TB_AsteriskDuration.Text = "*";
}
else
{
TB_AsteriskDuration.Text = "";
}
// Time
if (!TP_NewItemTime.SelectedTime.HasValue &&
CB_TypePicker.SelectedItem != null && (TaskType)CB_TypePicker.SelectedItem != TaskType.FullDay)
{
filled = false;
TB_AsteriskTime.Text = "*";
}
else
{
TB_AsteriskTime.Text = "";
}
// Required?
// *** Assume no selection means Yes required ***
if (CB_RequiredPicker.SelectedValue == null)
{
CB_RequiredPicker.SelectedValue = YN.Yes;
}
// Repeat
// *** Assume no selection means None repeat ***
if (CB_RepeatPicker.SelectedValue == null)
{
CB_RepeatPicker.SelectedValue = RepeatType.None;
}
// Repeat #times
// TODO - make repeat events show up in repeated days!
int repeatNumTimes = 1;
if (CB_RepeatPicker.SelectedValue != null && (RepeatType)CB_RepeatPicker.SelectedValue != RepeatType.None)
{
// Repeat is selected - we need to parse the number of times // TODO
try
{
repeatNumTimes = int.Parse(TB_NewRepeatTimes.Text);
if (repeatNumTimes < 1)
{
throw new Exception("Value of repeat times must be at least 1");
}
TB_AsteriskRepeatTimes.Text = "";
}
catch (FormatException fe)
{
Debug.WriteLine($"Improper event repeat input: Exception = {fe}");
filled = false;
TB_AsteriskRepeatTimes.Text = "*";
}
}
if (!filled)
{
TB_RequiredFields.Text = "The marked(*) fields are required.";
return;
}
// Remove failed text markers from UI
TB_RequiredFields.Text = "";
// Create SmartTask object out of the text fields
// Get full DateTime from composite boxes
DateTimeOffset date = CDP_NewItemDate.Date.Value;
int hour = 0, minute = 0;
if (TP_NewItemTime.SelectedTime != null)
{
hour = TP_NewItemTime.SelectedTime.Value.Hours;
minute = TP_NewItemTime.SelectedTime.Value.Minutes;
}
DateTime when = new DateTime(date.Year, date.Month, date.Day, hour, minute, 0);
// Get duration
int durHour = 0, durMinute = 0;
if (CB_DurHoursPicker.SelectedItem != null)
{
durHour = (int)CB_DurHoursPicker.SelectedItem;
durMinute = (int)CB_DurMinsPicker.SelectedItem;
}
// Type classification
TaskType taskType = (TaskType) CB_TypePicker.SelectedValue;
RepeatType repeatType = CB_RepeatPicker.SelectedValue == null ? RepeatType.None : (RepeatType) CB_RepeatPicker.SelectedValue;
YN required = CB_RequiredPicker.SelectedValue == null ? YN.Yes : (YN) CB_RequiredPicker.SelectedValue;
// Create a new task from the schedule
SmartTask newTask = schedule.CreateTask(nextEventID++, date, hour, minute, durHour, durMinute, taskType, repeatType, repeatNumTimes, TB_NewTitle.Text, TB_NewDescription.Text, required, TB_NewLocation.Text, TB_NewURL.Text);
// Add to global schedule variable
schedule.AddTask(newTask);
if (selectedDate.Date == when.Date)
{
//Update the ListView viewer when adding a new task to the current date
Debug.WriteLine($"Selected: {selectedDate.Date}, when: {when.Date}");
RefreshScheduleView();
}
}
private void PB_DeleteSelectedTask(object sender, RoutedEventArgs e)
{
// Deletes the task whose flyout menu was selected
((SmartTask)((Button)sender).DataContext).DeleteTask();
// Refreshes the schedule viewer display to remove the task
RefreshScheduleView();
}
private void PB_EditFlyoutShowFlyout(object sender, TappedRoutedEventArgs e)
{
FlyoutBase.ShowAttachedFlyout((FrameworkElement)sender);
}
private void PB_UpdateSelectedTask(object sender, RoutedEventArgs e)
{
Debug.WriteLine($"Updating task: {(SmartTask)((Button)sender).DataContext}");
// Get XAML items from Grid
Grid elements = (Grid)((Button)sender).Parent;
// Retrieve each field from the edit menu
try
{
SmartTask existingTask = (SmartTask)((Button)sender).DataContext;
// Null fields were handled when creating the tasks, so these should have selections
DateTimeOffset date = (DateTimeOffset)((CalendarDatePicker)elements.FindName("CDP_edit_date")).Date;
int hour = (int)((TimePicker)elements.FindName("TP_edit_time")).SelectedTime.Value.Hours;
int minute = (int)((TimePicker)elements.FindName("TP_edit_time")).SelectedTime.Value.Minutes;
int durHour = (int)((ComboBox)elements.FindName("TB_edit_durHours")).SelectedItem;
int durMinute = (int)((ComboBox)elements.FindName("TB_edit_durMins")).SelectedItem;
TaskType taskType = (TaskType)((ComboBox)elements.FindName("CB_edit_taskType")).SelectedItem;
RepeatType repeatType = (RepeatType)((ComboBox)elements.FindName("CB_edit_repeat")).SelectedItem;
string title = ((TextBox)elements.FindName("TB_edit_title")).Text;
string description = ((TextBox)elements.FindName("TB_edit_description")).Text;
YN required = (YN)((ComboBox)elements.FindName("CB_edit_required")).SelectedItem;
string location = ((TextBox)elements.FindName("TB_edit_location")).Text;
string url = ((TextBox)elements.FindName("TB_edit_url")).Text;
string repeatNumTimesText = ((TextBox)elements.FindName("TB_edit_repeatNumTimes")).Text;
int repeatNumTimes;
try
{
repeatNumTimes = int.Parse(repeatNumTimesText);
}
catch
{
repeatNumTimes = -1;
}
if (repeatNumTimes < 1)
{
repeatNumTimes = existingTask.repeatNumTimes;
}
// Create a new task from the schedule
SmartTask newTask = schedule.CreateTask(nextEventID++, date, hour, minute, durHour, durMinute, taskType, repeatType, repeatNumTimes, title, description, required, location, url);
// Remove the existing task
existingTask.DeleteTask();
// Add the updated task back to the schedule
schedule.AddTask(newTask);
}
catch(Exception err)
{
Debug.WriteLine($"Exception thrown in task update: {err.Message}");
}
RefreshScheduleView();
}
public void RefreshScheduleView()
{
// Refreshes the schedule viewer display
Debug.WriteLine($"RefreshScheduleView: date {selectedDate}");
LV_Schedule.ItemsSource = null;
LV_Schedule.ItemsSource = schedule.GetTastsAt(selectedDate.Date);
}
} // End of SmartScheduler namespace
}