/
AlbumPage.xaml.cs
298 lines (272 loc) · 10.1 KB
/
AlbumPage.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
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 Microsoft.Live;
using System.Windows.Media.Imaging;
using System.Collections.ObjectModel;
using Microsoft.Xna.Framework.GamerServices;
using System.Diagnostics;
using Microsoft.Phone.Shell;
namespace SkyPhoto
{
/// <summary>
/// AlbumPage page shows all picture folder from sky drive
/// </summary>
public partial class AlbumPage : PhoneApplicationPage
{
/// <summary>
/// collections of picture folder
/// </summary>
public ObservableCollection<SkydriveAlbum> Albums { get; private set; }
/// <summary>
/// number of images in each foder ImageCounte. ImageCounte hide progessbar
/// upon completeing all piture
/// </summary>
private int ImageCounter { get; set; }
/// <summary>
/// NewAlbumName holds user input for new album name
/// </summary>
private string NewAlbumName { get; set; }
/// <summary>
/// AlbumPage class contructor
/// </summary>
public AlbumPage()
{
InitializeComponent();
ApplicationBarIconButton album = ApplicationBar.Buttons[0] as ApplicationBarIconButton;
ApplicationBarIconButton refresh = ApplicationBar.Buttons[1] as ApplicationBarIconButton;
NewAlbumName = null;
album.Text = SkyPhoto.Resources.Resources.NewAlubum;
refresh.Text = SkyPhoto.Resources.Resources.Refresh;
Albums = new ObservableCollection<SkydriveAlbum>();
AlbumListBox.ItemsSource = Albums;
GetAlubmData();
}
/// <summary>
/// GetAlubmData gets all the album
/// </summary>
private async void GetAlubmData()
{
ImageCounter = 0;
// show progressbar.
string progMsgDownAlbum = SkyPhoto.Resources.Resources.progMsgDownAlbum;
ShowProgressBar(progMsgDownAlbum);
LiveConnectClient clientFolder = new LiveConnectClient(App.Session);
LiveOperationResult result = await clientFolder.GetAsync("/me/albums");
GetAlubmData_Completed(result);
}
/// <summary>
/// LiveConnectClient event handler
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void GetAlubmData_Completed(LiveOperationResult e)
{
List<object> data = (List<object>)e.Result["data"];
if (ImageCounter == 0)
{
ImageCounter = data.Count;
}
foreach (IDictionary<string, object> album in data)
{
SkydriveAlbum albumItem = new SkydriveAlbum();
albumItem.Title = (string)album["name"];
albumItem.Description = (string)album["description"];
albumItem.ID = (string)album["id"];
Albums.Add(albumItem);
GetAlbumPicture(albumItem);
}
}
/// <summary>
/// GetAlbumPicture get all the picture from each album
/// </summary>
/// <param name="albumItem"></param>
private async void GetAlbumPicture(SkydriveAlbum albumItem)
{
try
{
LiveConnectClient albumPictureClient = new LiveConnectClient(App.Session);
LiveOperationResult result = await albumPictureClient.GetAsync(albumItem.ID + "/picture");
albumPictureClient_GetCompleted(result, albumItem);
}
catch (LiveConnectException lce)
{
albumPictureClient_GetCompleted(null, albumItem);
}
}
/// <summary>
/// LiveConnectClient event handler
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void albumPictureClient_GetCompleted(LiveOperationResult e, SkydriveAlbum album)
{
if (e != null)
{
album.AlbumPicture = (string)e.Result["location"];
}
else
{
album.AlbumPicture = "/icons/empty_album.png";
}
// hide progressbar.
ImageCounter--;
if (ImageCounter == 0)
{
HideProgressBar();
}
}
/// <summary>
/// AlbumListBox listbox tap event handler
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void AlbumListBox_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
ListBox lb = sender as ListBox;
App.CurrentAlbum = Albums[lb.SelectedIndex];
NavigationService.Navigate(new Uri("/AlbumDetailPage.xaml", UriKind.Relative));
}
/// <summary>
/// shows progessbar
/// </summary>
/// <param name="progressText"></param>
private void ShowProgressBar(string progressText)
{
// Making progessbar visible
Loading.Visibility = Visibility.Visible;
ContentPanel.Opacity = 0.25;
progressBar.IsIndeterminate = true;
progressBarText.Text = progressText;
ContentPanel.IsHitTestVisible = false;
(ApplicationBar as ApplicationBar).IsVisible = false;
}
/// <summary>
/// hide progressbar
/// </summary>
private void HideProgressBar()
{
// Making progress bar invisible.
Loading.Visibility = Visibility.Collapsed;
ContentPanel.Opacity = 1;
ContentPanel.IsHitTestVisible = true;
progressBar.IsIndeterminate = false;
progressBarText.Text = string.Empty;
(ApplicationBar as ApplicationBar).IsVisible = true;
}
/// <summary>
/// ContextMenu delete button tergar MenuDelte_Click
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private async void MenuDelte_Click(object sender, RoutedEventArgs e)
{
var Menu = sender as MenuItem;
SkydriveAlbum selectedAlbum = Menu.DataContext as SkydriveAlbum;
string msgDel = SkyPhoto.Resources.Resources.msgDelAlbum;
string msgDelAlbumTitel = SkyPhoto.Resources.Resources.msgDelAlbumTitel;
MessageBoxResult m = MessageBox.Show(msgDel + selectedAlbum.Title + "?", msgDelAlbumTitel, MessageBoxButton.OKCancel);
if (m == MessageBoxResult.OK)
{
LiveConnectClient client = new LiveConnectClient(App.Session);
LiveOperationResult result = await client.DeleteAsync(selectedAlbum.ID);
DeleteFolder_Completed(selectedAlbum, result);
}
else
{
return;
}
}
/// <summary>
/// LiveConnectClient event handler
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void DeleteFolder_Completed(SkydriveAlbum selectedAlbum, LiveOperationResult e)
{
if (e.Result != null)
{
Albums.Remove(selectedAlbum);
}
else
{
string msgDelFailed = SkyPhoto.Resources.Resources.msgDelFailed;
MessageBox.Show(msgDelFailed);
}
}
/// <summary>
/// menubar button new album click tregar New_album_click
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void New_album_click(object sender, EventArgs e)
{
string fileName = SkyPhoto.Resources.Resources.fileName;
Guide.BeginShowKeyboardInput(Microsoft.Xna.Framework.PlayerIndex.One, fileName, " ", " ", new AsyncCallback(GetInputString), null);
}
/// <summary>
/// get input string from album name input dialog
/// </summary>
/// <param name="res"></param>
void GetInputString(IAsyncResult res)
{
NewAlbumName = Guide.EndShowKeyboardInput(res);
// check null or empty string.
if (string.IsNullOrEmpty(NewAlbumName) ||
string.IsNullOrWhiteSpace(NewAlbumName))
{
return;
}
Create_new_album();
}
/// <summary>
/// Create_new_album crates a new album
/// </summary>
private async void Create_new_album()
{
Dictionary<string, object> folderData = new Dictionary<string, object>();
folderData.Add("name", NewAlbumName);
folderData.Add("type", "album");
LiveConnectClient client = new LiveConnectClient(App.Session);
LiveOperationResult result = await client.PostAsync("me/skydrive", folderData);
CreateFolder_Completed(result);
}
void CreateFolder_Completed(LiveOperationResult result)
{
if (result != null)
{
// succesful.
Dispatcher.BeginInvoke(() =>
{
Albums.Clear();
GetAlubmData();
}
);
}
else
{
string newAlbumFaild = SkyPhoto.Resources.Resources.newAlbumFaild;
MessageBox.Show(newAlbumFaild);
}
}
/// <summary>
/// menubar button refresh click
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Refresh_Click(object sender, EventArgs e)
{
Albums.Clear();
GetAlubmData();
}
}
}