/
PanoramaViewModel.cs
360 lines (307 loc) · 12 KB
/
PanoramaViewModel.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
using System.Collections.ObjectModel;
using MyToolkit.Networking;
using System.Windows;
using Newtonsoft.Json.Linq;
using DanielVaughan.WindowsPhone7Unleashed;
using System.Threading;
using System.ComponentModel;
using System.Windows.Input;
using System;
using System.Diagnostics;
using Microsoft.Phone.Net.NetworkInformation;
namespace OValiantHearts.ViewModels
{
public class PanoramaViewModel : INotifyPropertyChanged
{
private DateTime _lastMessageNoConnection = new DateTime(1970, 1, 1);
bool busy, videoBusy, feedBusy, tourBusy;
public bool Busy
{
get
{
return busy;
}
set
{
if (busy == value)
{
return;
}
busy = value;
OnPropertyChanged(new PropertyChangedEventArgs("Busy"));
}
}
readonly DelegateCommand fetchMoreDataCommand;
public ICommand FetchMoreDataCommand
{
get
{
return fetchMoreDataCommand;
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(PropertyChangedEventArgs e)
{
var tempEvent = PropertyChanged;
if (tempEvent != null)
{
tempEvent(this, e);
}
}
/// <summary>
/// A collection for VideoItemViewModel objects.
/// </summary>
readonly ObservableCollection<VideoItemViewModel> videos = new ObservableCollection<VideoItemViewModel>();
public ObservableCollection<VideoItemViewModel> Videos
{
get
{
return videos;
}
}
/// <summary>
/// A collection for FeedItemViewModel objects.
/// </summary>
readonly ObservableCollection<FeedItemViewModel> feeds = new ObservableCollection<FeedItemViewModel>();
public ObservableCollection<FeedItemViewModel> Feeds
{
get
{
return feeds;
}
}
/// <summary>
/// A collection for ConcertItemViewModel objects.
/// </summary>
readonly ObservableCollection<ConcertItemViewModel> concerts = new ObservableCollection<ConcertItemViewModel>();
public ObservableCollection<ConcertItemViewModel> Concerts
{
get
{
return concerts;
}
}
private String paging;
public PanoramaViewModel()
{
videoBusy = true;
feedBusy = true;
tourBusy = true;
Busy = true;
if (!NetworkInterface.GetIsNetworkAvailable())
{
long totalMiliSecond = (DateTime.UtcNow.Ticks - _lastMessageNoConnection.Ticks) / 10000;
if (totalMiliSecond < 10000) return;
_lastMessageNoConnection = DateTime.UtcNow;
MessageBox.Show("You need internet connection to request the band's info. Please, check your internet connection and try again.");
return;
}
loadVideos();
loadFeeds();
loadTour();
paging = "";
fetchMoreDataCommand = new DelegateCommand(
obj =>
{
if (busy)
{
return;
}
Busy = true;
ThreadPool.QueueUserWorkItem(
delegate
{
/* This Thread.Sleep is just to demonstrate a slow operation. */
// Thread.Sleep(3000);
/* We invoke back to the UI thread.
* Ordinarily this would be done
* by the Calcium infrastructure automatically. */
Deployment.Current.Dispatcher.BeginInvoke(
delegate
{
if (!"".Equals(paging))
loadPaging();
else
Busy = false;
});
});
});
}
public void loadVideos()
{
var request = new HttpGetRequest("http://gdata.youtube.com/feeds/api/users/ovalianthearts/uploads");
request.Query.Add("v", "2");
request.Query.Add("alt", "json");
Http.Get(request, VideoListRequestFinished);
}
public void loadFeeds()
{
var request = new HttpGetRequest("https://graph.facebook.com/oauth/access_token");
request.Query.Add("client_id", "270546663027963");
request.Query.Add("client_secret", "9c7eb6f1a4ddaef888fa73dd0734d896");
request.Query.Add("grant_type", "client_credentials");
Http.Get(request, AccesTokenRequestFinished);
}
public void loadTour()
{
var request = new HttpGetRequest("http://ovaliantheartsmusic.appspot.com/concerts.json");
request.Query.Add("period", "last");
Http.Get(request, TourRequestFinished);
}
public void loadPaging()
{
var request = new HttpGetRequest(paging);
Http.Get(request, FeedRequestFinished);
}
private void VideoListRequestFinished(HttpResponse response)
{
if (response.Successful)
{
// process response outside UI thread
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
// set your data to view model or control (this code is called in the UI thread)
var jsonResponse = response.Response;
JObject jsonObject = JObject.Parse(jsonResponse);
JArray entries = (JArray)jsonObject["feed"]["entry"];
for (int i = 0; i < entries.Count; i++)
{
VideoItemViewModel videoItem = new VideoItemViewModel();
string[] idSplit = entries[i]["id"]["$t"].ToString().Split(':');
videoItem.Id = (idSplit[idSplit.Length-1]);
videoItem.Title = entries[i]["title"]["$t"].ToString();
videoItem.Thumbnail = ((JArray)entries[i]["media$group"]["media$thumbnail"])[0]["url"].ToString();
videoItem.Date = entries[i]["published"]["$t"].ToString();
this.Videos.Add(videoItem);
}
videoBusy = false;
updateBusy();
});
}
else
{
if (!response.Canceled)
{
videoBusy = false;
updateBusy();
// display exception
MessageBox.Show("Error loading videos - Try again");
}
}
}
private void AccesTokenRequestFinished(HttpResponse response)
{
if (response.Successful)
{
// process response outside UI thread
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
// set your data to view model or control (this code is called in the UI thread)
var jsonResponse = response.Response;
var request = new HttpGetRequest("https://graph.facebook.com/ovaliantheartsmusic/feed");
request.Query.Add("access_token", jsonResponse.Split('=')[1]);
Http.Get(request, FeedRequestFinished);
});
}
else
{
if (!response.Canceled)
{
feedBusy = false;
updateBusy();
// display exception
MessageBox.Show("Error loading feeds - Try again");
}
}
}
private void FeedRequestFinished(HttpResponse response)
{
if (response.Successful)
{
// process response outside UI thread
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
// set your data to view model or control (this code is called in the UI thread)
var jsonResponse = response.Response;
JObject jsonObject = JObject.Parse(jsonResponse);
JArray entries = (JArray)jsonObject["data"];
for (int i = 0; i < entries.Count; i++)
{
if (entries[i]["message"] != null)
{
FeedItemViewModel feedItem = new FeedItemViewModel();
feedItem.Message = entries[i]["message"].ToString();
feedItem.Username = entries[i]["from"]["name"].ToString();
feedItem.Date = entries[i]["created_time"].ToString();
if (entries[i]["picture"] != null)
{ feedItem.Thumbnail = entries[i]["picture"].ToString(); }
if (entries[i]["link"] != null)
{ feedItem.Link = entries[i]["link"].ToString(); }
feeds.Add(feedItem);
}
}
if (jsonObject["paging"] != null)
{
paging = jsonObject["paging"]["next"].ToString();
}
else
{
paging = "";
}
feedBusy = false;
updateBusy();
});
}
else
{
if (!response.Canceled)
{
feedBusy = false;
updateBusy();
// display exception
MessageBox.Show("Error loading feeds - Try again");
}
}
}
private void TourRequestFinished(HttpResponse response)
{
if (response.Successful)
{
// process response outside UI thread
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
// set your data to view model or control (this code is called in the UI thread)
var jsonResponse = response.Response;
JObject jsonObject = JObject.Parse(jsonResponse);
JArray entries = (JArray)jsonObject["concerts"];
for (int i = 0; i < entries.Count; i++)
{
ConcertItemViewModel concertItem = new ConcertItemViewModel();
concertItem.Title = entries[i]["title"].ToString();
concertItem.Date = entries[i]["subtitle"].ToString();
concertItem.Latitude = entries[i]["latitude"].ToString();
concertItem.Longitude = entries[i]["longitude"].ToString();
this.Concerts.Add(concertItem);
}
tourBusy = false;
updateBusy();
});
}
else
{
if (!response.Canceled)
{
tourBusy = false;
updateBusy();
// display exception
MessageBox.Show("Error loading tour - Try again");
}
}
}
private void updateBusy()
{
if (!videoBusy && !feedBusy && !tourBusy)
Busy = false;
}
}
}