This repository has been archived by the owner on Apr 18, 2020. It is now read-only.
/
TweetModel.cs
425 lines (364 loc) · 13.6 KB
/
TweetModel.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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
using DanielVaughan.Windows;
using Microsoft.Phone.Shell;
using Microsoft.Phone.Tasks;
using Ocell.Commands;
using Ocell.Library;
using Ocell.Library.Twitter;
using Ocell.Localization;
using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using TweetSharp;
namespace Ocell.Pages.Elements
{
public class TweetModel : ExtendedViewModelBase
{
ApplicationBarMode appBarMode;
public ApplicationBarMode AppBarMode
{
get { return appBarMode; }
set { Assign("AppBarMode", ref appBarMode, value); }
}
bool completed;
public bool Completed
{
get { return completed; }
set { Assign("Completed", ref completed, value); }
}
bool isMuting;
public bool IsMuting
{
get { return isMuting; }
set { Assign("IsMuting", ref isMuting, value); }
}
TwitterStatus tweet;
public TwitterStatus Tweet
{
get { return tweet; }
set { Assign("Tweet", ref tweet, value); }
}
bool hasReplies;
public bool HasReplies
{
get { return hasReplies; }
set { Assign("HasReplies", ref hasReplies, value); }
}
bool isFavorited;
public bool IsFavorited
{
get { return isFavorited; }
set { Assign("IsFavorited", ref isFavorited, value); }
}
bool hasImage;
public bool HasImage
{
get { return hasImage; }
set { Assign("HasImage", ref hasImage, value); }
}
ObservableCollection<ITweeter> usersWhoRetweeted;
public ObservableCollection<ITweeter> UsersWhoRetweeted
{
get { return usersWhoRetweeted; }
set { Assign("UsersWhoRetweeted", ref usersWhoRetweeted, value); }
}
int retweetCount;
public int RetweetCount
{
get { return retweetCount; }
set { Assign("RetweetCount", ref retweetCount, value); }
}
bool hasRetweets;
public bool HasRetweets
{
get { return hasRetweets; }
set { Assign("HasRetweets", ref hasRetweets, value); }
}
string whoRetweeted;
public string WhoRetweeted
{
get { return whoRetweeted; }
set { Assign("WhoRetweeted", ref whoRetweeted, value); }
}
string avatar;
public string Avatar
{
get { return avatar; }
set { Assign("Avatar", ref avatar, value); }
}
string replyText;
public string ReplyText
{
get { return replyText; }
set { Assign("ReplyText", ref replyText, value); }
}
DelegateCommand deleteTweet;
public ICommand DeleteTweet
{
get { return deleteTweet; }
}
DelegateCommand share;
public ICommand Share
{
get { return share; }
}
DelegateCommand quote;
public ICommand Quote
{
get { return quote; }
}
DelegateCommand favorite;
public ICommand Favorite
{
get { return favorite; }
}
DelegateCommand sendTweet;
public ICommand SendTweet
{
get { return sendTweet; }
}
string imageSource;
public string ImageSource
{
get { return imageSource; }
set { Assign("ImageSource", ref imageSource, value); }
}
SafeObservable<ITweetable> replies;
public SafeObservable<ITweetable> Replies
{
get { return replies; }
protected set { Assign("Replies", ref replies, value); }
}
SafeObservable<string> images;
public SafeObservable<string> Images
{
get { return images; }
protected set { Assign("Images", ref images, value); }
}
public event EventHandler<EventArgs<ITweetable>> TweetSent;
Uri ImageNavigationUri;
public void Initialize()
{
AppBarMode = ApplicationBarMode.Default;
if (DataTransfer.Status == null)
{
MessageService.ShowError(Localization.Resources.ErrorLoadingTweet);
GoBack();
return;
}
if (DataTransfer.Status.RetweetedStatus != null)
{
Tweet = DataTransfer.Status.RetweetedStatus;
WhoRetweeted = " " + String.Format(Localization.Resources.RetweetBy, DataTransfer.Status.Author.ScreenName);
HasRetweets = true;
}
else
{
Tweet = DataTransfer.Status;
WhoRetweeted = "";
}
Avatar = String.Format("https://api.twitter.com/1/users/profile_image?screen_name={0}&size=original", Tweet.Author.ScreenName);
HasReplies = (Tweet.InReplyToStatusId != null);
HasImage = (Tweet.Entities != null && Tweet.Entities.Media.Any());
IsFavorited = Tweet.IsFavorited;
RetweetCount = Tweet.RetweetCount;
if (Tweet.User == null || Tweet.User.Name == null)
FillUser();
UsersWhoRetweeted = new ObservableCollection<ITweeter>();
Replies = new SafeObservable<ITweetable>();
Images = new SafeObservable<string>();
UsersWhoRetweeted.CollectionChanged += (s, e) =>
{
RetweetCount = UsersWhoRetweeted.Count;
};
CreateCommands();
}
private void GetReplies()
{
var convService = new ConversationService(DataTransfer.CurrentAccount);
convService.Finished += (sender, e) => IsLoading = false;
convService.GetConversationForStatus(Tweet, (statuses, response) =>
{
if (statuses != null)
{
var statuses_noRepeat = statuses.Cast<ITweetable>().Except(Replies).ToList();
foreach (var status in statuses_noRepeat)
Replies.Add(status);
}
});
}
private void GetRetweets()
{
var service = ServiceDispatcher.GetCurrentService();
if (service != null)
service.Retweets(new RetweetsOptions { Id = Tweet.Id }, (statuses, response) =>
{
if (statuses != null && statuses.Any())
{
HasRetweets = true;
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
foreach (var rt in statuses)
UsersWhoRetweeted.Add(rt.Author);
});
}
});
}
public TweetModel()
: base("Tweet")
{
Initialize();
}
public void OnLoad()
{
ThreadPool.QueueUserWorkItem((c) =>
{
GetRetweets();
GetReplies();
CreateCommands();
SetImage();
});
}
private void CreateCommands()
{
deleteTweet = new DelegateCommand((obj) =>
{
var user = Config.Accounts.FirstOrDefault(item => item != null && item.ScreenName == Tweet.Author.ScreenName);
ServiceDispatcher.GetService(user).DeleteTweet(new DeleteTweetOptions { Id = Tweet.Id }, (s, response) =>
{
if (response.StatusCode == HttpStatusCode.OK)
MessageService.ShowMessage(Localization.Resources.TweetDeleted, "");
else
MessageService.ShowError(Localization.Resources.ErrorDeletingTweet);
});
}, (obj) => Tweet != null && Tweet.Author != null && Config.Accounts.Any(item => item != null && item.ScreenName == Tweet.Author.ScreenName));
share = new DelegateCommand((obj) => Deployment.Current.Dispatcher.BeginInvoke(() =>
{
EmailComposeTask emailComposeTask = new EmailComposeTask();
emailComposeTask.Subject = String.Format(Localization.Resources.TweetFrom, Tweet.Author.ScreenName);
emailComposeTask.Body = "@" + Tweet.Author.ScreenName + ": " + Tweet.Text +
Environment.NewLine + Environment.NewLine + Tweet.CreatedDate.ToString();
emailComposeTask.Show();
}), obj => Tweet != null);
quote = new DelegateCommand((obj) =>
{
DataTransfer.Text = "RT @" + Tweet.Author.ScreenName + ": " + Tweet.Text;
Navigate(Uris.WriteTweet);
}, obj => Config.Accounts.Any() && Tweet != null);
favorite = new DelegateCommand((parameter) =>
{
TwitterStatus param = (TwitterStatus)parameter;
if (IsFavorited)
ServiceDispatcher.GetService(DataTransfer.CurrentAccount).UnfavoriteTweet(new UnfavoriteTweetOptions { Id = param.Id }, (sts, resp) =>
{
MessageService.ShowLightNotification(Localization.Resources.Unfavorited);
IsFavorited = false;
});
else
ServiceDispatcher.GetService(DataTransfer.CurrentAccount).FavoriteTweet(new FavoriteTweetOptions { Id = param.Id }, (sts, resp) =>
{
MessageService.ShowLightNotification(Localization.Resources.Favorited);
IsFavorited = true;
});
}, parameter => (parameter is TwitterStatus) && Config.Accounts.Count > 0 && DataTransfer.CurrentAccount != null);
sendTweet = new DelegateCommand((parameter) =>
{
IsLoading = true;
BarText = Resources.SendingTweet;
ServiceDispatcher.GetCurrentService().SendTweet(new SendTweetOptions
{
InReplyToStatusId = Tweet.Id,
Status = ReplyText
}, (status, response) =>
{
IsLoading = false;
BarText = "";
if (response.StatusCode != HttpStatusCode.OK)
MessageService.ShowError(response.Error != null ? response.Error.Message : Resources.UnknownValue);
else if (TweetSent != null)
TweetSent(this, new EventArgs<ITweetable>(status));
});
});
}
void FillUser()
{
ServiceDispatcher.GetDefaultService().GetUserProfileFor(new GetUserProfileForOptions { ScreenName = Tweet.Author.ScreenName }, (user, response) =>
{
if (response.StatusCode != HttpStatusCode.OK)
MessageService.ShowError(Localization.Resources.ErrorGettingProfile);
Tweet.User = user;
});
}
public void ImageFailed(object sender, ExceptionRoutedEventArgs e)
{
IsLoading = false;
MessageService.ShowError(Localization.Resources.ErrorDownloadingImage);
}
public void ImageOpened(object sender, RoutedEventArgs e)
{
IsLoading = false;
BarText = "";
}
public void ImageTapped(object sender, System.Windows.Input.GestureEventArgs e)
{
Image img = sender as Image;
if (img != null)
{
var url = img.Tag as string;
if (url != null && Uri.IsWellFormedUriString(url, UriKind.Absolute))
{
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
var task = new Microsoft.Phone.Tasks.WebBrowserTask { Uri = new Uri(url, UriKind.Absolute) };
task.Show();
});
}
}
}
void SetImage()
{
if (Tweet.Entities == null)
return;
if (Tweet.Entities.Media != null && Tweet.Entities.Media.Any())
{
var photo = Tweet.Entities.Media.First();
Images.Add(photo.MediaUrl);
}
if (Tweet.Entities.Urls != null && Tweet.Entities.Urls.Any())
{
var parser = new MediaLinkParser();
foreach (var i in Tweet.Entities.Urls)
{
if (i.EntityType == TwitterEntityType.Url)
{
var url = i as TwitterUrl;
if (url != null && !string.IsNullOrWhiteSpace(url.ExpandedValue))
{
string photoUrl;
if (parser.TryGetMediaUrl(url.ExpandedValue, out photoUrl))
Images.Add(photoUrl);
}
}
}
}
if (Images.Count > 0)
{
HasImage = true;
IsLoading = true;
BarText = Localization.Resources.DownloadingImage;
}
}
public void ReplyBoxGotFocus()
{
ReplyText = ReplyAllCommand.GetReplied(Tweet);
}
public void ReplyBoxLostFocus()
{
ReplyText = String.Format("{0}...", Resources.Reply);
}
}
}