2021-07-02 21:18:39 +02:00
|
|
|
|
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
|
2023-01-03 15:12:53 +01:00
|
|
|
|
// Copyright © 2023 Clemens Fischer
|
2021-07-02 21:18:39 +02:00
|
|
|
|
// Licensed under the Microsoft Public License (Ms-PL)
|
|
|
|
|
|
|
2021-07-05 00:03:44 +02:00
|
|
|
|
using Microsoft.UI.Dispatching;
|
|
|
|
|
|
using Microsoft.UI.Xaml.Media;
|
2021-07-02 21:18:39 +02:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.IO;
|
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
|
|
namespace MapControl
|
|
|
|
|
|
{
|
|
|
|
|
|
namespace Caching
|
|
|
|
|
|
{
|
|
|
|
|
|
public interface IImageCache
|
|
|
|
|
|
{
|
|
|
|
|
|
Task<Tuple<byte[], DateTime>> GetAsync(string key);
|
|
|
|
|
|
|
|
|
|
|
|
Task SetAsync(string key, byte[] buffer, DateTime expiration);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public partial class TileImageLoader
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Default folder path where an IImageCache instance may save cached data, i.e. C:\ProgramData\MapControl\TileCache
|
|
|
|
|
|
/// </summary>
|
2022-08-06 10:40:59 +02:00
|
|
|
|
public static string DefaultCacheFolder =>
|
|
|
|
|
|
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "MapControl", "TileCache");
|
2021-07-02 21:18:39 +02:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2021-07-05 00:03:44 +02:00
|
|
|
|
/// An IImageCache implementation used to cache tile images.
|
2021-07-02 21:18:39 +02:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
public static Caching.IImageCache Cache { get; set; }
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-11-22 23:07:07 +01:00
|
|
|
|
private static async Task LoadCachedTile(Tile tile, Uri uri, string cacheKey)
|
2021-07-02 21:18:39 +02:00
|
|
|
|
{
|
|
|
|
|
|
var cacheItem = await Cache.GetAsync(cacheKey).ConfigureAwait(false);
|
|
|
|
|
|
var buffer = cacheItem?.Item1;
|
|
|
|
|
|
|
|
|
|
|
|
if (cacheItem == null || cacheItem.Item2 < DateTime.UtcNow)
|
|
|
|
|
|
{
|
|
|
|
|
|
var response = await ImageLoader.GetHttpResponseAsync(uri).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
|
|
if (response != null) // download succeeded
|
|
|
|
|
|
{
|
|
|
|
|
|
buffer = response.Buffer; // may be null or empty when no tile available, but still be cached
|
|
|
|
|
|
|
|
|
|
|
|
await Cache.SetAsync(cacheKey, buffer, GetExpiration(response.MaxAge)).ConfigureAwait(false);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2021-07-06 18:49:48 +02:00
|
|
|
|
//else System.Diagnostics.Debug.WriteLine($"Cached: {cacheKey}");
|
2021-07-02 21:18:39 +02:00
|
|
|
|
|
|
|
|
|
|
if (buffer != null && buffer.Length > 0)
|
|
|
|
|
|
{
|
2022-11-20 18:51:59 +01:00
|
|
|
|
await LoadTile(tile, () => ImageLoader.LoadImageAsync(buffer)).ConfigureAwait(false);
|
2021-07-02 21:18:39 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-11-20 18:51:59 +01:00
|
|
|
|
private static Task LoadTile(Tile tile, Func<Task<ImageSource>> loadImageFunc)
|
2021-07-02 21:18:39 +02:00
|
|
|
|
{
|
|
|
|
|
|
var tcs = new TaskCompletionSource();
|
|
|
|
|
|
|
2021-07-05 00:03:44 +02:00
|
|
|
|
async void callback()
|
2021-07-02 21:18:39 +02:00
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
2022-11-17 23:11:40 +01:00
|
|
|
|
tile.SetImageSource(await loadImageFunc());
|
2021-07-02 21:18:39 +02:00
|
|
|
|
tcs.TrySetResult();
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
tcs.TrySetException(ex);
|
|
|
|
|
|
}
|
2021-07-05 00:03:44 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-08-12 17:36:37 +02:00
|
|
|
|
tile.Image.DispatcherQueue.TryEnqueue(DispatcherQueuePriority.Low, callback);
|
2021-07-02 21:18:39 +02:00
|
|
|
|
|
|
|
|
|
|
return tcs.Task;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|