forked from lizy14/Tsinghua-UWP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainPage.xaml.cs
170 lines (143 loc) · 5.87 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
using System;
using System.Threading.Tasks;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;
// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409
namespace TsinghuaUWP {
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainPage : Page {
public MainPage() {
this.InitializeComponent();
}
protected override async void OnNavigatedTo(NavigationEventArgs e) {
if (DataAccess.supposedToWorkAnonymously()) {
btnLogin.Content = "登录";
btnRefreshTimetable.IsEnabled = false;
btnUpdate.IsEnabled = false;
update_without_credential();
} else if (!DataAccess.supposedToWorkAnonymously()
&& DataAccess.credentialAbsent()) {
update_without_credential();
await changeAccountAsync();
} else if (!DataAccess.credentialAbsent()) {
update_with_credential();
}
}
private async void update_with_credential() {
updateDeadlinesAsyc();
updateTimetableAsync();
Appointment.updateCalendar();
try {
await Appointment.updateLectures();
} catch { }
}
private async void update_without_credential() {
try {
await Notification.update(calendarOnly: true);
await Appointment.updateCalendar();
} catch { }
try {
await Appointment.updateLectures();
} catch { }
}
private async Task changeAccountAsync() {
btnLogin.Content = "登录";
this.btnRefreshTimetable.IsEnabled = false;
this.btnUpdate.IsEnabled = false;
this.btnLogin.IsEnabled = false;
if (await changeAccountHelper()) {
this.btnLogin.Content = "注销登录";
this.btnRefreshTimetable.IsEnabled = true;
this.btnUpdate.IsEnabled = true;
update_with_credential();
} else {
this.btnLogin.Content = "登录";
update_without_credential();
}
this.progressLogin.IsActive = false;
this.btnLogin.IsEnabled = true;
}
private async Task<bool> changeAccountHelper() //false for anonymous
{
var dialog = new PasswordDialog();
Password password;
this.progressLogin.IsActive = true;
try {
password = await dialog.getCredentialAsyc();
this.progressLogin.IsActive = false;
} catch (UserCancelException) {
//user choose to stay anonymous
DataAccess.setLocalSettings("username", "__anonymous");
return false;
}
//save credential
//TODO: wrap as a function and move into DataAccess
DataAccess.setLocalSettings("toasted_assignments", "");
DataAccess.setLocalSettings("username", password.username);
var vault = new Windows.Security.Credentials.PasswordVault();
vault.Add(new Windows.Security.Credentials.PasswordCredential(
"Tsinghua_Learn_Website", password.username, password.password));
return true;
}
private int updateNotificationsCounter = 0;
private async Task updateDeadlinesAsyc() {
updateNotificationsCounter++;
this.progressUpdate.IsActive = true;
this.btnUpdate.IsEnabled = false;
this.errorUpdate.Visibility = Visibility.Collapsed;
//TODO: simplify update logic of local being fall-back
try {
await Notification.update(true);
await Appointment.updateDeadlines();
} catch (Exception e) {
this.errorUpdate.Visibility = Visibility.Visible;
try {
await Notification.update();
} catch (Exception) { }
}
if (--updateNotificationsCounter == 0) {
this.progressUpdate.IsActive = false;
this.btnUpdate.IsEnabled = !DataAccess.credentialAbsent();
}
}
private int updateTimetableCounter = 0;
private async Task updateTimetableAsync() {
updateTimetableCounter++;
this.progressRefreshTimetable.IsActive = true;
this.btnRefreshTimetable.IsEnabled = false;
this.errorRefreshTimetable.Visibility = Visibility.Collapsed;
try {
await Appointment.updateTimetable(true);
} catch (Exception e) {
this.errorRefreshTimetable.Visibility = Visibility.Visible;
try {
await Appointment.updateTimetable(true);
this.errorRefreshTimetable.Visibility = Visibility.Collapsed;
} catch (Exception) { }
}
if (--updateTimetableCounter == 0) {
this.progressRefreshTimetable.IsActive = false;
this.btnRefreshTimetable.IsEnabled = true;
}
}
private void launchHelp() {
Windows.System.Launcher.LaunchUriAsync(new Uri(Remote.helpUrl));
}
private void btnRefreshTimetable_Click(object sender, RoutedEventArgs e) {
updateTimetableAsync();
}
private void btnUpdate_Click(object sender, RoutedEventArgs e) {
updateDeadlinesAsyc();
}
private void btnHelp_Click(object sender, RoutedEventArgs e) {
launchHelp();
}
private void button_Click(object sender, RoutedEventArgs e) {
changeAccountAsync();
}
}
}