Skip to content

feat: removed external dependencies (VPN, mutagen) connection management #111

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jun 2, 2025
Merged
Show file tree
Hide file tree
Changes from 3 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
27 changes: 14 additions & 13 deletions App/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,20 +165,21 @@ protected override void OnLaunched(LaunchActivatedEventArgs args)
}, CancellationToken.None);

// Initialize file sync.
var syncSessionCts = new CancellationTokenSource(TimeSpan.FromSeconds(10));
var syncSessionController = _services.GetRequiredService<ISyncSessionController>();
_ = syncSessionController.RefreshState(syncSessionCts.Token).ContinueWith(t =>
{
if (t.IsCanceled || t.Exception != null)
{
_logger.LogError(t.Exception, "failed to refresh sync state (canceled = {canceled})", t.IsCanceled);
#if DEBUG
Debugger.Break();
#endif
}

syncSessionCts.Dispose();
}, CancellationToken.None);
_ = Task.Delay(5000).ContinueWith((_) =>
{
var syncSessionCts = new CancellationTokenSource(TimeSpan.FromSeconds(10));
var syncSessionController = _services.GetRequiredService<ISyncSessionController>();
syncSessionController.RefreshState(syncSessionCts.Token).ContinueWith(
t =>
{
if (t.IsCanceled || t.Exception != null)
{
_logger.LogError(t.Exception, "failed to refresh sync state (canceled = {canceled})", t.IsCanceled);
}
syncSessionCts.Dispose();
}, CancellationToken.None);
});

// Prevent the TrayWindow from closing, just hide it.
var trayWindow = _services.GetRequiredService<TrayWindow>();
Expand Down
2 changes: 1 addition & 1 deletion App/Services/RpcController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ private void SpeakerOnError(Exception e)
Debug.WriteLine($"Error: {e}");
try
{
Reconnect(CancellationToken.None).Wait();
using var _ = Reconnect(CancellationToken.None);
}
catch
{
Expand Down
13 changes: 12 additions & 1 deletion App/ViewModels/FileSyncListViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ private void SyncSessionStateChanged(object? sender, SyncSessionControllerStateM
UpdateSyncSessionState(syncSessionState);
}

private void MaybeSetUnavailableMessage(RpcModel rpcModel, CredentialModel credentialModel)
private void MaybeSetUnavailableMessage(RpcModel rpcModel, CredentialModel credentialModel, SyncSessionControllerStateModel? syncSessionState = null)
{
var oldMessage = UnavailableMessage;
if (rpcModel.RpcLifecycle != RpcLifecycle.Connected)
Expand All @@ -205,6 +205,10 @@ private void MaybeSetUnavailableMessage(RpcModel rpcModel, CredentialModel crede
{
UnavailableMessage = "Please start Coder Connect from the tray window to access file sync.";
}
else if (syncSessionState != null && syncSessionState.Lifecycle == SyncSessionControllerLifecycle.Uninitialized)
{
UnavailableMessage = "Sync session controller is not initialized. Please wait...";
}
else
{
UnavailableMessage = null;
Expand All @@ -219,6 +223,13 @@ private void MaybeSetUnavailableMessage(RpcModel rpcModel, CredentialModel crede

private void UpdateSyncSessionState(SyncSessionControllerStateModel syncSessionState)
{
// This should never happen.
if (syncSessionState == null)
return;
if (syncSessionState.Lifecycle == SyncSessionControllerLifecycle.Uninitialized)
{
MaybeSetUnavailableMessage(_rpcController.GetState(), _credentialManager.GetCachedCredentials(), syncSessionState);
}
Error = syncSessionState.DaemonError;
Sessions = syncSessionState.SyncSessions.Select(s => new SyncSessionViewModel(this, s)).ToList();
}
Expand Down
21 changes: 19 additions & 2 deletions App/ViewModels/TrayWindowDisconnectedViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
using System.Threading.Tasks;
using Coder.Desktop.App.Models;
using Coder.Desktop.App.Services;
using Coder.Desktop.App.Views.Pages;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using System;
using System.Diagnostics;
using System.Threading.Tasks;

namespace Coder.Desktop.App.ViewModels;

Expand All @@ -11,6 +16,8 @@ public partial class TrayWindowDisconnectedViewModel : ObservableObject
private readonly IRpcController _rpcController;

[ObservableProperty] public partial bool ReconnectButtonEnabled { get; set; } = true;
[ObservableProperty] public partial string ErrorMessage { get; set; } = string.Empty;
[ObservableProperty] public partial bool ReconnectFailed { get; set; } = false;

public TrayWindowDisconnectedViewModel(IRpcController rpcController)
{
Expand All @@ -26,6 +33,16 @@ private void UpdateFromRpcModel(RpcModel rpcModel)
[RelayCommand]
public async Task Reconnect()
{
await _rpcController.Reconnect();
try
{
ReconnectFailed = false;
ErrorMessage = string.Empty;
await _rpcController.Reconnect();
}
catch (Exception ex)
{
ErrorMessage = ex.Message;
ReconnectFailed = true;
}
}
}
11 changes: 11 additions & 0 deletions App/Views/Pages/TrayWindowDisconnectedPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,17 @@

<controls:HorizontalRule />

<TextBlock TextWrapping="Wrap" Foreground="Red" Visibility="{x:Bind ViewModel.ReconnectFailed, Converter={StaticResource BoolToVisibilityConverter}, Mode=OneWay}">
<Bold>Reconnect failed</Bold>
</TextBlock>

<TextBlock
TextWrapping="Wrap"
Margin="0,0,0,10"
Foreground="Red"
Visibility="{x:Bind ViewModel.ReconnectFailed, Converter={StaticResource BoolToVisibilityConverter}, Mode=OneWay}"
Text="{x:Bind ViewModel.ErrorMessage, Mode=OneWay}" />

<HyperlinkButton
HorizontalContentAlignment="Left"
HorizontalAlignment="Stretch"
Expand Down
2 changes: 2 additions & 0 deletions App/Views/Pages/TrayWindowDisconnectedPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using Coder.Desktop.App.ViewModels;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using System;

namespace Coder.Desktop.App.Views.Pages;

Expand Down
3 changes: 1 addition & 2 deletions App/Views/TrayWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,7 @@ public TrayWindow(IRpcController rpcController, ICredentialManager credentialMan
private void SetPageByState(RpcModel rpcModel, CredentialModel credentialModel,
SyncSessionControllerStateModel syncSessionModel)
{
if (credentialModel.State == CredentialState.Unknown ||
syncSessionModel.Lifecycle == SyncSessionControllerLifecycle.Uninitialized)
if (credentialModel.State == CredentialState.Unknown)
{
SetRootFrame(_loadingPage);
return;
Expand Down
Loading