Skip to content

Commit

Permalink
Merge pull request #285 from stevejackson80/WIP/Disable-play-button-w…
Browse files Browse the repository at this point in the history
…hile-process-is-running

Enable and Disable the Login and Play buttons on Character Summary view dependent on EFT's running state.
  • Loading branch information
Plootie authored May 30, 2024
2 parents 818cc90 + a87c89a commit 19ee540
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
29 changes: 29 additions & 0 deletions SIT.Manager/ViewModels/Play/CharacterSummaryViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Avalonia.Media.Imaging;
using Avalonia.Threading;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using FluentAvalonia.UI.Controls;
Expand Down Expand Up @@ -43,6 +44,9 @@ public partial class CharacterSummaryViewModel : ObservableRecipient
[ObservableProperty]
private bool _requireLogin = true;

[ObservableProperty]
public bool _canLaunch = true;

public IAsyncRelayCommand PlayCommand { get; }

public IAsyncRelayCommand LogoutCommand { get; }
Expand Down Expand Up @@ -90,6 +94,11 @@ public CharacterSummaryViewModel(AkiServer server,
Task.Run(SetSideImage);

PlayCommand = new AsyncRelayCommand(Play);

// In an ideal world we would use OnActivated and OnDeactivated - which are implemented from IActivatableViewModel in the Avalonia.ReactiveUI package.
// However, this would also require changes in the CharacterSummaryView class - for not this implementation, while crude, does suffice.
// It may be worth implementing Avalonia.ReactiveUI.IActivatableViewModel at a later date for all pages as part of a larger refactor.
_tarkovClientService.RunningStateChanged += TarkovClient_RunningStateChanged;
LogoutCommand = new AsyncRelayCommand(Logout);
}

Expand Down Expand Up @@ -165,6 +174,26 @@ private async Task Play()
}
}

private void TarkovClient_RunningStateChanged(object? sender, RunningState runningState)
{
Dispatcher.UIThread.Invoke(() =>
{
switch (runningState)
{
case RunningState.Starting:
case RunningState.Running:
CanLaunch = false;
break;
case RunningState.NotRunning:
case RunningState.StoppedUnexpectedly:
CanLaunch = true;
break;
}
});
}

private async Task Logout()

Check warning on line 197 in SIT.Manager/ViewModels/Play/CharacterSummaryViewModel.cs

View workflow job for this annotation

GitHub Actions / build (win-x64)

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.

Check warning on line 197 in SIT.Manager/ViewModels/Play/CharacterSummaryViewModel.cs

View workflow job for this annotation

GitHub Actions / build (win-x64)

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.

Check warning on line 197 in SIT.Manager/ViewModels/Play/CharacterSummaryViewModel.cs

View workflow job for this annotation

GitHub Actions / build (linux-x64)

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.

Check warning on line 197 in SIT.Manager/ViewModels/Play/CharacterSummaryViewModel.cs

View workflow job for this annotation

GitHub Actions / build (linux-x64)

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.
{
if (character != null)
Expand Down
4 changes: 2 additions & 2 deletions SIT.Manager/Views/Play/CharacterSummaryView.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@
</StackPanel>

<Button Grid.Column="2"
Command="{Binding PlayCommand}">
<Panel>
Command="{Binding PlayCommand}" IsEnabled="{Binding CanLaunch}">
<Panel>
<TextBlock IsVisible="{Binding !RequireLogin}"
Text="{DynamicResource CharacterSummaryViewPlayButtonText}"/>
<TextBlock IsVisible="{Binding RequireLogin}"
Expand Down

0 comments on commit 19ee540

Please sign in to comment.