Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,6 @@ public interface IChangeServerModerator
TimeSpan GetDelayUntilNextAttempt();

TimeSpan GetRemainingDelayUntilNextAttempt();

bool HasTroubleConnecting();
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,16 @@ public class ChangeServerModerator :
IEventMessageReceiver<SettingChangedMessage>,
IEventMessageReceiver<ConnectionStatusChangedMessage>
{
private const int TroubleConnectingDelaySeconds = 6;

private readonly ISettings _settings;
private readonly IConnectionManager _connectionManager;
private readonly IEventMessageSender _eventMessageSender;

private ChangeServerSettings _changeServerSettings;
private ChangeServerAttempts _changeServerAttempts;
private DateTimeOffset? _connectionStartTime;
private bool _isConnecting;

protected override TimeSpan PollingInterval { get; } = TimeSpan.FromSeconds(1);

Expand Down Expand Up @@ -92,6 +96,17 @@ public TimeSpan GetRemainingDelayUntilNextAttempt()
: TimeSpan.FromSeconds(Math.Ceiling(remainingTime.TotalSeconds));
}

public bool HasTroubleConnecting()
{
if (!_isConnecting || !_connectionStartTime.HasValue)
{
return false;
}

TimeSpan elapsedTime = DateTimeOffset.UtcNow - _connectionStartTime.Value;
return elapsedTime.TotalSeconds >= TroubleConnectingDelaySeconds;
}

public void Receive(LoggedInMessage message)
{
InvalidateChangeServerAttempts();
Expand All @@ -116,6 +131,20 @@ public void Receive(SettingChangedMessage message)

public void Receive(ConnectionStatusChangedMessage message)
{
// Track connection state for HasTroubleConnecting logic
switch (message.ConnectionStatus)
{
case ConnectionStatus.Connecting:
_isConnecting = true;
_connectionStartTime = DateTimeOffset.UtcNow;
break;
case ConnectionStatus.Connected:
case ConnectionStatus.Disconnected:
_isConnecting = false;
_connectionStartTime = null;
break;
}

if (_connectionManager.IsConnected &&
_connectionManager.CurrentConnectionIntent?.Location is FreeServerLocationIntent intent &&
intent.Kind == ConnectionIntentKind.Random)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public partial class ChangeServerComponentViewModel : ActivatableViewModelBase,
public double DelayInSeconds => _changeServerModerator.GetDelayUntilNextAttempt().TotalSeconds;
public double RemainingDelayInSeconds => _changeServerModerator.GetRemainingDelayUntilNextAttempt().TotalSeconds;
public string? FormattedRemainingTime => Localizer.GetFormattedShortTime(_changeServerModerator.GetRemainingDelayUntilNextAttempt());
public bool HasTroubleConnecting => _changeServerModerator.HasTroubleConnecting();

public ChangeServerComponentViewModel(
IConnectionManager connectionManager,
Expand Down Expand Up @@ -99,7 +100,10 @@ private Task ChangeServerAsync()

private bool CanChangeServer()
{
return _connectionManager.IsConnected && _changeServerModerator.CanChangeServer();
bool isConnectedOrHasTroubleConnecting = _connectionManager.IsConnected ||
(_connectionManager.IsConnecting && _changeServerModerator.HasTroubleConnecting());

return isConnectedOrHasTroubleConnecting && _changeServerModerator.CanChangeServer();
}

[RelayCommand]
Expand All @@ -115,6 +119,7 @@ private void InvalidateChangeServer()
OnPropertyChanged(nameof(DelayInSeconds));
OnPropertyChanged(nameof(RemainingDelayInSeconds));
OnPropertyChanged(nameof(FormattedRemainingTime));
OnPropertyChanged(nameof(HasTroubleConnecting));

ChangeServerCommand.NotifyCanExecuteChanged();
}
Expand Down