2017-08-04 21:38:58 +02:00
|
|
|
|
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
|
2022-01-14 20:22:56 +01:00
|
|
|
|
// © 2022 Clemens Fischer
|
2017-08-04 21:38:58 +02:00
|
|
|
|
// Licensed under the Microsoft Public License (Ms-PL)
|
|
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
|
using System.Threading.Tasks;
|
2021-06-28 23:35:03 +02:00
|
|
|
|
using Windows.UI.Core;
|
|
|
|
|
|
using Windows.UI.Xaml.Media;
|
2017-08-04 21:38:58 +02:00
|
|
|
|
|
|
|
|
|
|
namespace MapControl
|
|
|
|
|
|
{
|
2021-07-02 11:35:20 +02:00
|
|
|
|
namespace Caching
|
|
|
|
|
|
{
|
|
|
|
|
|
public interface IImageCache
|
|
|
|
|
|
{
|
2021-07-02 15:57:01 +02:00
|
|
|
|
Task<Tuple<byte[], DateTime>> GetAsync(string key);
|
2021-07-02 11:35:20 +02:00
|
|
|
|
|
2021-07-02 15:57:01 +02:00
|
|
|
|
Task SetAsync(string key, byte[] buffer, DateTime expiration);
|
2021-07-02 11:35:20 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-08-08 23:31:52 +02:00
|
|
|
|
public partial class TileImageLoader
|
2017-08-04 21:38:58 +02:00
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
2021-06-30 14:17:54 +02:00
|
|
|
|
/// Default folder path where an IImageCache instance may save cached data,
|
|
|
|
|
|
/// i.e. Windows.Storage.ApplicationData.Current.TemporaryFolder.Path.
|
2017-08-04 21:38:58 +02:00
|
|
|
|
/// </summary>
|
2021-06-30 14:17:54 +02:00
|
|
|
|
public static string DefaultCacheFolder
|
2017-09-06 20:43:46 +02:00
|
|
|
|
{
|
2021-06-30 14:17:54 +02:00
|
|
|
|
get { return Windows.Storage.ApplicationData.Current.TemporaryFolder.Path; }
|
2017-09-06 20:43:46 +02:00
|
|
|
|
}
|
2017-08-04 21:38:58 +02:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2021-07-05 00:03:44 +02:00
|
|
|
|
/// An IImageCache implementation used to cache tile images.
|
2017-08-04 21:38:58 +02:00
|
|
|
|
/// </summary>
|
2021-07-02 15:57:01 +02:00
|
|
|
|
public static Caching.IImageCache Cache { get; set; }
|
2017-08-04 21:38:58 +02:00
|
|
|
|
|
2019-06-15 01:39:07 +02:00
|
|
|
|
|
2021-11-22 23:07:07 +01:00
|
|
|
|
private static async Task LoadCachedTile(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);
|
2021-07-02 15:57:01 +02:00
|
|
|
|
var buffer = cacheItem?.Item1;
|
2017-08-04 21:38:58 +02:00
|
|
|
|
|
2021-07-02 15:57:01 +02:00
|
|
|
|
if (cacheItem == null || cacheItem.Item2 < 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
|
|
|
|
{
|
2021-07-02 11:35:20 +02:00
|
|
|
|
buffer = response.Buffer; // may be null or empty when no tile available, but still be cached
|
|
|
|
|
|
|
2021-07-02 15:57:01 +02:00
|
|
|
|
await Cache.SetAsync(cacheKey, buffer, GetExpiration(response.MaxAge)).ConfigureAwait(false);
|
2017-08-04 21:38:58 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2021-07-06 18:49:48 +02:00
|
|
|
|
//else System.Diagnostics.Debug.WriteLine($"Cached: {cacheKey}");
|
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
|
|
|
|
{
|
2021-11-22 23:07:07 +01:00
|
|
|
|
await SetTileImage(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
|
|
|
|
|
2021-11-22 23:07:07 +01:00
|
|
|
|
private static Task LoadTile(Tile tile, TileSource tileSource)
|
2019-06-13 21:38:01 +02:00
|
|
|
|
{
|
2021-11-22 23:07:07 +01:00
|
|
|
|
return SetTileImage(tile, () => tileSource.LoadImageAsync(tile.XIndex, tile.Y, tile.ZoomLevel));
|
2018-08-21 23:56:54 +02:00
|
|
|
|
}
|
2021-06-28 23:35:03 +02:00
|
|
|
|
|
2021-11-22 23:07:07 +01:00
|
|
|
|
private static async Task SetTileImage(Tile tile, Func<Task<ImageSource>> loadImageFunc)
|
2021-06-28 23:35:03 +02:00
|
|
|
|
{
|
|
|
|
|
|
var tcs = new TaskCompletionSource<object>();
|
|
|
|
|
|
|
2021-07-05 00:03:44 +02:00
|
|
|
|
async void callback()
|
2021-06-28 23:35:03 +02:00
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
tile.SetImage(await loadImageFunc());
|
|
|
|
|
|
tcs.TrySetResult(null);
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
tcs.TrySetException(ex);
|
|
|
|
|
|
}
|
2021-07-05 00:03:44 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
await tile.Image.Dispatcher.RunAsync(CoreDispatcherPriority.Low, callback);
|
2021-06-28 23:35:03 +02:00
|
|
|
|
|
|
|
|
|
|
await tcs.Task.ConfigureAwait(false);
|
|
|
|
|
|
}
|
2017-08-04 21:38:58 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|