LANCommander/LANCommander.Launcher.Avalonia/ViewModels/ServerSelectionViewModel.cs
2026-04-07 23:12:41 -05:00

76 lines
2.3 KiB
C#

using System;
using System.Threading.Tasks;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using LANCommander.SDK.Providers;
using LANCommander.SDK.Services;
namespace LANCommander.Launcher.Avalonia.ViewModels;
public partial class ServerSelectionViewModel : ViewModelBase
{
private readonly IConnectionClient _connectionClient;
private readonly SettingsProvider<Settings.Settings> _settingsProvider;
[ObservableProperty]
private string _serverAddress = string.Empty;
[ObservableProperty]
private string _statusMessage = string.Empty;
[ObservableProperty]
private bool _isLoading;
[ObservableProperty]
private bool _hasError;
public event EventHandler? ServerConnected;
public ServerSelectionViewModel(
IConnectionClient connectionClient,
SettingsProvider<Settings.Settings> settingsProvider)
{
_connectionClient = connectionClient;
_settingsProvider = settingsProvider;
// Load saved server address if available
if (_settingsProvider.CurrentValue.Authentication?.ServerAddress != null)
ServerAddress = _settingsProvider.CurrentValue.Authentication.ServerAddress.ToString();
}
[RelayCommand]
private async Task ConnectAsync()
{
if (string.IsNullOrWhiteSpace(ServerAddress))
{
StatusMessage = "Please enter a server address";
HasError = true;
return;
}
IsLoading = true;
HasError = false;
StatusMessage = "Testing connection...";
try
{
// UpdateServerAddressAsync discovers and validates the server internally
// (pings candidate URIs). If it returns without throwing, the server is reachable.
await _connectionClient.UpdateServerAddressAsync(ServerAddress);
ServerAddress = _connectionClient.GetServerAddress().ToString();
StatusMessage = "Connected!";
ServerConnected?.Invoke(this, EventArgs.Empty);
}
catch (Exception ex)
{
StatusMessage = $"Connection failed: {ex.Message}";
HasError = true;
}
finally
{
IsLoading = false;
}
}
}