-
Notifications
You must be signed in to change notification settings - Fork 4
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
ibetitsmike
wants to merge
17
commits into
main
Choose a base branch
from
mike/57-starting-coder-desktop-on-boot
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 10 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
7465bb5
added new settings dialog + settings manager
ibetitsmike cd99645
WIP
ibetitsmike 779c11b
added StartupManager to handle auto-start
ibetitsmike fcefec4
settings manager moved from generic to explicit settings
ibetitsmike 39ff83c
added comments
ibetitsmike 07ec725
PR review + fmt
ibetitsmike c21072f
created Settings class to handle versioning
ibetitsmike bad5320
async handling of dependency load in app
ibetitsmike 065eda1
fmt fix
ibetitsmike fc426a8
JsonContext improvements and usage within Settings
ibetitsmike fa4fbd8
implemented a generic settings manager
ibetitsmike e7b2491
formatting
ibetitsmike ced517e
PR adjustments
ibetitsmike c4c52e2
PR review fixes
ibetitsmike 0c7567b
renamed Settings models
ibetitsmike 2824bd8
comment added to ConnectOnLaunch setting
ibetitsmike c11f6db
removed unecessary using
ibetitsmike File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
ibetitsmike marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
/// <summary> | ||
/// Settings contract exposing properties for app settings. | ||
/// </summary> | ||
public interface ISettingsManager | ||
ibetitsmike marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
/// <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; } | ||
} | ||
ibetitsmike marked this conversation as resolved.
Show resolved
Hide resolved
ibetitsmike marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
/// <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 | ||
ibetitsmike marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
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; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.