Skip to content

Commit

Permalink
Posting Photos
Browse files Browse the repository at this point in the history
Completed the api method for posting photos, as well as added in a
dialog to get a caption for a photo after selecting one. Also Made a
minor tweak to the selectedListModel getter to use a default if it's
null, and there's one available. This will prevent having the list tab
open and no list selected.
  • Loading branch information
xdumaine committed Mar 24, 2013
1 parent 5600e70 commit 7d746ed
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 28 deletions.
35 changes: 30 additions & 5 deletions Avocado/Models/AuthClient.cs
Expand Up @@ -11,6 +11,7 @@
using Newtonsoft.Json;
using System.Collections.ObjectModel;
using Avocado.ViewModels;
using Windows.Storage.Streams;

namespace Avocado.Models
{
Expand Down Expand Up @@ -114,6 +115,24 @@ public bool AttemptLogin()

#region DataAccessHelpers

private HttpResponseMessage PostBuffer(Uri uri, MultipartFormDataContent content)
{
var baseAddress = new Uri(API_URL_BASE);
var cookieContainer = new CookieContainer();
using (var handler = new HttpClientHandler() { CookieContainer = cookieContainer })
using (var client = new HttpClient(handler) { BaseAddress = baseAddress })
{
client.DefaultRequestHeaders.Add("User-Agent", USER_AGENT);
client.DefaultRequestHeaders.Add("X-AvoSig", AvoSignature);
cookieContainer.Add(baseAddress, new Cookie(COOKIE_NAME, CookieValue));

var response = client.PostAsync(uri, content);

return response.Result;

}
}

private HttpResponseMessage Post(Uri uri, FormUrlEncodedContent body)
{
var baseAddress = new Uri(API_URL_BASE);
Expand Down Expand Up @@ -266,14 +285,20 @@ public void SendMessage(string message)
response.EnsureSuccessStatusCode();
}

public void UploadPhoto(string photoContents)
public void UploadPhoto(string fileName, string contentType, System.IO.Stream photoContents, string caption = "")
{
var uri = new Uri(API_URL_MEDIA);
var body = new FormUrlEncodedContent(new[] {
new KeyValuePair<string, string>("media", photoContents)
});
var content = new MultipartFormDataContent();

content.Add(new StringContent(caption), "\"caption\"");

var t = new StreamContent(photoContents);
t.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(contentType);
fileName = string.IsNullOrEmpty(fileName) ? "img.png" : fileName;
content.Add(t, "\"media\"", "\"" + fileName + "\"");

var response = PostBuffer(uri, content);

var response = Post(uri, body);
response.EnsureSuccessStatusCode();
}

Expand Down
80 changes: 57 additions & 23 deletions Avocado/ViewModels/Activities.cs
Expand Up @@ -4,7 +4,6 @@
using System.Collections.Specialized;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using Avocado.Models;
Expand All @@ -16,12 +15,13 @@
using Windows.Foundation;
using Windows.Storage;
using Windows.Storage.Pickers;
using Windows.Storage.Streams;
using Windows.System;
using Windows.System.Threading;
using Windows.UI;
using Windows.UI.Notifications;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using WinRTXamlToolkit.Controls;

namespace Avocado.ViewModel
{
Expand All @@ -36,6 +36,24 @@ public enum TabType

class Activities : ObservableObject
{
public static string PostButtonText = "Post";
public static string CancelPostButtonText = "Cancel Post";
public static SolidColorBrush AvocadoGreen = new SolidColorBrush(Color.FromArgb(0xFF, 0x62, 0x94, 0x3C));
public static InputDialog CaptionDialog;


public Activities()
{
DispatcherHelper.Initialize();

// Set up the Caption dialog. This will be re-used frequently
CaptionDialog = new InputDialog();
CaptionDialog.BackgroundStripeBrush = AvocadoGreen;
CaptionDialog.Background = AvocadoGreen;
CaptionDialog.BorderBrush = new SolidColorBrush(Colors.Transparent);
CaptionDialog.AcceptButton = PostButtonText;
CaptionDialog.CancelButton = CancelPostButtonText;
}
public AuthClient AuthClient { get; set; }

private Couple couple;
Expand Down Expand Up @@ -112,7 +130,11 @@ public Media SelectedMediaListItem
private AvoList selectedListModel;
public AvoList SelectedListModel {
get
{
{
if (selectedListModel == null && ListModelList != null && ListModelList.Count > 0)
{
SelectedListModel = ListModelList.First();
}
return selectedListModel;
}
set
Expand All @@ -132,15 +154,6 @@ public Media SelectedMediaListItem
}
}

private object ListItemDrop(object t, NotifyCollectionChangedEventArgs y)
{
if (y.Action == NotifyCollectionChangedAction.Add)
{
EditListItem(((ObservableCollection<AvoListItem>)t)[y.NewStartingIndex], false, y.NewStartingIndex);
}
return t;
}

private string newMessage;
public string NewMessage
{
Expand Down Expand Up @@ -251,6 +264,15 @@ public bool IsLoading

#region ListItemActions

private object ListItemDrop(object t, NotifyCollectionChangedEventArgs y)
{
if (y.Action == NotifyCollectionChangedAction.Add)
{
EditListItem(((ObservableCollection<AvoListItem>)t)[y.NewStartingIndex], false, y.NewStartingIndex);
}
return t;
}

public void EditListItem(AvoListItem listItem, bool delete = false, int index = -1)
{
if (string.IsNullOrEmpty(listItem.ListId))
Expand Down Expand Up @@ -544,10 +566,11 @@ public void AddPhoto()

public async void PickPhoto()
{
IsLoading = true;
FileOpenPicker open = new FileOpenPicker();
open.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
open.ViewMode = PickerViewMode.Thumbnail;

// Filter to include a sample subset of file types
open.FileTypeFilter.Clear();
open.FileTypeFilter.Add(".bmp");
Expand All @@ -559,11 +582,28 @@ public async void PickPhoto()
StorageFile file = await open.PickSingleFileAsync();

// Ensure a file was selected
if (file != null)

if (file == null)
{
var stream = file.OpenStreamForReadAsync().Result;
AuthClient.UploadPhoto(stream);
IsLoading = false;
return;
}
var stream = await file.OpenStreamForReadAsync();


var result = await CaptionDialog.ShowAsync("Caption?", "Totally optional", PostButtonText, CancelPostButtonText);
string captionResult = CaptionDialog.InputText;
CaptionDialog.InputText = string.Empty;
if (result == CancelPostButtonText)
{
IsLoading = false;
return;
}
var task = ThreadPool.RunAsync(t => AuthClient.UploadPhoto(file.Name, file.ContentType, stream, captionResult));
task.Completed = RunOnComplete(Update);
//AuthClient.UploadPhoto(file.Name, file.ContentType, stream);
// ON UI Thread - we're here - do a confirm, then start the loading indicator, call an async operation to upload photo

}

public ICommand AddPhotoCommand
Expand All @@ -576,12 +616,6 @@ public ICommand AddPhotoCommand

#endregion

public Activities()
{
DispatcherHelper.Initialize();

}

public void ClearListActivities()
{
DispatcherHelper.CheckBeginInvokeOnUI(ConfirmClearListActivities);
Expand Down

0 comments on commit 7d746ed

Please sign in to comment.