2017-08-04 21:38:58 +02:00
|
|
|
|
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
|
2020-01-09 19:40:10 +01:00
|
|
|
|
// © 2020 Clemens Fischer
|
2017-08-04 21:38:58 +02:00
|
|
|
|
// Licensed under the Microsoft Public License (Ms-PL)
|
|
|
|
|
|
|
|
|
|
|
|
using System;
|
2019-07-13 00:08:56 +02:00
|
|
|
|
using System.Runtime.InteropServices.WindowsRuntime;
|
2017-08-04 21:38:58 +02:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
using Windows.Storage;
|
|
|
|
|
|
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
|
|
|
|
|
2020-10-21 22:18:16 +02:00
|
|
|
|
private static async Task LoadCachedTileAsync(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);
|
2019-07-13 19:53:03 +02:00
|
|
|
|
var buffer = cacheItem?.Buffer;
|
2017-08-04 21:38:58 +02:00
|
|
|
|
|
2019-07-22 19:47:57 +02:00
|
|
|
|
if (cacheItem == null || cacheItem.Expiration < DateTime.UtcNow)
|
2017-08-04 21:38:58 +02:00
|
|
|
|
{
|
2020-09-25 15:02:41 +02:00
|
|
|
|
var response = await ImageLoader.GetHttpResponseAsync(uri).ConfigureAwait(false);
|
2019-07-13 08:49:49 +02:00
|
|
|
|
|
|
|
|
|
|
if (response != null) // download succeeded
|
2017-10-27 17:15:18 +02:00
|
|
|
|
{
|
2019-07-22 19:47:57 +02:00
|
|
|
|
buffer = response.Buffer?.AsBuffer(); // may be null or empty when no tile available, but still be cached
|
2019-07-13 00:08:56 +02:00
|
|
|
|
|
2019-07-22 19:47:57 +02:00
|
|
|
|
await Cache.SetAsync(cacheKey, buffer, GetExpiration(response.MaxAge)).ConfigureAwait(false);
|
2017-08-04 21:38:58 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2017-10-27 17:15:18 +02:00
|
|
|
|
|
2019-07-22 19:47:57 +02:00
|
|
|
|
if (buffer != null && buffer.Length > 0)
|
2017-08-04 21:38:58 +02:00
|
|
|
|
{
|
2019-07-13 19:53:03 +02:00
|
|
|
|
await SetTileImageAsync(tile, () => ImageLoader.LoadImageAsync(buffer)).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
|
|
|
|
|
2020-10-21 22:18:16 +02:00
|
|
|
|
private static Task LoadTileAsync(Tile tile, TileSource tileSource)
|
2019-06-13 21:38:01 +02:00
|
|
|
|
{
|
|
|
|
|
|
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
|
|
|
|
|
2020-10-21 22:18:16 +02:00
|
|
|
|
private static async Task SetTileImageAsync(Tile tile, Func<Task<ImageSource>> loadImage)
|
2018-08-21 23:56:54 +02:00
|
|
|
|
{
|
|
|
|
|
|
var tcs = new TaskCompletionSource<object>();
|
|
|
|
|
|
|
|
|
|
|
|
await tile.Image.Dispatcher.RunAsync(CoreDispatcherPriority.Low, async () =>
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
2020-10-21 22:18:16 +02:00
|
|
|
|
tile.SetImage(await loadImage());
|
2018-08-21 23:56:54 +02:00
|
|
|
|
tcs.SetResult(null);
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
tcs.SetException(ex);
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2020-09-25 15:02:41 +02:00
|
|
|
|
await tcs.Task.ConfigureAwait(false); // wait until image loading in the UI thread is completed
|
2018-08-21 23:56:54 +02:00
|
|
|
|
}
|
2017-08-04 21:38:58 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|