From f8e267f75ead2d88513e2336a5eb9a22c6fa7b65 Mon Sep 17 00:00:00 2001 From: Steve Jackson Date: Thu, 23 May 2024 02:26:26 +0100 Subject: [PATCH] Updated the CharacterSummaryViewModel to expose an additional prooperty _canLaunch (with an Observable generated instance) which is set to either true or false (defaults to true) depending on the state of _tarkovClientService.RunningStateChanged. An event handler is attached to listen for RunningStateChanged events, if the state is either Starting or Running _canLaunch is set to false, the other states (NotRunning and StoppedUnexpectedly) set this to true. The CharacterSummaryView.axaml's command button (Login or Play) has the property IsEnabled bound to this new property - in doing this all buttons are enabled or disabled when EFT is started/stopped. --- .../Play/CharacterSummaryViewModel.cs | 29 +++++++++++++++++++ .../Views/Play/CharacterSummaryView.axaml | 4 +-- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/SIT.Manager/ViewModels/Play/CharacterSummaryViewModel.cs b/SIT.Manager/ViewModels/Play/CharacterSummaryViewModel.cs index bd7d0dc7..3812a9ed 100644 --- a/SIT.Manager/ViewModels/Play/CharacterSummaryViewModel.cs +++ b/SIT.Manager/ViewModels/Play/CharacterSummaryViewModel.cs @@ -1,4 +1,5 @@ using Avalonia.Media.Imaging; +using Avalonia.Threading; using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.Input; using FluentAvalonia.UI.Controls; @@ -41,6 +42,9 @@ public partial class CharacterSummaryViewModel : ObservableRecipient [ObservableProperty] private bool _requireLogin = true; + [ObservableProperty] + public bool _canLaunch = true; + public IAsyncRelayCommand PlayCommand { get; } public CharacterSummaryViewModel(AkiServer server, @@ -86,6 +90,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; } private async Task SetSideImage() @@ -161,4 +170,24 @@ private async Task Play() await errorDialog.ShowAsync(); } } + + 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; + } + }); + } } diff --git a/SIT.Manager/Views/Play/CharacterSummaryView.axaml b/SIT.Manager/Views/Play/CharacterSummaryView.axaml index a93c0912..983ba33c 100644 --- a/SIT.Manager/Views/Play/CharacterSummaryView.axaml +++ b/SIT.Manager/Views/Play/CharacterSummaryView.axaml @@ -53,8 +53,8 @@