|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.IO; |
| 4 | +using System.Linq; |
| 5 | +using System.Text; |
| 6 | +using System.Text.Json; |
| 7 | +using System.Threading.Tasks; |
| 8 | + |
| 9 | +namespace Coder.Desktop.App.Services; |
| 10 | +/// <summary> |
| 11 | +/// Generic persistence contract for simple key/value settings. |
| 12 | +/// </summary> |
| 13 | +public interface ISettingsManager |
| 14 | +{ |
| 15 | + /// <summary> |
| 16 | + /// Saves <paramref name="value"/> under <paramref name="name"/> and returns the value. |
| 17 | + /// </summary> |
| 18 | + T Save<T>(string name, T value); |
| 19 | + |
| 20 | + /// <summary> |
| 21 | + /// Reads the setting or returns <paramref name="defaultValue"/> when the key is missing. |
| 22 | + /// </summary> |
| 23 | + T Read<T>(string name, T defaultValue); |
| 24 | +} |
| 25 | +/// <summary> |
| 26 | +/// JSON‑file implementation that works in unpackaged Win32/WinUI 3 apps. |
| 27 | +/// </summary> |
| 28 | +public sealed class SettingsManager : ISettingsManager |
| 29 | +{ |
| 30 | + private readonly string _settingsFilePath; |
| 31 | + private readonly string _fileName = "app-settings.json"; |
| 32 | + private readonly object _lock = new(); |
| 33 | + private Dictionary<string, JsonElement> _cache; |
| 34 | + |
| 35 | + /// <param name="appName"> |
| 36 | + /// Sub‑folder under %LOCALAPPDATA% (e.g. "coder-desktop"). |
| 37 | + /// If <c>null</c> the folder name defaults to the executable name. |
| 38 | + /// For unit‑tests you can pass an absolute path that already exists. |
| 39 | + /// </param> |
| 40 | + public SettingsManager(string? appName = null) |
| 41 | + { |
| 42 | + // Allow unit‑tests to inject a fully‑qualified path. |
| 43 | + if (appName is not null && Path.IsPathRooted(appName)) |
| 44 | + { |
| 45 | + _settingsFilePath = Path.Combine(appName, _fileName); |
| 46 | + Directory.CreateDirectory(Path.GetDirectoryName(_settingsFilePath)!); |
| 47 | + } |
| 48 | + else |
| 49 | + { |
| 50 | + string folder = Path.Combine( |
| 51 | + Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), |
| 52 | + appName ?? AppDomain.CurrentDomain.FriendlyName.ToLowerInvariant()); |
| 53 | + Directory.CreateDirectory(folder); |
| 54 | + _settingsFilePath = Path.Combine(folder, _fileName); |
| 55 | + } |
| 56 | + |
| 57 | + _cache = Load(); |
| 58 | + } |
| 59 | + |
| 60 | + public T Save<T>(string name, T value) |
| 61 | + { |
| 62 | + lock (_lock) |
| 63 | + { |
| 64 | + _cache[name] = JsonSerializer.SerializeToElement(value); |
| 65 | + Persist(); |
| 66 | + return value; |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + public T Read<T>(string name, T defaultValue) |
| 71 | + { |
| 72 | + lock (_lock) |
| 73 | + { |
| 74 | + if (_cache.TryGetValue(name, out var element)) |
| 75 | + { |
| 76 | + try |
| 77 | + { |
| 78 | + return element.Deserialize<T>() ?? defaultValue; |
| 79 | + } |
| 80 | + catch |
| 81 | + { |
| 82 | + // Malformed value – fall back. |
| 83 | + return defaultValue; |
| 84 | + } |
| 85 | + } |
| 86 | + return defaultValue; // key not found – return caller‑supplied default (false etc.) |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + private Dictionary<string, JsonElement> Load() |
| 91 | + { |
| 92 | + if (!File.Exists(_settingsFilePath)) |
| 93 | + return new(); |
| 94 | + |
| 95 | + try |
| 96 | + { |
| 97 | + using var fs = File.OpenRead(_settingsFilePath); |
| 98 | + return JsonSerializer.Deserialize<Dictionary<string, JsonElement>>(fs) ?? new(); |
| 99 | + } |
| 100 | + catch |
| 101 | + { |
| 102 | + // Corrupted file – start fresh. |
| 103 | + return new(); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + private void Persist() |
| 108 | + { |
| 109 | + using var fs = File.Create(_settingsFilePath); |
| 110 | + var options = new JsonSerializerOptions { WriteIndented = true }; |
| 111 | + JsonSerializer.Serialize(fs, _cache, options); |
| 112 | + } |
| 113 | +} |
0 commit comments