Skip to content
This repository has been archived by the owner on May 7, 2021. It is now read-only.

Commit

Permalink
This closes #24 and #16. Changed the core logic...
Browse files Browse the repository at this point in the history
This closes #24 and #16. Changed the core logic to use a index file.
Added ImageBrush ImageExtensions control.
  • Loading branch information
angusbreno committed Jun 23, 2015
1 parent 22fbd4c commit 7bb7b00
Show file tree
Hide file tree
Showing 4 changed files with 239 additions and 52 deletions.
15 changes: 14 additions & 1 deletion Q42.WinRT.SampleApp/ViewModel/DataExampleViewModel.cs
Expand Up @@ -56,7 +56,16 @@ public string CacheRefreshResult
RaisePropertyChanged("CacheRefreshResult");
}
}


private Uri _imageBrushUri;

public Uri ImageBrushUri
{
get { return _imageBrushUri; }
set { Set(ref _imageBrushUri, value); }
}



public DataLoader StartLongRunningDataLoader { get; set; }
public DataLoader StartLongRunningWithExceptionDataLoader { get; set; }
Expand Down Expand Up @@ -107,6 +116,10 @@ public DataExampleViewModel()
ClearWebDataCacheCommand = new RelayCommand(() => ClearWebDataCacheCommandAction());
GetUriCommand = new RelayCommand(() => GetUriCommandAction());

ImageBrushUri = new Uri("https://pbs.twimg.com/profile_images/478304416148099072/1_rxoQgR.png");

WebDataCache.Init();

}


Expand Down
21 changes: 18 additions & 3 deletions Q42.WinRT.SampleApp/Views/DataExamplePage.xaml
Expand Up @@ -125,11 +125,26 @@
<TextBlock FontSize="18">Source already caches http uri's by default. This extension can be used when you want to cache https data.</TextBlock>
<Image HorizontalAlignment="Left" Stretch="None" q42controls:ImageExtensions.CacheUri="https://www.google.nl/favicon.ico" />
<TextBlock>Normal:</TextBlock>
<Image HorizontalAlignment="Left" Stretch="None" Source="https://www.google.nl/favicon.ico" />

<Image HorizontalAlignment="Left" Stretch="None" Source="https://www.google.nl/favicon.ico" />

<TextBlock>Image brush:</TextBlock>

<Ellipse
Height="80"
Width="80"
HorizontalAlignment="Left"
>
<Ellipse.Fill>
<ImageBrush
Stretch="UniformToFill"
q42controls:ImageExtensions.CacheUri="{Binding ImageBrushUri}"
/>
</Ellipse.Fill>
</Ellipse>

<TextBlock Margin="0,30,0,0" FontSize="18">Experimental: Uri result caching (debug to see in action)</TextBlock>
<Button Command="{Binding GetUriCommand}">Get result of Uri http://microsoft.com (from cache or internet)</Button>
<!--<Button Command="{Binding ClearWebDataCacheCommand}">Clear WebData cache</Button>-->
<Button Command="{Binding ClearWebDataCacheCommand}">Clear WebData cache</Button>
</StackPanel>
</ScrollViewer>

Expand Down
110 changes: 90 additions & 20 deletions Q42.WinRT/Controls/ImageExtensions.cs
Expand Up @@ -5,9 +5,11 @@
#if NETFX_CORE
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Imaging;
#elif WINDOWS_PHONE
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Controls;
using System.IO;
Expand Down Expand Up @@ -55,23 +57,93 @@ private static async void OnCacheUriChanged(DependencyObject d, DependencyProper

//Uri oldCacheUri = (Uri)e.OldValue;
Uri newCacheUri = (Uri)d.GetValue(CacheUriProperty);
var image = (Image)d;

if (newCacheUri != null)
{
try
//Get image from cache (download and set in cache if needed)
var cacheUri = await WebDataCache.GetLocalUriAsync(newCacheUri);

// Check if the wanted image uri has not changed while we were loading
if (newCacheUri != (Uri)d.GetValue(CacheUriProperty)) return;

if (d is Image)
{
SetSourceOnImageControl((Image)d, cacheUri, newCacheUri);
}
else
{
//Get image from cache (download and set in cache if needed)
var cacheUri = await WebDataCache.GetLocalUriAsync(newCacheUri);
if (d is ImageBrush)
{
SetSourceOnImageBrushControl((ImageBrush)d, cacheUri, newCacheUri);
}
}
}
else
{

// Check if the wanted image uri has not changed while we were loading
if (newCacheUri != (Uri)d.GetValue(CacheUriProperty)) return;
if (d is Image)
{
((Image)d).Source = null;
}
else
{
if (d is ImageBrush)
{
((ImageBrush)d).ImageSource = null;
}
}
}

#if NETFX_CORE
//Set cache uri as source for the image
image.Source = new BitmapImage(cacheUri);

#elif WINDOWS_PHONE

}


private static void SetSourceOnImageBrushControl(ImageBrush imageBrush, Uri cacheUri, Uri newCacheUri)
{
try
{


#if NETFX_CORE
//Set cache uri as source for the image
imageBrush.ImageSource = new BitmapImage(cacheUri);

#elif WINDOWS_PHONE
BitmapImage bimg = new BitmapImage();

using (IsolatedStorageFile iso = IsolatedStorageFile.GetUserStoreForApplication())
{
using (IsolatedStorageFileStream stream = iso.OpenFile(cacheUri.PathAndQuery, FileMode.Open, FileAccess.Read))
{
bimg.SetSource(stream);
}
}
//Set cache uri as source for the image
imageBrush.ImageSource = bimg;
#endif
}
catch (Exception ex)
{
Debug.WriteLine(ex);

//Revert to using passed URI
imageBrush.ImageSource = new BitmapImage(newCacheUri);
}
}


private static void SetSourceOnImageControl(Image image, Uri cacheUri, Uri newCacheUri)
{
try
{


#if NETFX_CORE
//Set cache uri as source for the image
image.Source = new BitmapImage(cacheUri);

#elif WINDOWS_PHONE
BitmapImage bimg = new BitmapImage();

using (IsolatedStorageFile iso = IsolatedStorageFile.GetUserStoreForApplication())
Expand All @@ -83,21 +155,19 @@ private static async void OnCacheUriChanged(DependencyObject d, DependencyProper
}
//Set cache uri as source for the image
image.Source = bimg;
#endif
}
catch (Exception ex)
{
Debug.WriteLine(ex);
#endif
}
catch (Exception ex)
{
Debug.WriteLine(ex);

//Revert to using passed URI
image.Source = new BitmapImage(newCacheUri);
}
//Revert to using passed URI
image.Source = new BitmapImage(newCacheUri);
}
else
image.Source = null;

}

}


}

0 comments on commit 7bb7b00

Please sign in to comment.