2017-08-04 21:38:58 +02:00
|
|
|
|
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
|
2019-03-27 18:39:59 +01:00
|
|
|
|
// © 2019 Clemens Fischer
|
2017-08-04 21:38:58 +02:00
|
|
|
|
// Licensed under the Microsoft Public License (Ms-PL)
|
|
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
using Windows.Storage;
|
|
|
|
|
|
using Windows.Storage.Streams;
|
|
|
|
|
|
using Windows.UI.Core;
|
2019-06-13 21:38:01 +02:00
|
|
|
|
using Windows.UI.Xaml.Media;
|
2017-08-04 21:38:58 +02:00
|
|
|
|
|
|
|
|
|
|
namespace MapControl
|
|
|
|
|
|
{
|
2018-08-08 23:31:52 +02:00
|
|
|
|
public partial class TileImageLoader
|
2017-08-04 21:38:58 +02:00
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
2017-09-06 20:43:46 +02:00
|
|
|
|
/// Default StorageFolder where an IImageCache instance may save cached data,
|
|
|
|
|
|
/// i.e. ApplicationData.Current.TemporaryFolder.
|
2017-08-04 21:38:58 +02:00
|
|
|
|
/// </summary>
|
2017-09-06 20:43:46 +02:00
|
|
|
|
public static StorageFolder DefaultCacheFolder
|
|
|
|
|
|
{
|
|
|
|
|
|
get { return ApplicationData.Current.TemporaryFolder; }
|
|
|
|
|
|
}
|
2017-08-04 21:38:58 +02:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// The IImageCache implementation used to cache tile images. The default is null.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public static Caching.IImageCache Cache { get; set; }
|
|
|
|
|
|
|
2019-06-15 01:39:07 +02:00
|
|
|
|
|
2019-06-10 19:30:47 +02:00
|
|
|
|
private async Task LoadCachedTileImageAsync(Tile tile, Uri uri, string cacheKey)
|
2017-08-04 21:38:58 +02:00
|
|
|
|
{
|
2019-06-13 21:38:01 +02:00
|
|
|
|
var cacheItem = await Cache.GetAsync(cacheKey).ConfigureAwait(false);
|
2017-10-27 17:15:18 +02:00
|
|
|
|
var cacheBuffer = cacheItem?.Buffer;
|
2017-08-04 21:38:58 +02:00
|
|
|
|
|
2017-10-27 17:15:18 +02:00
|
|
|
|
if (cacheBuffer == null || cacheItem.Expiration < DateTime.UtcNow)
|
2017-08-04 21:38:58 +02:00
|
|
|
|
{
|
2019-06-13 21:38:01 +02:00
|
|
|
|
var response = await ImageLoader.LoadHttpBufferAsync(uri).ConfigureAwait(false);
|
2018-08-09 18:21:16 +02:00
|
|
|
|
|
2019-06-13 21:38:01 +02:00
|
|
|
|
if (response != null) // download succeeded
|
2017-10-27 17:15:18 +02:00
|
|
|
|
{
|
2018-08-09 18:21:16 +02:00
|
|
|
|
cacheBuffer = null; // discard cached image
|
|
|
|
|
|
|
2019-06-13 21:38:01 +02:00
|
|
|
|
if (response.Buffer != null) // tile image available
|
2018-08-09 18:21:16 +02:00
|
|
|
|
{
|
2019-06-13 21:38:01 +02:00
|
|
|
|
await LoadTileImageAsync(tile, response.Buffer).ConfigureAwait(false);
|
|
|
|
|
|
await Cache.SetAsync(cacheKey, response.Buffer, GetExpiration(response.MaxAge)).ConfigureAwait(false);
|
2018-08-09 18:21:16 +02:00
|
|
|
|
}
|
2017-08-04 21:38:58 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2017-10-27 17:15:18 +02:00
|
|
|
|
|
2019-06-08 23:18:33 +02:00
|
|
|
|
if (cacheBuffer != null) // cached image not expired or download failed
|
2017-08-04 21:38:58 +02:00
|
|
|
|
{
|
2019-06-13 21:38:01 +02:00
|
|
|
|
await LoadTileImageAsync(tile, cacheBuffer).ConfigureAwait(false);
|
2017-08-04 21:38:58 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-08-21 23:56:54 +02:00
|
|
|
|
private async Task LoadTileImageAsync(Tile tile, IBuffer buffer)
|
2017-08-04 21:38:58 +02:00
|
|
|
|
{
|
|
|
|
|
|
using (var stream = new InMemoryRandomAccessStream())
|
|
|
|
|
|
{
|
|
|
|
|
|
await stream.WriteAsync(buffer);
|
|
|
|
|
|
stream.Seek(0);
|
|
|
|
|
|
|
2019-06-13 21:38:01 +02:00
|
|
|
|
await SetTileImageAsync(tile, () => ImageLoader.LoadImageAsync(stream)).ConfigureAwait(false);
|
2017-08-04 21:38:58 +02:00
|
|
|
|
}
|
2019-06-13 21:38:01 +02:00
|
|
|
|
}
|
2017-08-04 21:38:58 +02:00
|
|
|
|
|
2019-06-13 21:38:01 +02:00
|
|
|
|
private Task LoadTileImageAsync(Tile tile, TileSource tileSource)
|
|
|
|
|
|
{
|
|
|
|
|
|
return SetTileImageAsync(tile, () => tileSource.LoadImageAsync(tile.XIndex, tile.Y, tile.ZoomLevel));
|
2017-08-04 21:38:58 +02:00
|
|
|
|
}
|
2018-08-21 23:56:54 +02:00
|
|
|
|
|
2019-06-13 21:38:01 +02:00
|
|
|
|
private async Task SetTileImageAsync(Tile tile, Func<Task<ImageSource>> loadImageFunc)
|
2018-08-21 23:56:54 +02:00
|
|
|
|
{
|
|
|
|
|
|
var tcs = new TaskCompletionSource<object>();
|
|
|
|
|
|
|
|
|
|
|
|
await tile.Image.Dispatcher.RunAsync(CoreDispatcherPriority.Low, async () =>
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
2019-06-15 01:39:07 +02:00
|
|
|
|
var image = await loadImageFunc();
|
|
|
|
|
|
|
|
|
|
|
|
if (image != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
tile.SetImage(image);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-08-21 23:56:54 +02:00
|
|
|
|
tcs.SetResult(null);
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
tcs.SetException(ex);
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2019-06-13 21:38:01 +02:00
|
|
|
|
await tcs.Task.ConfigureAwait(false);
|
2018-08-21 23:56:54 +02:00
|
|
|
|
}
|
2017-08-04 21:38:58 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|