XAML-Map-Control/MapControl/UWP/TileImageLoader.UWP.cs

58 lines
2 KiB
C#
Raw Normal View History

2017-08-04 21:38:58 +02:00
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
2021-01-13 21:19:27 +01:00
// © 2021 Clemens Fischer
2017-08-04 21:38:58 +02:00
// Licensed under the Microsoft Public License (Ms-PL)
using System;
using System.Runtime.InteropServices.WindowsRuntime;
2017-08-04 21:38:58 +02:00
using System.Threading.Tasks;
using Windows.Storage;
namespace MapControl
{
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; }
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
{
var cacheItem = await Cache.GetAsync(cacheKey).ConfigureAwait(false);
var buffer = cacheItem?.Buffer;
2017-08-04 21:38:58 +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);
if (response != null) // download succeeded
{
buffer = response.Buffer?.AsBuffer(); // may be null or empty when no tile available, but still be cached
await Cache.SetAsync(cacheKey, buffer, GetExpiration(response.MaxAge)).ConfigureAwait(false);
2017-08-04 21:38:58 +02:00
}
}
if (buffer != null && buffer.Length > 0)
2017-08-04 21:38:58 +02:00
{
2021-06-27 21:50:13 +02:00
await tile.SetImageAsync(() => ImageLoader.LoadImageAsync(buffer)).ConfigureAwait(false);
2017-08-04 21:38:58 +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)
{
2021-06-27 21:50:13 +02:00
return tile.SetImageAsync(() => tileSource.LoadImageAsync(tile.XIndex, tile.Y, tile.ZoomLevel));
}
2017-08-04 21:38:58 +02:00
}
}