/
MenuPage.xaml.cs
410 lines (338 loc) · 15.1 KB
/
MenuPage.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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.IO.IsolatedStorage;
using System.IO;
using Newtonsoft.Json;
using System.Collections.ObjectModel;
using Microsoft.Phone.Shell;
namespace mockup
{
public partial class PanoramaPage1 : PhoneApplicationPage
{
// using observable collection for data binding
public ObservableCollection<Module> Modules = new ObservableCollection<Module>();
public ObservableCollection<Class> Classes = new ObservableCollection<Class>();
public ObservableCollection<Todo> Todos = new ObservableCollection<Todo>();
// to store academic year and semester information
private List<SemInfo> Sems = new List<SemInfo>();
private int numOfSems;
// define OS version 7.10.8858
private static Version TargetedVersion = new Version(7, 10, 8858);
// check if the current OS version is sufficient
public static bool IsTargetedVersion {
get { return Environment.OSVersion.Version >= TargetedVersion; }
}
public PanoramaPage1()
{
InitializeComponent();
if ((Application.Current as App).online)
{
loadModules();
}
else
{
// load modules information from memory
Modules = new ObservableCollection<Module>((Application.Current as App).modules);
foreach (Module module in Modules)
{
foreach (Announcement announce in module.moduleAnnouncements)
{
announce.GenerateDisplayContent(module.moduleCode);
}
}
// load classes information from memory
Classes = new ObservableCollection<Class>((Application.Current as App).classes);
foreach (Class myClass in Classes)
{
myClass.GenerateDisplay();
}
}
// link view with model
ModuleInfo.ItemsSource = Modules;
ClassInfo.ItemsSource = Classes;
}
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
base.OnNavigatedTo(e);
if (NavigationService.CanGoBack)
{
while (NavigationService.RemoveBackEntry() != null)
{
NavigationService.RemoveBackEntry();
}
}
// load to-do data from public variables
// every time the menupage is reloaded
// just in case user have modified the to-do list
Todos = new ObservableCollection<Todo>((Application.Current as App).todos);
TodoInfo.ItemsSource = Todos;
}
private void loadModules()
{
string[] paramNames = new string[] { "Duration", "IncludeAllInfo" };
string[] paramValues = new string[] { "0", "true" };
string URL = LAPI.requestURL("Modules", paramNames, paramValues);
GetFeed(URL);
}
private void viewModule(object sender, System.Windows.Input.GestureEventArgs e)
{
if (ModuleInfo.SelectedIndex != -1)
{
int moduleIndex = ModuleInfo.SelectedIndex;
NavigationService.Navigate(new Uri(("/ModulePage.xaml?moduleIndex=" + moduleIndex), UriKind.Relative));
}
}
private void viewTodo(object sender, System.Windows.Input.GestureEventArgs e)
{
if (TodoInfo.SelectedIndex != -1)
{
int todoIndex = TodoInfo.SelectedIndex;
NavigationService.Navigate(new Uri(("/TodoPage.xaml?todoIndex=" + todoIndex), UriKind.Relative));
}
}
public void GetFeed(string url)
{
loadModuleProgressBar.IsIndeterminate = true;
HttpWebRequest request = HttpWebRequest.CreateHttp(url);
request.BeginGetResponse(new AsyncCallback(HandleResponse), request);
}
// handle the response and get the json string
public void HandleResponse(IAsyncResult result)
{
HttpWebRequest request = (HttpWebRequest)result.AsyncState;
HttpWebResponse response;
try
{
response = (HttpWebResponse)request.EndGetResponse(result);
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
string feed = reader.ReadToEnd();
ModuleInfoWrapper test = (ModuleInfoWrapper)Deserialize(feed, typeof(ModuleInfoWrapper));
if (test.comments.Equals("Valid login!"))
{
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
foreach (Module module in test.modules)
{
foreach (Announcement announce in module.moduleAnnouncements)
{
announce.GenerateDisplayContent(module.moduleCode);
}
Modules.Add(module);
SemInfo newSemInfo = new SemInfo(module.moduleAcadYear, module.moduleSemester.Replace("Semester ", String.Empty));
if (!Sems.Contains(newSemInfo, new SeminfoEqualityComparer()))
{
Sems.Add(newSemInfo);
}
}
// update existing tiles
UpdateAllModuleTile();
numOfSems = Sems.Count;
foreach (SemInfo sem in Sems)
{
string[] paramNames = new string[] { "AcadYear", "Semester" };
string[] paramValues = new string[] { sem.AcademicYear, sem.Semester };
string URL = LAPI.requestURL("Timetable_Student", paramNames, paramValues);
GetClass(URL);
}
(Application.Current as App).modules = Modules.ToList();
});
}
}
}
catch (WebException ex)
{
if (ex.Status == WebExceptionStatus.RequestCanceled)
{
Dispatcher.BeginInvoke(() =>
{
loadModules();
});
}
}
}
public void GetClass(string url)
{
HttpWebRequest request = HttpWebRequest.CreateHttp(url);
request.BeginGetResponse(new AsyncCallback(HandleClassResponse), request);
}
// handle the response and get the json string
public void HandleClassResponse(IAsyncResult result)
{
HttpWebRequest request = result.AsyncState as HttpWebRequest;
if (request != null)
{
using (WebResponse response = request.EndGetResponse(result))
{
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
string feed = reader.ReadToEnd();
ClassWrapper test = (ClassWrapper)Deserialize(feed, typeof(ClassWrapper));
if (test.comments.Equals("Valid login!"))
{
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
numOfSems--;
foreach (Class myClass in test.classes)
{
myClass.GenerateDisplay();
Classes.Add(myClass);
}
if (numOfSems == 0)
{
// hide the progress bar
loadModuleProgressBar.IsIndeterminate = false;
Classes = new ObservableCollection<Class>(Classes.OrderBy(Class => Class.classTimePoint));
ClassInfo.ItemsSource = Classes;
(Application.Current as App).classes = Classes.ToList();
}
});
}
}
}
}
}
public static object Deserialize(String input, Type objectType)
{
if (objectType == null || input == null)
return null;
JsonSerializer ser = new JsonSerializer();
JsonReader jr = new JsonTextReader(new StringReader(input));
return ser.Deserialize(jr, objectType);
}
private void PinToStart_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
ListBoxItem selectedListBoxItem = this.ModuleInfo.ItemContainerGenerator.ContainerFromItem((sender as MenuItem).DataContext) as ListBoxItem;
int selectedModuleIndex = this.ModuleInfo.ItemContainerGenerator.IndexFromContainer(selectedListBoxItem);
// generate a secondary tile
string moduleTitle = Modules[selectedModuleIndex].moduleCode;
if (Modules[selectedModuleIndex].moduleAnnouncements.Length > 0)
{
string moduleLatestAnnounceTitle = Modules[selectedModuleIndex].moduleAnnouncements[0].announceNameDisplay;
string moduleLatestAnnounceContent = Modules[selectedModuleIndex].moduleAnnouncements[0].announceContentPreview;
if (moduleLatestAnnounceContent.Length > 50)
{
CreateModuleTile(selectedModuleIndex, moduleTitle, moduleLatestAnnounceTitle, moduleLatestAnnounceContent.Substring(0, 50));
}
else
{
CreateModuleTile(selectedModuleIndex, moduleTitle, moduleLatestAnnounceTitle, moduleLatestAnnounceContent);
}
}
else
{
CreateModuleTile(selectedModuleIndex, moduleTitle, null, null);
}
}
private void CreateModuleTile(int moduleIndex, string title, string backTitle, string backContent)
{
// check for existing tiles
var foundTile = ShellTile.ActiveTiles.FirstOrDefault(x => x.NavigationUri.ToString().Contains("moduleIndex=" + moduleIndex));
if (foundTile == null)
{
var moduleTile = new StandardTileData
{
BackgroundImage = new Uri("IVLE_module.png", UriKind.Relative),
Title = title,
BackTitle = backTitle,
BackContent = backContent
};
ShellTile.Create(new Uri(("/ModulePage.xaml?moduleIndex=" + moduleIndex), UriKind.Relative), moduleTile);
}
else
{
MessageBox.Show("The module has been added to the start screen!");
}
}
private void UpdateAllModuleTile()
{
for (int i = 0; i < Modules.Count; i++)
{
string moduleLatestAnnounceTitle = null;
string moduleLatestAnnounceContent = null;
string moduleTitle = Modules[i].moduleCode;
if (Modules[i].moduleAnnouncements.Length > 0)
{
moduleLatestAnnounceTitle = Modules[i].moduleAnnouncements[0].announceNameDisplay;
moduleLatestAnnounceContent = Modules[i].moduleAnnouncements[0].announceContentPreview;
}
UpdateModuleTile(i, moduleTitle, moduleLatestAnnounceTitle, moduleLatestAnnounceContent);
}
}
private void UpdateModuleTile(int moduleIndex, string title, string backTitle, string backContent)
{
// check for existing tiles
var foundTile = ShellTile.ActiveTiles.FirstOrDefault(x => x.NavigationUri.ToString().Contains("moduleIndex=" + moduleIndex));
if (foundTile != null)
{
if (backContent != null && backContent.Length > 50)
{
backContent = backContent.Substring(0, 50);
}
var moduleTile = new StandardTileData
{
BackgroundImage = new Uri("IVLE_module.png", UriKind.Relative),
Title = title,
BackTitle = backTitle,
BackContent = backContent
};
foundTile.Update(moduleTile);
}
}
class SeminfoEqualityComparer : IEqualityComparer<SemInfo>
{
public bool Equals(SemInfo s1, SemInfo s2)
{
if (s1.Semester == s2.Semester && s1.AcademicYear == s2.AcademicYear)
{
return true;
}
else
{
return false;
}
}
public int GetHashCode(SemInfo sem)
{
int hCode = sem.AcademicYear.Length ^ sem.Semester.Length;
return hCode.GetHashCode();
}
}
private void RemoveToDo(object sender, System.Windows.Input.GestureEventArgs e)
{
ListBoxItem selectedListBoxItem = this.TodoInfo.ItemContainerGenerator.ContainerFromItem((sender as MenuItem).DataContext) as ListBoxItem;
int selectedTodoIndex = this.TodoInfo.ItemContainerGenerator.IndexFromContainer(selectedListBoxItem);
(Application.Current as App).todos.RemoveAt(selectedTodoIndex);
(Application.Current as App).UpdateAppTile();
Todos.RemoveAt(selectedTodoIndex);
}
// click event on log off menu button
private void Logoff_Menu_Click(object sender, EventArgs e)
{
LAPI.token = null;
// remove token from storage settings
var settings = IsolatedStorageSettings.ApplicationSettings;
if (settings.Contains("token"))
{
settings.Remove("token");
}
settings.Save();
NavigationService.Navigate(new Uri(("/MainPage.xaml"), UriKind.Relative));
}
// click event on settings menu button
private void Settings_Menu_Click(object sender, EventArgs e)
{
NavigationService.Navigate(new Uri(("/SettingsPage.xaml"), UriKind.Relative));
}
}
}