Skip to content

added new settings dialog + settings manager #113

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

Open
wants to merge 17 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 10 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
1 change: 1 addition & 0 deletions App/App.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
<ItemGroup>
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.4.0" />
<PackageReference Include="CommunityToolkit.WinUI.Controls.Primitives" Version="8.2.250402" />
<PackageReference Include="CommunityToolkit.WinUI.Controls.SettingsControls" Version="8.2.250402" />
<PackageReference Include="CommunityToolkit.WinUI.Extensions" Version="8.2.250402" />
<PackageReference Include="DependencyPropertyGenerator" Version="1.5.0">
<PrivateAssets>all</PrivateAssets>
Expand Down
122 changes: 71 additions & 51 deletions App/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ public partial class App : Application
private readonly ILogger<App> _logger;
private readonly IUriHandler _uriHandler;

private readonly ISettingsManager _settingsManager;

private readonly IHostApplicationLifetime _appLifetime;

public App()
{
var builder = Host.CreateApplicationBuilder();
Expand Down Expand Up @@ -90,6 +94,13 @@ public App()
// FileSyncListMainPage is created by FileSyncListWindow.
services.AddTransient<FileSyncListWindow>();

services.AddSingleton<ISettingsManager, SettingsManager>();
services.AddSingleton<IStartupManager, StartupManager>();
// SettingsWindow views and view models
services.AddTransient<SettingsViewModel>();
// SettingsMainPage is created by SettingsWindow.
services.AddTransient<SettingsWindow>();

// DirectoryPickerWindow views and view models are created by FileSyncListViewModel.

// TrayWindow views and view models
Expand All @@ -109,6 +120,8 @@ public App()
_services = services.BuildServiceProvider();
_logger = (ILogger<App>)_services.GetService(typeof(ILogger<App>))!;
_uriHandler = (IUriHandler)_services.GetService(typeof(IUriHandler))!;
_settingsManager = (ISettingsManager)_services.GetService(typeof(ISettingsManager))!;
_appLifetime = (IHostApplicationLifetime)_services.GetRequiredService<IHostApplicationLifetime>();

InitializeComponent();
}
Expand All @@ -129,58 +142,8 @@ public async Task ExitApplication()
protected override void OnLaunched(LaunchActivatedEventArgs args)
{
_logger.LogInformation("new instance launched");
// Start connecting to the manager in the background.
var rpcController = _services.GetRequiredService<IRpcController>();
if (rpcController.GetState().RpcLifecycle == RpcLifecycle.Disconnected)
// Passing in a CT with no cancellation is desired here, because
// the named pipe open will block until the pipe comes up.
_logger.LogDebug("reconnecting with VPN service");
_ = rpcController.Reconnect(CancellationToken.None).ContinueWith(t =>
{
if (t.Exception != null)
{
_logger.LogError(t.Exception, "failed to connect to VPN service");
#if DEBUG
Debug.WriteLine(t.Exception);
Debugger.Break();
#endif
}
});

// Load the credentials in the background.
var credentialManagerCts = new CancellationTokenSource(TimeSpan.FromSeconds(15));
var credentialManager = _services.GetRequiredService<ICredentialManager>();
_ = credentialManager.LoadCredentials(credentialManagerCts.Token).ContinueWith(t =>
{
if (t.Exception != null)
{
_logger.LogError(t.Exception, "failed to load credentials");
#if DEBUG
Debug.WriteLine(t.Exception);
Debugger.Break();
#endif
}

credentialManagerCts.Dispose();
}, CancellationToken.None);

// Initialize file sync.
// We're adding a 5s delay here to avoid race conditions when loading the mutagen binary.

_ = 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);
});
_ = InitializeServicesAsync(_appLifetime.ApplicationStopping);

// Prevent the TrayWindow from closing, just hide it.
var trayWindow = _services.GetRequiredService<TrayWindow>();
Expand All @@ -192,6 +155,63 @@ protected override void OnLaunched(LaunchActivatedEventArgs args)
};
}

/// <summary>
/// Loads stored VPN credentials, reconnects the RPC controller,
/// and (optionally) starts the VPN tunnel on application launch.
/// </summary>
private async Task InitializeServicesAsync(CancellationToken cancellationToken = default)
{
var credentialManager = _services.GetRequiredService<ICredentialManager>();
var rpcController = _services.GetRequiredService<IRpcController>();

using var credsCts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
credsCts.CancelAfter(TimeSpan.FromSeconds(15));

Task loadCredsTask = credentialManager.LoadCredentials(credsCts.Token);
Task reconnectTask = rpcController.Reconnect(cancellationToken);

try
{
await Task.WhenAll(loadCredsTask, reconnectTask);
}
catch (Exception)
{
if (loadCredsTask.IsFaulted)
_logger.LogError(loadCredsTask.Exception!.GetBaseException(),
"Failed to load credentials");

if (reconnectTask.IsFaulted)
_logger.LogError(reconnectTask.Exception!.GetBaseException(),
"Failed to connect to VPN service");

return;
}

if (_settingsManager.ConnectOnLaunch)
{
try
{
await rpcController.StartVpn(cancellationToken);
}
catch (Exception ex)
{
_logger.LogError(ex, "Failed to connect on launch");
}
}

// Initialize file sync.
var syncSessionCts = new CancellationTokenSource(TimeSpan.FromSeconds(10));
var syncSessionController = _services.GetRequiredService<ISyncSessionController>();
try
{
await syncSessionController.RefreshState(syncSessionCts.Token);
}
catch (Exception ex)
{
_logger.LogError($"Failed to refresh sync session state {ex.Message}", ex);
}
}

public void OnActivated(object? sender, AppActivationArguments args)
{
switch (args.Kind)
Expand Down
189 changes: 189 additions & 0 deletions App/Services/SettingsManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace Coder.Desktop.App.Services;

/// <summary>
/// Settings contract exposing properties for app settings.
/// </summary>
public interface ISettingsManager
{
/// <summary>
/// Returns the value of the StartOnLogin setting. Returns <c>false</c> if the key is not found.
/// </summary>
bool StartOnLogin { get; set; }

/// <summary>
/// Returns the value of the ConnectOnLaunch setting. Returns <c>false</c> if the key is not found.
/// </summary>
bool ConnectOnLaunch { get; set; }
}

/// <summary>
/// Implemention of <see cref="ISettingsManager"/> that persists settings to a JSON file
/// located in the user's local application data folder.
/// </summary>
public sealed class SettingsManager : ISettingsManager
{
private readonly string _settingsFilePath;
private Settings _settings;
private readonly string _fileName = "app-settings.json";
private readonly string _appName = "CoderDesktop";
private readonly object _lock = new();

public const string ConnectOnLaunchKey = "ConnectOnLaunch";
public const string StartOnLoginKey = "StartOnLogin";

public bool StartOnLogin
{
get
{
return Read(StartOnLoginKey, false);
}
set
{
Save(StartOnLoginKey, value);
}
}

public bool ConnectOnLaunch
{
get
{
return Read(ConnectOnLaunchKey, false);
}
set
{
Save(ConnectOnLaunchKey, value);
}
}

/// <param name="settingsFilePath">
/// For unit‑tests you can pass an absolute path that already exists.
/// Otherwise the settings file will be created in the user's local application data folder.
/// </param>
public SettingsManager(string? settingsFilePath = null)
{
if (settingsFilePath is null)
{
settingsFilePath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
}
else if (!Path.IsPathRooted(settingsFilePath))
{
throw new ArgumentException("settingsFilePath must be an absolute path if provided", nameof(settingsFilePath));
}

var folder = Path.Combine(
settingsFilePath,
_appName);

Directory.CreateDirectory(folder);
_settingsFilePath = Path.Combine(folder, _fileName);

if (!File.Exists(_settingsFilePath))
{
// Create the settings file if it doesn't exist
_settings = new();
File.WriteAllText(_settingsFilePath, JsonSerializer.Serialize(_settings, SettingsJsonContext.Default.Settings));
}
else
{
_settings = Load();
}
}

private void Save(string name, bool value)
{
lock (_lock)
{
try
{
// We lock the file for the entire operation to prevent concurrent writes
using var fs = new FileStream(_settingsFilePath,
FileMode.OpenOrCreate,
FileAccess.ReadWrite,
FileShare.None);

// Ensure cache is loaded before saving
var freshCache = JsonSerializer.Deserialize(fs, SettingsJsonContext.Default.Settings) ?? new();
_settings = freshCache;
_settings.Options[name] = JsonSerializer.SerializeToElement(value);
fs.Position = 0; // Reset stream position to the beginning before writing

JsonSerializer.Serialize(fs, _settings, SettingsJsonContext.Default.Settings);

// This ensures the file is truncated to the new length
// if the new content is shorter than the old content
fs.SetLength(fs.Position);
}
catch
{
throw new InvalidOperationException($"Failed to persist settings to {_settingsFilePath}. The file may be corrupted, malformed or locked.");
}
}
}

private bool Read(string name, bool defaultValue)
{
lock (_lock)
{
if (_settings.Options.TryGetValue(name, out var element))
{
try
{
return element.Deserialize<bool?>() ?? defaultValue;
}
catch
{
// malformed value – return default value
return defaultValue;
}
}
return defaultValue; // key not found – return default value
}
}

private Settings Load()
{
try
{
using var fs = File.OpenRead(_settingsFilePath);
return JsonSerializer.Deserialize(fs, SettingsJsonContext.Default.Settings) ?? new();
}
catch (Exception ex)
{
throw new InvalidOperationException($"Failed to load settings from {_settingsFilePath}. The file may be corrupted or malformed. Exception: {ex.Message}");
}
}
}

public class Settings
{
/// <summary>
/// User settings version. Increment this when the settings schema changes.
/// In future iterations we will be able to handle migrations when the user has
/// an older version.
/// </summary>
public int Version { get; set; }
public Dictionary<string, JsonElement> Options { get; set; }

private const int VERSION = 1; // Default version for backward compatibility
public Settings()
{
Version = VERSION;
Options = [];
}

public Settings(int? version, Dictionary<string, JsonElement> options)
{
Version = version ?? VERSION;
Options = options;
}
}

[JsonSerializable(typeof(Settings))]
[JsonSourceGenerationOptions(WriteIndented = true)]
public partial class SettingsJsonContext : JsonSerializerContext;
Loading
Loading