-Komplette view überarbeitet für Thunderstore mit mods suche, vorschau, markdown (readme) etc
todo: wenn neuer mod aufgerufen wird, soll die zurückfunktion deaktviert sein oder so das sie den mod mit aktualisiert zu dem der davor ausgewählt war (:
This commit is contained in:
56
Valheim Dashboard/Converters/ImageUrlToBitmapConverter.cs
Normal file
56
Valheim Dashboard/Converters/ImageUrlToBitmapConverter.cs
Normal file
@@ -0,0 +1,56 @@
|
||||
using Avalonia.Data.Converters;
|
||||
using Avalonia.Media.Imaging;
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
using System.Net.Http;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Valheim_Dashboard.Converters
|
||||
{
|
||||
public class ImageUrlToBitmapConverter : IValueConverter
|
||||
{
|
||||
private static readonly HttpClient _http = new HttpClient();
|
||||
private static readonly ConcurrentDictionary<string, Bitmap?> _cache = new();
|
||||
|
||||
public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
|
||||
{
|
||||
if (value is not string url || string.IsNullOrWhiteSpace(url))
|
||||
return null;
|
||||
|
||||
// Schon im Cache?
|
||||
if (_cache.TryGetValue(url, out var cached))
|
||||
return cached;
|
||||
|
||||
// async im Hintergrund laden
|
||||
_ = Task.Run(async () =>
|
||||
{
|
||||
try
|
||||
{
|
||||
var data = await _http.GetByteArrayAsync(url);
|
||||
using var ms = new MemoryStream(data);
|
||||
var bmp = new Bitmap(ms);
|
||||
|
||||
_cache[url] = bmp;
|
||||
|
||||
Avalonia.Threading.Dispatcher.UIThread.Post(() =>
|
||||
{
|
||||
// Binding refreshen (z.B. PropertyChanged für Mods triggern)
|
||||
});
|
||||
}
|
||||
catch
|
||||
{
|
||||
_cache[url] = null;
|
||||
}
|
||||
});
|
||||
|
||||
// solange Platzhalter
|
||||
return null;
|
||||
}
|
||||
|
||||
public object ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
|
||||
=> throw new NotImplementedException();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user