LANCommander/LANCommander.Launcher.Avalonia/ViewModels/LoginViewModel.cs
Aaron Powell 8cf2139ccf feat: Add Avalonia launcher PoC with cross-platform UI
- Create LANCommander.Launcher.Avalonia project using Avalonia MVVM
- Implement server selection, login, and game library views
- Add library sidebar showing user's games
- Add depot/games list view with game details
- Integrate with LANCommander.Launcher.Services for:
  - DepotService, LibraryService, ImportService, MediaService
  - SDK authentication and connection clients
- Fix async initialization pattern to prevent UI thread deadlocks
- Add ConfigureAwait(false) to ServerConfigurationProvider.RefreshAsync
- Add Microsoft.Extensions.Http package for HttpClient DI

Key features:
- Server discovery and connection
- User authentication with credential persistence
- Library import from server to local SQLite cache
- Game icons and banners via MediaService
- Navigation between library, depot, and game details
2026-01-19 14:24:49 +11:00

95 lines
2.7 KiB
C#

using System;
using System.Threading.Tasks;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using LANCommander.Launcher.Services;
using LANCommander.SDK.Providers;
using LANCommander.SDK.Services;
namespace LANCommander.Launcher.Avalonia.ViewModels;
public partial class LoginViewModel : ViewModelBase
{
private readonly IConnectionClient _connectionClient;
private readonly AuthenticationService _authenticationService;
private readonly SettingsProvider<Settings.Settings> _settingsProvider;
[ObservableProperty]
private string _username = string.Empty;
[ObservableProperty]
private string _password = string.Empty;
[ObservableProperty]
private string _statusMessage = string.Empty;
[ObservableProperty]
private bool _isLoading;
[ObservableProperty]
private bool _hasError;
[ObservableProperty]
private string _serverAddress = string.Empty;
public event EventHandler? LoginSucceeded;
public event EventHandler? ChangeServerRequested;
public LoginViewModel(
IConnectionClient connectionClient,
AuthenticationService authenticationService,
SettingsProvider<Settings.Settings> settingsProvider)
{
_connectionClient = connectionClient;
_authenticationService = authenticationService;
_settingsProvider = settingsProvider;
ServerAddress = _connectionClient.GetServerAddress()?.ToString() ?? "Not connected";
}
[RelayCommand]
private async Task LoginAsync()
{
if (string.IsNullOrWhiteSpace(Username) || string.IsNullOrWhiteSpace(Password))
{
StatusMessage = "Please enter username and password";
HasError = true;
return;
}
IsLoading = true;
HasError = false;
StatusMessage = "Logging in...";
try
{
var serverAddress = _connectionClient.GetServerAddress();
if (serverAddress == null)
{
StatusMessage = "No server configured";
HasError = true;
return;
}
await _authenticationService.Login(serverAddress, Username, Password);
StatusMessage = "Login successful!";
LoginSucceeded?.Invoke(this, EventArgs.Empty);
}
catch (Exception ex)
{
StatusMessage = $"Login failed: {ex.Message}";
HasError = true;
}
finally
{
IsLoading = false;
}
}
[RelayCommand]
private void ChangeServer()
{
ChangeServerRequested?.Invoke(this, EventArgs.Empty);
}
}