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

Merged
merged 18 commits into from
Jun 10, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
45 changes: 24 additions & 21 deletions App/Services/SettingsManager.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Google.Protobuf.WellKnownTypes;
using Serilog;
using System;
using System.Collections.Generic;
using System.IO;
Expand Down Expand Up @@ -30,11 +31,6 @@ namespace Coder.Desktop.App.Services;
/// <param name="ct"></param>
/// <returns></returns>
public Task Write(T settings, CancellationToken ct = default);
/// <summary>
/// Returns null if the settings are not cached or not available.
/// </summary>
/// <returns></returns>
public T? GetFromCache();
}

/// <summary>
Expand Down Expand Up @@ -80,6 +76,12 @@ public SettingsManager(string? settingsFilePath = null)

public async Task<T> Read(CancellationToken ct = default)
{
if (_cachedSettings is not null)
{
// return cached settings if available
return (T)_cachedSettings.Clone();
}

// try to get the lock with short timeout
if (!await _gate.WaitAsync(LockTimeout, ct).ConfigureAwait(false))
throw new InvalidOperationException(
Expand Down Expand Up @@ -145,41 +147,39 @@ await File.WriteAllTextAsync(_settingsFilePath, json, ct)
_gate.Release();
}
}

public T? GetFromCache()
{
return _cachedSettings;
}
}

public interface ISettings
{
/// <summary>
/// Gets the version of the settings schema.
/// FileName where the settings are stored.
/// </summary>
int Version { get; }
static abstract string SettingsFileName { get; }

/// <summary>
/// FileName where the settings are stored.
/// Gets the version of the settings schema.
/// </summary>
static abstract string SettingsFileName { get; }
int Version { get; }

ISettings Clone();
}

/// <summary>
/// CoderConnect settings class that holds the settings for the CoderConnect feature.
/// </summary>
public class CoderConnectSettings : ISettings
{
public static string SettingsFileName { get; } = "coder-connect-settings.json";
public int Version { get; set; }
public bool ConnectOnLaunch { get; set; }

/// <summary>
/// CoderConnect settings version. Increment this when the settings schema changes.
/// CoderConnect current 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 bool ConnectOnLaunch { get; set; }
public static string SettingsFileName { get; } = "coder-connect-settings.json";
private const int VERSION = 1;

private const int VERSION = 1; // Default version for backward compatibility
public CoderConnectSettings()
{
Version = VERSION;
Expand All @@ -192,10 +192,13 @@ public CoderConnectSettings(int? version, bool connectOnLogin)
ConnectOnLaunch = connectOnLogin;
}

ISettings ISettings.Clone()
{
return Clone();
}

public CoderConnectSettings Clone()
{
return new CoderConnectSettings(Version, ConnectOnLaunch);
}


}
6 changes: 1 addition & 5 deletions App/ViewModels/SettingsViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,7 @@ public SettingsViewModel(ILogger<SettingsViewModel> logger, ISettingsManager<Cod
_logger = logger;
// Application settings are loaded on application startup,
// so we expect the settings to be available immediately.
var settingsCache = settingsManager.GetFromCache();
if (settingsCache is not null)
{
_connectSettings = settingsCache.Clone();
}
var settingsCache = settingsManager.Read();
StartOnLogin = startupManager.IsEnabled();
ConnectOnLaunch = _connectSettings.ConnectOnLaunch;

Expand Down
Loading