Skip to content

Commit

Permalink
implemented sharing contract
Browse files Browse the repository at this point in the history
  • Loading branch information
jimoneil committed Jun 20, 2013
1 parent 46933c9 commit 85389c0
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,17 @@
using APIMASH_StarterKit.Mapping;
using Bing.Maps;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Threading.Tasks;
using Windows.ApplicationModel.DataTransfer;
using Windows.Storage;
using Windows.Storage.Streams;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using System.IO;
using System.Runtime.InteropServices.WindowsRuntime;

//
// LICENSE: http://aka.ms/LicenseTerms-SampleApps
Expand Down Expand Up @@ -110,7 +116,55 @@ public LeftPanel()
this.DefaultViewModel["ApiViewModel"] = _tomTomApi.TomTomViewModel;
_tomTomApi.TomTomViewModel.Results.CollectionChanged += Results_CollectionChanged;
}


//
// TODO: implement code needed to share item from main page
//
public async void GetSharedData(DataTransferManager sender, DataRequestedEventArgs args)
{
try
{
var currentCam = MappableListView.SelectedItem as APIMASH_TomTom.TomTomCameraViewModel;
if (currentCam != null)
{

DataRequestDeferral deferral = args.Request.GetDeferral();

args.Request.Data.Properties.Title = String.Format("TomTom Camera: {0}", currentCam.CameraId);
args.Request.Data.Properties.Description = currentCam.Name;

// share a file
var file = await StorageFile.CreateStreamedFileAsync(
String.Format("{0}_{1}.jpg", currentCam.CameraId, DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss")),
async stream =>
{
await stream.WriteAsync(currentCam.ImageBytes.AsBuffer());
await stream.FlushAsync();
stream.Dispose();
},
null);
args.Request.Data.SetStorageItems(new List<IStorageItem> { file });

// share as bitmap
InMemoryRandomAccessStream raStream = new InMemoryRandomAccessStream();
await raStream.WriteAsync(currentCam.ImageBytes.AsBuffer());
await raStream.FlushAsync();
args.Request.Data.SetBitmap(RandomAccessStreamReference.CreateFromStream(raStream));

deferral.Complete();
}
else
{
args.Request.FailWithDisplayText("Select a camera to share its image.");
}
}
catch (Exception ex)
{
args.Request.FailWithDisplayText(ex.Message);
}
}


/// <summary>
/// Carries out application-specific handling of the item selected in the listview. The synchronization with
/// the map display is already accomodated.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.Threading.Tasks;
using Windows.ApplicationModel.DataTransfer;
using Windows.ApplicationModel.Search;
using Windows.Devices.Geolocation;
using Windows.Storage;
Expand Down Expand Up @@ -250,6 +251,17 @@ protected override void OnNavigatedTo(NavigationEventArgs e)
// reset map to last known view
TheMap.SetView(new Location(_pageState.MapCenter.Latitude, _pageState.MapCenter.Longitude), _pageState.Zoom, MapAnimationDuration.None);
}

DataTransferManager dtm = DataTransferManager.GetForCurrentView();
dtm.DataRequested += LeftPanel.GetSharedData;
}

protected override void OnNavigatedFrom(NavigationEventArgs e)
{
base.OnNavigatedFrom(e);

DataTransferManager dtm = DataTransferManager.GetForCurrentView();
dtm.DataRequested -= LeftPanel.GetSharedData;
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ public class TomTomCameraViewModel : BindableBase, IMappable
public String Orientation { get; set; }
public Int32 RefreshRate { get; set; }

private Byte[] _imageBytes;
public Byte[] ImageBytes { get; set; }

private BitmapImage _image;
public BitmapImage Image
{
Expand Down Expand Up @@ -130,9 +133,10 @@ public static Boolean PopulateViewModel(cameras model, ObservableCollection<TomT
return resultsWereTruncated;
}

public static void PopulateViewModel(BitmapImage camImage, TomTomCameraViewModel viewModel)
public static void PopulateViewModel(BitmapImage camImage, Byte[] imageBytes, TomTomCameraViewModel viewModel)
{
viewModel.Image = camImage;
viewModel.ImageBytes = imageBytes;
viewModel.LastRefresh = DateTime.UtcNow;
}
}
Expand Down Expand Up @@ -259,7 +263,7 @@ public async Task<APIMASH.ApiResponseStatus> GetCameraImage(TomTomCameraViewMode
}

// populate the ViewModel with the image
TomTomCamerasModel.PopulateViewModel(cameraImage, camera);
TomTomCamerasModel.PopulateViewModel(cameraImage, apiResponse.RawResponse, camera);

// return a success status (there will always be an image returned)
return ApiResponseStatus.Default;
Expand Down

0 comments on commit 85389c0

Please sign in to comment.