moved connection state to status bar

This commit is contained in:
2026-04-29 04:05:16 +02:00
parent b6a4903847
commit 06d5113711
6 changed files with 48 additions and 39 deletions
+1 -1
View File
@@ -15,7 +15,7 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.4.0" />
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.4.2" />
</ItemGroup>
<ItemGroup>
+5 -21
View File
@@ -7,7 +7,7 @@
xmlns:vm="clr-namespace:ARAMUtility.ViewModel"
mc:Ignorable="d"
Title="{Binding Title}"
Height="480"
Height="500"
Width="700"
Background="#0a1a2a"
Topmost="True"
@@ -67,27 +67,11 @@
<Separator />
<MenuItem Header="Quit" Command="{Binding QuitCommand}" />
</MenuItem>
<MenuItem Header="{Binding ConnectionStatus}" Command="{Binding ConnectCommand}" />
<MenuItem Header="Update ARAM">
<StackPanel Orientation="Vertical">
<ComboBox ItemsSource="{Binding .}"
SelectedIndex="0"
SelectedItem="{Binding .}"
/>
<ItemsControl ItemsSource="{Binding .}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Label Content="{Binding .}" />
<TextBox Text="{Binding .}" />
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
<Button Content="Save" />
</StackPanel>
</MenuItem>
<MenuItem Header="Reconnect" Command="{Binding ConnectCommand}" Visibility="{Binding ReconnectButtonVisibility, Mode=OneWay}" />
</Menu>
<StatusBar DockPanel.Dock="Bottom">
<StatusBarItem Content="{Binding Status}" />
</StatusBar>
<Viewbox Stretch="Uniform" StretchDirection="DownOnly">
<Grid Margin="10">
<Grid.RowDefinitions>
+4
View File
@@ -11,6 +11,10 @@ public partial class MainWindow : Window
{
InitializeComponent();
#if DEBUG
Topmost = false;
#endif
if (DataContext is MainViewModel viewModel)
{
Loaded += viewModel.OnInit;
+32 -13
View File
@@ -18,6 +18,13 @@ public partial class MainViewModel : ObservableObject, IDisposable
private const int TEAM_CHAMPIONS_MAX = 5;
private const int BENCH_CHAMPIONS_MAX = 10;
public enum ConnectionStatus
{
Disconnected,
Connecting,
Connected,
}
private bool _isDisposed;
private readonly Lock _syncRoot = new();
private readonly SemaphoreSlim _championUpdateSemaphore = new(1, 1);
@@ -36,12 +43,13 @@ public partial class MainViewModel : ObservableObject, IDisposable
[ObservableProperty]
public partial bool IsDisconnected { get; private set; } = false;
[ObservableProperty]
public partial string ConnectionStatus { get; private set; } = "Not connected.";
[ObservableProperty, NotifyPropertyChangedFor(nameof(ReconnectButtonVisibility))]
public partial ConnectionStatus Status { get; private set; } = ConnectionStatus.Disconnected;
public Visibility ReconnectButtonVisibility => Status == ConnectionStatus.Disconnected ? Visibility.Visible : Visibility.Collapsed;
[ObservableProperty]
public partial bool AutoAccept { get; set; } = true;
private Dictionary<int, ChampionData> _allChampions = [];
private List<int> _needChampionIds = [];
private readonly APIClient _client = new();
@@ -51,9 +59,9 @@ public partial class MainViewModel : ObservableObject, IDisposable
public MainViewModel()
{
_lcuWebsocket.Connecting += (_, _) => UpdateConnectionStatus(false, "Connecting ...");
_lcuWebsocket.Connected += (_, _) => UpdateConnectionStatus(false, "Connected");
_lcuWebsocket.Disconnected += (_, _) => UpdateConnectionStatus(true, "Reconnect?");
_lcuWebsocket.Connecting += (_, _) => UpdateConnectionStatus(false, ConnectionStatus.Connecting);
_lcuWebsocket.Connected += (_, _) => UpdateConnectionStatus(false, ConnectionStatus.Connected);
_lcuWebsocket.Disconnected += (_, _) => UpdateConnectionStatus(true, ConnectionStatus.Disconnected);
_lcuWebsocket.LcuApiEvent += OnLcuApiEvent;
#if DEBUG
@@ -75,16 +83,27 @@ public partial class MainViewModel : ObservableObject, IDisposable
{
_allChampions = championDictionary;
}
await UpdateNeedChampionIdsAsync();
await FillChampionLists();
try
{
await UpdateNeedChampionIdsAsync();
}
catch (InvalidOperationException)
{
UpdateConnectionStatus(false, ConnectionStatus.Disconnected);
return;
}
finally
{
await FillChampionLists();
}
}
private void UpdateConnectionStatus(bool isConnected, string statusMessage)
private void UpdateConnectionStatus(bool isConnected, ConnectionStatus status)
{
Dispatcher.Invoke(() =>
{
IsDisconnected = isConnected;
ConnectionStatus = statusMessage;
Status = status;
});
}
@@ -191,7 +210,7 @@ public partial class MainViewModel : ObservableObject, IDisposable
}
}
[RelayCommand(AllowConcurrentExecutions = false, CanExecute = nameof(IsDisconnected))]
[RelayCommand(AllowConcurrentExecutions = false)]
private async Task Connect()
{
if (_lcuWebsocketTask is null || _lcuWebsocketTask.IsCompleted)
@@ -219,7 +238,7 @@ public partial class MainViewModel : ObservableObject, IDisposable
await _aramBalanceService.ReloadAsync(force: true);
MessageBox.Show("Reloaded ARAM balance data.", "Info", MessageBoxButton.OK, MessageBoxImage.Exclamation);
}
[RelayCommand]
private async Task Quit()
{
+5 -2
View File
@@ -51,10 +51,13 @@ public class LcuWebsocket : IDisposable
}
catch
{
await Task.Delay(TimeSpan.FromMilliseconds(500));
Disconnected?.Invoke(this, EventArgs.Empty);
return;
}
if (!ProcessFinder.IsPortOpen(ProcessInfo))
{
Disconnected?.Invoke(this, EventArgs.Empty);
throw new InvalidOperationException("Failed to connect to LCUx process port.");
}
@@ -67,8 +70,6 @@ public class LcuWebsocket : IDisposable
{
await _socket.ConnectAsync(uri, CancellationToken.None);
Connected?.Invoke(this, EventArgs.Empty);
foreach (string eventName in SUBSCRIBE_EVENTS)
{
string message = $"[{OPCODE_SUBSCRIBE}, \"{eventName}\"]";
@@ -77,6 +78,8 @@ public class LcuWebsocket : IDisposable
await _socket.SendAsync(memory, WebSocketMessageType.Text, true, CancellationToken.None);
}
Connected?.Invoke(this, EventArgs.Empty);
while (_socket.State is WebSocketState.Open)
{
try
+1 -2
View File
@@ -7,8 +7,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="CliWrap" Version="3.10.0" />
<PackageReference Include="MoonSharp" Version="2.0.0" />
<PackageReference Include="CliWrap" Version="3.10.1" />
</ItemGroup>
</Project>