Skip to content

Commit

Permalink
Update PhotoSkyOnTheGo to use Background Transfer
Browse files Browse the repository at this point in the history
  • Loading branch information
skrueger committed Jul 12, 2012
1 parent 638e5ae commit 9ae3970
Showing 1 changed file with 87 additions and 78 deletions.
Expand Up @@ -25,20 +25,14 @@ namespace PhotoSkyOnTheGo
public class LiveServicesViewModel : INotifyPropertyChanged
{
#region Constructor

public LiveServicesViewModel()
{
this.Albums = new ObservableCollection<SkydriveAlbum>();
}

#endregion

#region Private Members
/// <summary>
/// A collection for ItemViewModel objects.
/// </summary>

#endregion

#region Properties

public ObservableCollection<SkydriveAlbum> Albums { get; private set; }
Expand Down Expand Up @@ -74,6 +68,7 @@ public string FullName
NotifyPropertyChanged("FullName");
}
}

public bool IsDataLoaded
{
get;
Expand Down Expand Up @@ -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>
Expand All @@ -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");
Expand All @@ -144,7 +140,6 @@ void clientGetPicture_GetCompleted(object sender, LiveOperationCompletedEventArg
{
if (e.Error == null)
{

ProfileImage = (string)e.Result["location"];
}
}
Expand All @@ -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);
}
}

Expand Down Expand Up @@ -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)
Expand All @@ -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();
Expand All @@ -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));

}
}
}
Expand Down

0 comments on commit 9ae3970

Please sign in to comment.