Workshop - Fixed current user avatar not displaying

Workshop - Improve error handling while uploading new entries
This commit is contained in:
RobertBeekman committed 2024-01-30 13:14:24 +01:00
1 parent c29d30d222
commit 17bd62e673
8 files changed
+51 -70

No files matched your search

@@ -5,6 +5,7 @@
xmlns:currentUser="clr-namespace:Artemis.UI.Screens.Workshop.CurrentUser"
xmlns:avalonia="clr-namespace:Material.Icons.Avalonia;assembly=Material.Icons.Avalonia"
xmlns:controls="clr-namespace:FluentAvalonia.UI.Controls;assembly=FluentAvalonia"
xmlns:asyncImageLoader="clr-namespace:AsyncImageLoader;assembly=AsyncImageLoader.Avalonia"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="Artemis.UI.Screens.Workshop.CurrentUser.CurrentUserView"
x:DataType="currentUser:CurrentUserViewModel">
@@ -27,13 +28,16 @@
</Ellipse>
<!-- Signed in -->
<Ellipse Height="{CompiledBinding Bounds.Height, ElementName=Container}" Width="{CompiledBinding Bounds.Height, ElementName=Container}" IsVisible="{CompiledBinding !IsAnonymous}" Name="UserMenu">
<Ellipse Height="{CompiledBinding Bounds.Height, ElementName=Container}"
Width="{CompiledBinding Bounds.Height, ElementName=Container}"
IsVisible="{CompiledBinding !IsAnonymous}"
Name="UserMenu">
<Ellipse.ContextFlyout>
<Flyout>
<Grid ColumnDefinitions="Auto,*" RowDefinitions="*,*,*" MinWidth="300">
<Ellipse Grid.Column="0" Grid.RowSpan="3" Height="50" Width="50" Margin="0 0 8 0" VerticalAlignment="Top">
<Ellipse.Fill>
<ImageBrush Source="{CompiledBinding Avatar}" />
<ImageBrush asyncImageLoader:ImageBrushLoader.Source="{CompiledBinding AvatarUrl}" />
</Ellipse.Fill>
</Ellipse>
<TextBlock Grid.Column="1" Grid.Row="0" Text="{CompiledBinding Name}" Margin="0 4 0 0"></TextBlock>
@@ -51,7 +55,7 @@
</Flyout>
</Ellipse.ContextFlyout>
<Ellipse.Fill>
<ImageBrush Source="{CompiledBinding Avatar}" />
<ImageBrush asyncImageLoader:ImageBrushLoader.Source="{CompiledBinding AvatarUrl}" />
</Ellipse.Fill>
</Ellipse>
</Panel>
@@ -21,7 +21,6 @@ public partial class CurrentUserViewModel : ActivatableViewModelBase
{
private readonly IAuthenticationService _authenticationService;
private readonly ObservableAsPropertyHelper<bool> _isAnonymous;
private readonly HttpClient _httpClient;
private readonly ILogger _logger;
private readonly IWindowService _windowService;
[Notify] private bool _allowLogout = true;
@@ -30,13 +29,13 @@ public partial class CurrentUserViewModel : ActivatableViewModelBase
[Notify(Setter.Private)] private bool _loading = true;
[Notify(Setter.Private)] private string? _name;
[Notify(Setter.Private)] private string? _userId;
[Notify(Setter.Private)] private string? _avatarUrl;
public CurrentUserViewModel(ILogger logger, IAuthenticationService authenticationService, IWindowService windowService)
{
_logger = logger;
_authenticationService = authenticationService;
_windowService = windowService;
_httpClient = new HttpClient();
Login = ReactiveCommand.CreateFromTask(ExecuteLogin);
_isAnonymous = this.WhenAnyValue(vm => vm.Loading, vm => vm.Name, (l, n) => l || n == null).ToProperty(this, vm => vm.IsAnonymous);
@@ -44,7 +43,7 @@ public partial class CurrentUserViewModel : ActivatableViewModelBase
this.WhenActivated(d =>
{
Task.Run(AutoLogin);
_authenticationService.IsLoggedIn.Subscribe(_ => Task.Run(LoadCurrentUser)).DisposeWith(d);
_authenticationService.IsLoggedIn.Subscribe(_ => LoadCurrentUser()).DisposeWith(d);
});
}
@@ -66,7 +65,7 @@ public partial class CurrentUserViewModel : ActivatableViewModelBase
.ShowAsync();
if (result == ContentDialogResult.Primary)
await LoadCurrentUser();
LoadCurrentUser();
}
private async Task AutoLogin()
@@ -74,7 +73,7 @@ public partial class CurrentUserViewModel : ActivatableViewModelBase
try
{
await _authenticationService.AutoLogin();
await LoadCurrentUser();
LoadCurrentUser();
}
catch (Exception e)
{
@@ -86,33 +85,11 @@ public partial class CurrentUserViewModel : ActivatableViewModelBase
}
}
private async Task LoadCurrentUser()
private void LoadCurrentUser()
{
UserId = _authenticationService.Claims.FirstOrDefault(c => c.Type == "sub")?.Value;
Name = _authenticationService.Claims.FirstOrDefault(c => c.Type == "name")?.Value;
Email = _authenticationService.Claims.FirstOrDefault(c => c.Type == "email")?.Value;
if (UserId != null)
{
await LoadAvatar(UserId);
}
else
{
Avatar?.Dispose();
Avatar = null;
}
}
private async Task LoadAvatar(string userId)
{
try
{
Avatar?.Dispose();
Avatar = new Bitmap(await _httpClient.GetStreamAsync($"{WorkshopConstants.AUTHORITY_URL}/user/avatar/{userId}"));
}
catch (Exception)
{
// ignored
}
AvatarUrl = $"{WorkshopConstants.AUTHORITY_URL}/user/avatar/{UserId}";
}
}