/
UserViewModel.cs
241 lines (212 loc) · 7.67 KB
/
UserViewModel.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
using System;
using System.Threading;
using System.Threading.Tasks;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Ioc;
using GalaSoft.MvvmLight.Messaging;
using S1Nyan.Resources;
using S1Nyan.Model;
using S1Nyan.Views;
using S1Parser.User;
namespace S1Nyan.ViewModel
{
/// <summary>
/// This class contains properties that the main View can data bind to.
/// <para>
/// See http://www.galasoft.ch/mvvm
/// </para>
/// </summary>
public class UserViewModel : ViewModelBase, ISendPostService
{
public static UserViewModel Current
{
get
{
return SimpleIoc.Default.GetInstance<ISendPostService>() as UserViewModel;
}
}
private Timer notifyTimer;
/// <summary>
/// Initializes a new instance of the UserViewModel class.
/// </summary>
public UserViewModel()
{
notifyTimer = new Timer(OnTimeUp, this, Timeout.Infinite, Timeout.Infinite);
MessengerInstance.Register<NotificationMessage<S1NyanViewModelBase>>(this, OnReLoginMsg);
}
private async void OnReLoginMsg(NotificationMessage<S1NyanViewModelBase> msg)
{
if (msg.Notification != Messages.ReLoginMessageString) return;
Uid = null;
if (SettingView.IsRememberPass && SettingView.CurrentUsername.Length > 0)
await BackgroundLogin(SettingView.CurrentUsername, SettingView.CurrentPassword);
if (Uid != null)
MessengerInstance.Send(new NotificationMessage<S1NyanViewModelBase>(msg.Content, Messages.RefreshMessageString));
}
private string _loginStatus = AppResources.AccountPageGuest;
/// <summary>
/// Sets and gets the LoginStatus property.
/// Changes to that property's value raise the PropertyChanged event.
/// </summary>
public string LoginStatus
{
get { return _loginStatus; }
set
{
if (_loginStatus == value) return;
_loginStatus = value;
RaisePropertyChanged(() => LoginStatus);
}
}
private string _uid = null;
/// <summary>
/// Sets and gets the Uid property.
/// Changes to that property's value raise the PropertyChanged event.
/// </summary>
public string Uid
{
get { return _uid; }
set
{
if (_uid == value) return;
_uid = value;
RaisePropertyChanged(() => Uid);
}
}
public bool IsBusy;
public async Task<string> DoLogin(string name, string pass)
{
if (name.Length == 0 || pass.Length == 0) return AppResources.ErrorMsgNamePassEmpty;
string uid = null;
bool isSuccess = false;
try
{
IsBusy = true;
if (!String.IsNullOrEmpty(Uid))
await new S1WebClient().Logout(SettingView.VerifyString);
S1WebClient.ResetCookie();
var user = await new S1WebClient().Login(name, pass);
uid = user.Member_uid;
SettingView.VerifyString = user.Formhash;
if (uid != null)
{
Uid = uid;
LoginStatus = name;
isSuccess = true;
return null;
}
return AppResources.ErrorMsgUnknown;
}
catch (Exception ex)
{
return S1Nyan.Utils.Util.ErrorMsg.GetExceptionMessage(ex);
}
finally
{
IsBusy = false;
MessengerInstance.Send<NotificationMessage<bool>>(new NotificationMessage<bool>(isSuccess, Messages.LoginStatusChangedMessageString));
}
}
public async Task BackgroundLogin(string name, string pass)
{
var error = await DoLogin(name, pass);
if (error == null)
{
previousText = null;
}
else
{
SetNotifyMsg(error);
}
}
private void SetNotifyMsg(string msg)
{
if (previousText == null)
previousText = LoginStatus;
LoginStatus = msg;
notifyTimer.Change(5000, Timeout.Infinite);
}
string previousText = null;
private static void OnTimeUp(object state)
{
UserViewModel user = state as UserViewModel;
GalaSoft.MvvmLight.Threading.DispatcherHelper.UIDispatcher.BeginInvoke(() =>
{
if (user.previousText == null) return;
else
{
user.LoginStatus = user.previousText;
user.previousText = null;
}
});
}
private RelayCommand _showAccount;
/// <summary>
/// Gets the ShowAccount.
/// </summary>
public RelayCommand ShowAccount
{
get
{
return _showAccount
?? (_showAccount = new RelayCommand(
() =>
{
SettingView.GotoSetting(SettingView.PivotAccount);
}));
}
}
internal async void InitLogin()
{
if (Uid != null) return;
try
{
SettingView.InitAccountData();
if (SettingView.IsRememberPass && SettingView.CurrentUsername.Length > 0)
await BackgroundLogin(SettingView.CurrentUsername, SettingView.CurrentPassword);
else if (SettingView.IsFirstLogin)
SetNotifyMsg(AppResources.ErrorMsgClickToLogin);
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
}
}
internal void ReLogin()
{
Uid = null;
InitLogin();
}
public async Task<string> DoSendPost(string replyLink, string replyText, string verify)
{
UserErrorTypes result = UserErrorTypes.Unknown;
int retryTimes = 0;
try
{
while (result != UserErrorTypes.Success)
{
if (retryTimes > 2)
throw new S1UserException("MaxRetry", UserErrorTypes.MaxRetryTime);
if (result == UserErrorTypes.InvalidVerify)
{
Uid = null;
var error = await DoLogin(SettingView.CurrentUsername, SettingView.CurrentPassword);
if (error != null)
throw new S1UserException(error, UserErrorTypes.LoginFailed);
}
result = await new S1WebClient().Reply(verify,
reletivePostUrl: replyLink,
content: replyText,
signature: S1Nyan.Views.SettingView.GetSignature());
retryTimes++;
}
return null;
}
catch (Exception ex)
{
return S1Nyan.Utils.Util.ErrorMsg.GetExceptionMessage(ex);
}
}
}
}