diff --git a/ModerationClient/ViewModels/RoomViewModel.cs b/ModerationClient/ViewModels/RoomViewModel.cs
index c18b842..2ce0363 100644
--- a/ModerationClient/ViewModels/RoomViewModel.cs
+++ b/ModerationClient/ViewModels/RoomViewModel.cs
@@ -6,6 +6,8 @@ using System.Collections.Specialized;
using System.ComponentModel;
using System.IO;
using System.Linq;
+using System.Threading;
+using System.Threading.Tasks;
using ArcaneLibs;
using Avalonia;
using Avalonia.Media.Imaging;
@@ -63,50 +65,48 @@ public class RoomMember(AuthenticatedHomeserverGeneric homeserver) : NotifyPrope
get;
set {
field = value;
-
- if(!Directory.Exists(MediaDir))
- Directory.CreateDirectory(MediaDir);
-
- var fsPath = Path.Combine(MediaDir, AvatarUrl.Replace("/", "_"));
- Console.WriteLine($"Avatar path for {UserId}: {fsPath}");
- if (File.Exists(fsPath)) {
- UserAvatarBitmap = new Bitmap(fsPath);
- Console.WriteLine($"Avatar bitmap for {UserId} loaded: {UserAvatarBitmap.GetHashCode()}, Path: {fsPath}");
- return;
- }
-
- homeserver.GetMediaStreamAsync(field).ContinueWith(async streamTask => {
- try {
- await using var stream = await streamTask;
- Console.WriteLine($"Avatar stream for {UserId} received: {stream.GetHashCode()}");
- // await using var ms = new MemoryStream();
- // await stream.CopyToAsync(ms);
- // var fs = new FileStream("avatar.png", FileMode.Create);
- // ms.Seek(0, SeekOrigin.Begin);
- // Console.WriteLine($"Avatar stream for {UserId} copied: {ms.GetHashCode()}");
- // var bm = new Bitmap(ms);
- // Console.WriteLine($"Avatar bitmap for {UserId} loaded: {bm.GetHashCode()}");
- // var sbm = bm.CreateScaledBitmap(new(32, 32));
- // Console.WriteLine($"Avatar bitmap for {UserId} loaded: {sbm.GetHashCode()}");
- // UserAvatarBitmap = sbm;
- // Console.WriteLine($"Bitmap for {UserId} set to {UserAvatarBitmap.GetHashCode()}");
-
- var fs = new FileStream(fsPath, FileMode.Create);
- await stream.CopyToAsync(fs);
- fs.Close();
- UserAvatarBitmap = new Bitmap(fsPath);
- Console.WriteLine($"Avatar bitmap for {UserId} loaded: {UserAvatarBitmap.GetHashCode()}, Path: {fsPath}");
- }
- catch (Exception e) {
- Console.WriteLine($"Failed to load avatar for {UserId}: {e}");
- UserAvatarBitmap = new Bitmap(PixelFormat.Rgba8888, AlphaFormat.Unpremul, 1, PixelSize.Empty, Vector.Zero, 0);
- }
- });
- // UserAvatarBitmap = new Bitmap();
+ _ = UpdateAvatar();
+ }
+ }
+
+ private static readonly SemaphoreSlim MediaFetchLock = new(16, 16);
+ private async Task UpdateAvatar() {
+ if (string.IsNullOrEmpty(AvatarUrl)) {
+ UserAvatarBitmap = new WriteableBitmap(new PixelSize(1, 1), Vector.One);
+ return;
+ }
+
+ if (!Directory.Exists(MediaDir))
+ Directory.CreateDirectory(MediaDir);
+
+ var fsPath = Path.Combine(MediaDir, AvatarUrl.Replace("/", "_"));
+ Console.WriteLine($"Avatar path for {UserId}: {fsPath}");
+ if (File.Exists(fsPath)) {
+ UserAvatarBitmap = new(fsPath);
+ Console.WriteLine($"Avatar bitmap for {UserId} loaded: {UserAvatarBitmap.GetHashCode()}, Path: {fsPath}");
+ return;
+ }
+
+ await MediaFetchLock.WaitAsync();
+ try {
+ var stream = await homeserver.GetMediaStreamAsync(AvatarUrl);
+ Console.WriteLine($"Avatar stream for {UserId} received: {stream.GetHashCode()}");
+ var fs = new FileStream(fsPath, FileMode.Create);
+ await stream.CopyToAsync(fs);
+ fs.Close();
+ UserAvatarBitmap = new Bitmap(fsPath);
+ Console.WriteLine($"Avatar bitmap for {UserId} loaded: {UserAvatarBitmap.GetHashCode()}, Path: {fsPath}");
+ }
+ catch (Exception e) {
+ Console.WriteLine($"Failed to load avatar for {UserId}: {e}");
+ UserAvatarBitmap = new WriteableBitmap(new PixelSize(1, 1), Vector.One);
+ }
+ finally {
+ MediaFetchLock.Release();
}
}
- public Bitmap UserAvatarBitmap {
+ public Bitmap? UserAvatarBitmap {
get;
private set => SetField(ref field, value);
}
|