Skip to content

Commit

Permalink
Added ability to pin user profiles
Browse files Browse the repository at this point in the history
- Changed background agent to only update primary tile
- Added TilesCreator class that takes care of creating tiles
- Added ShellTileExtension to use the ShellTile.Create(Uri,
ShellTileData,Bool) overload
  • Loading branch information
ChrisK91 committed Sep 7, 2013
1 parent e1e0389 commit 397409a
Show file tree
Hide file tree
Showing 9 changed files with 254 additions and 9 deletions.
19 changes: 13 additions & 6 deletions SparklrWP Background Agent/ScheduledAgent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,19 @@ protected async override void OnInvoke(ScheduledTask task)

foreach (ShellTile til in ShellTile.ActiveTiles)
{
Mangopollo.Tiles.FlipTileData data = new Mangopollo.Tiles.FlipTileData();
data.Title = "Sparklr*";
data.Title = "Sparklr*";
data.BackgroundImage = new Uri("/Background.png", UriKind.Relative);
data.BackBackgroundImage = new Uri("http://til.eaterofcorps.es/?url=http%3A%2F%2Fd.sparklr.me%2Fi%2F" + strm.notifications[0].from + ".jpg&text=" + Uri.EscapeDataString(String.Format(textGenerator(strm.notifications[0]), client.Usernames[strm.notifications[0].from])));
til.Update(data);
//Update only primary tile
if (til.NavigationUri.ToString() == "/")
{
Mangopollo.Tiles.FlipTileData data = new Mangopollo.Tiles.FlipTileData();
data.Title = "Sparklr*";
data.Title = "Sparklr*";
data.BackgroundImage = new Uri("/Background.png", UriKind.Relative);
data.BackBackgroundImage = new Uri("http://til.eaterofcorps.es/?url=http%3A%2F%2Fd.sparklr.me%2Fi%2F" + strm.notifications[0].from + ".jpg&text=" + Uri.EscapeDataString(String.Format(textGenerator(strm.notifications[0]), client.Usernames[strm.notifications[0].from])));
til.Update(data);

//We can only have one primary tily --> break
break;
}
}

foreach (Notification not in strm.notifications)
Expand Down
Binary file added SparklrWP/Assets/TileBackgrounds/Medium.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added SparklrWP/Assets/TileBackgrounds/Small.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added SparklrWP/Assets/TileBackgrounds/Wide.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 11 additions & 0 deletions SparklrWP/Pages/Profile.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,17 @@
</sparklrControls:ExtendedListBox>
</controls:PanoramaItem>

<controls:PanoramaItem Header="">
<toolkit:WrapPanel>
<toolkit:HubTile Size="Large" Title="{Binding Handle}" x:Name="userProfileTile" >
<toolkit:ContextMenuService.ContextMenu>
<toolkit:ContextMenu>
<toolkit:MenuItem Header="Pin to start..." Click="PinProfileMenuItem_Click" />
</toolkit:ContextMenu>
</toolkit:ContextMenuService.ContextMenu>
</toolkit:HubTile>
</toolkit:WrapPanel>
</controls:PanoramaItem>
<!--<controls:PanoramaItem Header="more">
<StackPanel>
<Button Click="MessageButton_Click">message</Button>
Expand Down
22 changes: 19 additions & 3 deletions SparklrWP/Pages/Profile.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using SparklrLib.Objects.Responses;
using SparklrLib.Objects.Responses.Work;
using SparklrWP.Controls;
using SparklrWP.Utils;
using System;
using System.Net;
using System.Windows.Media;
Expand Down Expand Up @@ -115,6 +116,16 @@ private void refreshUserDetails(JSONRequestEventArgs<SparklrLib.Objects.Response
{
model.Bio = usargs.Object.name + " is too shy to write something about his/herself maybe check again later!";
}

updateProfileTile();
}

private async void updateProfileTile()
{
if (await Utils.TilesCreator.CreateProfileTileImages(model.ID))
{
userProfileTile.Source = Utils.TilesCreator.LoadProfileTileImage(model.ID, Utils.TileSize.Wide);
}
}

private static PostItemViewModel generateItemViewModel(SparklrLib.Objects.Responses.Work.Timeline t)
Expand Down Expand Up @@ -164,12 +175,12 @@ private void filterModePicker_SelectionChanged(object sender, System.Windows.Con
}
}

private void MentionButton_Click(object sender, System.Windows.RoutedEventArgs e)
private void MentionButton_Click(object sender, EventArgs e)
{
NavigationService.Navigate(new Uri(String.Format("/NewPostPage.xaml?content={0}", HttpUtility.UrlEncode(String.Format(model.Handle))), UriKind.Relative));
}

private async void FollowButton_Click(object sender, System.Windows.RoutedEventArgs e)
private async void FollowButton_Click(object sender, EventArgs e)
{
if (!model.Following)
{
Expand All @@ -196,7 +207,7 @@ private async void FollowButton_Click(object sender, System.Windows.RoutedEventA
refreshUserDetails(userargs);
}

private void MessageButton_Click(object sender, System.Windows.RoutedEventArgs e)
private void MessageButton_Click(object sender, EventArgs e)
{
NavigationService.Navigate(new Uri("/Pages/ChatPage.xaml?id=" + model.ID, UriKind.Relative));
}
Expand All @@ -207,5 +218,10 @@ private void SparklrPostControl_Tap(object sender, System.Windows.Input.GestureE
if (control != null)
NavigationService.Navigate(new Uri("/Pages/DetailsPage.xaml?id=" + control.Post.Id, UriKind.Relative));
}

private void PinProfileMenuItem_Click(object sender, System.Windows.RoutedEventArgs e)
{
Utils.TilesCreator.PinUserprofile(model.ID);
}
}
}
6 changes: 6 additions & 0 deletions SparklrWP/SparklrWP.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -227,9 +227,11 @@
<Compile Include="Utils\ListToGroupedListConverter.cs" />
<Compile Include="Utils\MemoryDiagnosticsHelper.cs" />
<Compile Include="Utils\ObservableCollectionWithItemNotification.cs" />
<Compile Include="Utils\ShellTileExtensions.cs" />
<Compile Include="Utils\SmartDispatcher.cs" />
<Compile Include="Utils\StringExtensions.cs" />
<Compile Include="Utils\Task.cs" />
<Compile Include="Utils\TilesCreator.cs" />
<Compile Include="ViewModels\CommentModel.cs" />
<Compile Include="ViewModels\FriendViewModel.cs" />
<Compile Include="ViewModels\InboxViewModel.cs" />
Expand Down Expand Up @@ -389,6 +391,9 @@
<Content Include="Assets\t19.jpg" />
<Content Include="Assets\t427.jpg" />
<Content Include="Assets\t591.jpg" />
<Content Include="Assets\TileBackgrounds\Medium.png" />
<Content Include="Assets\TileBackgrounds\Small.png" />
<Content Include="Assets\TileBackgrounds\Wide.png" />
<Content Include="Controls\appbar.message.send.png" />
<Content Include="Icons\appbar.apply.rest.png" />
<Content Include="Icons\appbar.cancel.rest.png" />
Expand Down Expand Up @@ -450,6 +455,7 @@
<LastGenOutput>AppResource.Designer.cs</LastGenOutput>
</EmbeddedResource>
</ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildExtensionsPath)\Microsoft\Silverlight for Phone\$(TargetFrameworkVersion)\Microsoft.Silverlight.$(TargetFrameworkProfile).Overrides.targets" />
<Import Project="$(MSBuildExtensionsPath)\Microsoft\Silverlight for Phone\$(TargetFrameworkVersion)\Microsoft.Silverlight.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Expand Down
16 changes: 16 additions & 0 deletions SparklrWP/Utils/ShellTileExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Microsoft.Phone.Shell;
using System;
using System.Reflection;

namespace SparklrWP.Utils
{
public static class ShellTileExtensions
{
public static void CreateWideTile(Uri uri, ShellTileData tiledata)
{
Type shellTileType = Type.GetType("Microsoft.Phone.Shell.ShellTile, Microsoft.Phone");
MethodInfo createmethod = shellTileType.GetMethod("Create", new Type[] { typeof(Uri), typeof(ShellTileData), typeof(bool) });
createmethod.Invoke(null, new object[] { uri, tiledata, true });
}
}
}
189 changes: 189 additions & 0 deletions SparklrWP/Utils/TilesCreator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
using Microsoft.Phone.Shell;
using SparklrLib.Objects;
using SparklrLib.Objects.Responses.Work;
using System;
using System.Collections.Generic;
using System.IO.IsolatedStorage;
using System.Threading.Tasks;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace SparklrWP.Utils
{
public enum TileSize
{
Small,
Medium,
Wide
}

public static class TilesCreator
{
private const string TilesFolder = @"Shared\ShellContent";
private const int SmallWidth = 156;
private const int SmallHeight = 156;
private const int MediumWidth = 336;
private const int MediumHeight = 336;
private const int WideWidth = 691;
private const int WideHeight = 336;

static TilesCreator()
{
#if DEBUG
using (IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication())
{
if (storage.DirectoryExists(TilesFolder))
{
string filter = System.IO.Path.Combine(TilesFolder, "*");
foreach (string file in storage.GetFileNames(filter))
{
string fileName = System.IO.Path.Combine(TilesFolder, file);
storage.DeleteFile(fileName);
App.logger.log("Deleted tile file {0}", fileName);
}
}
}
#endif
}

public static async Task<bool> PinUserprofile(int userid)
{
JSONRequestEventArgs<User> userdata = await App.Client.GetUserAsync(userid);

if (userdata.IsSuccessful)
{
if (await CreateProfileTileImages(userid))
{
ShellTileData data = Mangopollo.Tiles.TilesCreator.CreateFlipTile(
userdata.Object.name,
"",
"",
"",
null,
new Uri("/Assets/TileBackgrounds/Small.png", UriKind.Relative),
new Uri("/Assets/TileBackgrounds/Medium.png", UriKind.Relative),
new Uri("isostore:/" + GetUserprofileTileLocation(userid, TileSize.Medium).Replace(@"\", @"/"), UriKind.Absolute),
new Uri("/Assets/TileBackgrounds/Wide.png", UriKind.Relative),
new Uri("isostore:/" + GetUserprofileTileLocation(userid, TileSize.Wide).Replace(@"\", @"/"), UriKind.Absolute)
);

ShellTileExtensions.CreateWideTile(new Uri("/Pages/Profile.xaml?userId=" + userid.ToString(), UriKind.Relative), data);
return true;
}
}
return false;
}

//TODO: make async
public async static Task<bool> CreateProfileTileImages(int userId)
{
using (IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication())
{
if (!isoStore.DirectoryExists(TilesFolder))
{
isoStore.CreateDirectory(TilesFolder);
}

string location = "http://d.sparklr.me/i/b" + userId + ".jpg";

string smallLocation = GetUserprofileTileLocation(userId, TileSize.Small);
string mediumLocation = GetUserprofileTileLocation(userId, TileSize.Medium);
string wideLocation = GetUserprofileTileLocation(userId, TileSize.Wide);

Rectangle r = new Rectangle();
ImageBrush image = new ImageBrush();
image.ImageSource = (BitmapImage)await Utils.Caching.Image.LoadCachedImageFromUrlAsync<BitmapImage>(location);
image.Stretch = Stretch.UniformToFill;
r.Fill = image;

try
{
if (!isoStore.FileExists(smallLocation))
{
saveTileImage(isoStore, smallLocation, r, SmallWidth, SmallHeight);
}

if (!isoStore.FileExists(mediumLocation))
{
saveTileImage(isoStore, mediumLocation, r, MediumWidth, MediumHeight);
}

if (!isoStore.FileExists(wideLocation))
{
saveTileImage(isoStore, wideLocation, r, WideWidth, WideHeight);
}

return true;
}
catch (System.IO.IOException)
{
#if DEBUG
if (System.Diagnostics.Debugger.IsAttached)
System.Diagnostics.Debugger.Break();
#endif

return false;
}
}
}

private static void saveTileImage(IsolatedStorageFile isoStore, string location, Rectangle r, int width, int height)
{
r.Width = width;
r.Height = height;
r.UpdateLayout();

using (IsolatedStorageFileStream fs = isoStore.CreateFile(location))
{
WriteableBitmap smallTile = new WriteableBitmap(r, null);
smallTile.SaveJpeg(fs, width, height, 0, 100);

#if DEBUG
App.logger.log("Created tile image {0}", location);
#endif
}
}

public static BitmapImage LoadProfileTileImage(int userid, TileSize size)
{
string location = GetUserprofileTileLocation(userid, size);

using (IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication())
{
if (isoStore.FileExists(location))
{
BitmapImage ret = new BitmapImage();
using (IsolatedStorageFileStream fs = isoStore.OpenFile(location, System.IO.FileMode.Open))
{
ret.SetSource(fs);
#if DEBUG
App.logger.log("loaded file {0}", location);
#endif
}
return ret;
}
else
{
return null;
}
}
}


public static string GetUserprofileTileLocation(int userid, TileSize size)
{
switch (size)
{
case TileSize.Small:
return System.IO.Path.Combine(TilesFolder, String.Format("{0}_{1}.jpg", userid, SmallWidth));
case TileSize.Medium:
return System.IO.Path.Combine(TilesFolder, String.Format("{0}_{1}.jpg", userid, MediumWidth));
case TileSize.Wide:
return System.IO.Path.Combine(TilesFolder, String.Format("{0}_{1}.jpg", userid, WideWidth));
}

//TODO: add better exception type
throw new Exception("size not set properly");
}
}
}

0 comments on commit 397409a

Please sign in to comment.