Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Update PhotoSkyOnTheGo to use Background Transfer

  • Loading branch information...
commit 9ae3970e4c22d33e09d39d5d6c6147c6dfdc3dc0 1 parent 638e5ae
@skrueger skrueger authored
View
165 Samples/WindowsPhone/LiveSDKSample/PhotoSkyOnTheGo/ViewModels/LiveServicesViewModel.cs
@@ -25,6 +25,7 @@ namespace PhotoSkyOnTheGo
public class LiveServicesViewModel : INotifyPropertyChanged
{
#region Constructor
+
public LiveServicesViewModel()
{
this.Albums = new ObservableCollection<SkydriveAlbum>();
@@ -32,13 +33,6 @@ public LiveServicesViewModel()
#endregion
- #region Private Members
- /// <summary>
- /// A collection for ItemViewModel objects.
- /// </summary>
-
- #endregion
-
#region Properties
public ObservableCollection<SkydriveAlbum> Albums { get; private set; }
@@ -74,6 +68,7 @@ public string FullName
NotifyPropertyChanged("FullName");
}
}
+
public bool IsDataLoaded
{
get;
@@ -115,9 +110,11 @@ public SkydrivePhoto SelectedPhoto
NotifyPropertyChanged("SelectedPhoto");
}
}
+
#endregion
#region Methods
+
/// <summary>
/// Creates and adds a few ItemViewModel objects into the Items collection.
/// </summary>
@@ -130,7 +127,6 @@ public void LoadData()
private void GetProfileData()
{
-
LiveConnectClient clientGetMe = new LiveConnectClient(App.Session);
clientGetMe.GetCompleted += new EventHandler<LiveOperationCompletedEventArgs>(clientGetMe_GetCompleted);
clientGetMe.GetAsync("me");
@@ -144,7 +140,6 @@ void clientGetPicture_GetCompleted(object sender, LiveOperationCompletedEventArg
{
if (e.Error == null)
{
-
ProfileImage = (string)e.Result["location"];
}
}
@@ -166,22 +161,24 @@ private void GetAlubmData()
void clientFolder_GetCompleted(object sender, LiveOperationCompletedEventArgs e)
{
- if (e.Error == null)
+ if (e.Error != null)
{
- List<object> data = (List<object>)e.Result["data"];
+ return;
+ }
- 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);
- DownloadPictures(albumItem);
- }
+ List<object> data = (List<object>)e.Result["data"];
+
+ 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);
+ DownloadPictures(albumItem);
}
}
@@ -210,41 +207,41 @@ private void DownloadPictures(SkydriveAlbum albumItem)
void folderListClient_GetCompleted(object sender, LiveOperationCompletedEventArgs e)
{
- if (e.Error == null)
+ if (e.Error != null)
{
- int i = 0;
- SkydriveAlbum album = (SkydriveAlbum)e.UserState;
+ return;
+ }
- album.Photos.Clear();
- List<object> data = (List<object>)e.Result["data"];
+ int i = 0;
+ SkydriveAlbum album = (SkydriveAlbum)e.UserState;
- foreach (IDictionary<string, object> photo in data)
- {
- var item = new SkydrivePhoto();
- item.Title = (string)photo["name"];
- item.Subtitle = (string)photo["name"];
+ album.Photos.Clear();
+ List<object> data = (List<object>)e.Result["data"];
- item.PhotoUrl = (string)photo["source"];
- item.Description = (string)photo["description"];
- item.ID = (string)photo["id"];
+ foreach (IDictionary<string, object> photo in data)
+ {
+ var item = new SkydrivePhoto();
+ item.Title = (string)photo["name"];
+ item.Subtitle = (string)photo["name"];
- if (album != null)
- {
- album.Photos.Add(item);
- }
- // Stop after downloaing 10 imates
- if (i++ > 10)
- break;
+ item.PhotoUrl = (string)photo["source"];
+ item.Description = (string)photo["description"];
+ item.ID = (string)photo["id"];
+
+ if (album != null)
+ {
+ album.Photos.Add(item);
}
+ // Stop after downloaing 10 imates
+ if (i++ > 10)
+ break;
}
-
-
-
-
}
+
#endregion
#region INPC
+
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String propertyName)
@@ -255,44 +252,30 @@ private void NotifyPropertyChanged(String propertyName)
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
+
#endregion
internal void Download()
{
if (SelectedPhoto == null)
+ {
return;
-
+ }
LiveConnectClient downloadClient = new LiveConnectClient(App.Session);
- downloadClient.DownloadCompleted += new EventHandler<LiveDownloadCompletedEventArgs>(downloadClient_DownloadCompleted);
- downloadClient.DownloadAsync(SelectedPhoto.ID+"/content");
+ downloadClient.BackgroundDownloadCompleted +=
+ new EventHandler<LiveOperationCompletedEventArgs>(downloadClient_BackgroundDownloadCompleted);
+
+ string path = SelectedPhoto.ID + "/content";
+ Uri downloadLocation = new Uri("/shared/transfers/" + SelectedPhoto.Title, UriKind.RelativeOrAbsolute);
+ string userState = SelectedPhoto.ID; // arbitrary string to uniquely identify the request.
+ downloadClient.BackgroundDownload(path, downloadLocation, userState);
}
- void downloadClient_DownloadCompleted(object sender, LiveDownloadCompletedEventArgs e)
+ private void downloadClient_BackgroundDownloadCompleted(object sender, LiveOperationCompletedEventArgs e)
{
- if (e.Error == null)
- {
- MemoryStream outputStream = (MemoryStream)e.Result;
-
-
- // Create a filename for JPEG file in isolated storage.
- String tempJPEG = SelectedPhoto.Title;
-
- // Create virtual store and file stream. Check for duplicate tempJPEG files.
- var myStore = IsolatedStorageFile.GetUserStoreForApplication();
- if (myStore.FileExists(tempJPEG))
- {
- myStore.DeleteFile(tempJPEG);
- }
- IsolatedStorageFileStream myFileStream = myStore.CreateFile(tempJPEG);
- myFileStream.Write(outputStream.GetBuffer(), 0, (int)outputStream.Length);
- myFileStream.Close();
- }
-
}
-
-
internal void Upload()
{
PhotoChooserTask task = new PhotoChooserTask();
@@ -304,20 +287,46 @@ internal void Upload()
void task_Completed(object sender, PhotoResult e)
{
if (e.ChosenPhoto == null)
+ {
return;
+ }
+
+ string uploadLocation = "/shared/transfers/Image" + DateTime.Now.Millisecond + ".jpg";
+ BackgroundWorker worker = new BackgroundWorker();
+ worker.DoWork += (o, args) =>
+ {
+ using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())
+ {
+ using (IsolatedStorageFileStream stream = store.CreateFile(uploadLocation))
+ {
+ byte[] buffer = new byte[1 << 10];
+ int bytesRead;
+ while ((bytesRead = e.ChosenPhoto.Read(buffer, 0, buffer.Length)) > 0)
+ {
+ stream.Write(buffer, 0, bytesRead);
+ }
+ }
+ }
+ };
+
+ worker.RunWorkerCompleted += (o, args) =>
+ {
+ LiveConnectClient uploadClient = new LiveConnectClient(App.Session);
+ uploadClient.BackgroundUploadCompleted +=
+ new EventHandler<LiveOperationCompletedEventArgs>(uploadClient_BackgroundUploadCompleted);
+
+ string userState = "myUserState"; // arbitrary string to identify the request.
+ uploadClient.BackgroundUpload(SelectedAlbum.ID, new Uri(uploadLocation, UriKind.RelativeOrAbsolute), userState);
+ };
- LiveConnectClient uploadClient = new LiveConnectClient(App.Session);
- uploadClient.UploadCompleted += new EventHandler<LiveOperationCompletedEventArgs>(uploadClient_UploadCompleted);
- uploadClient.UploadAsync(SelectedAlbum.ID, "Image"+DateTime.Now.Millisecond+".jpg", e.ChosenPhoto);
+ worker.RunWorkerAsync();
}
- void uploadClient_UploadCompleted(object sender, LiveOperationCompletedEventArgs e)
+ private void uploadClient_BackgroundUploadCompleted(object sender, LiveOperationCompletedEventArgs e)
{
if (e.Error == null)
{
-
Deployment.Current.Dispatcher.BeginInvoke(() => DownloadPictures(SelectedAlbum));
-
}
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.