|
4 | 4 | usingSystem.Text.Json;
|
5 | 5 |
|
6 | 6 | namespaceCoder.Desktop.App.Services;
|
| 7 | + |
7 | 8 | /// <summary>
|
8 |
| -///Generic persistencecontractfor simple key/value settings. |
| 9 | +///Settingscontractexposing properties for app settings. |
9 | 10 | /// </summary>
|
10 | 11 | publicinterfaceISettingsManager
|
11 | 12 | {
|
12 | 13 | /// <summary>
|
13 |
| -///Saves <paramref name="value"/> under <paramref name="name"/> and returnsthevalue. |
| 14 | +///Returns thevalue of the StartOnLogin setting. Returns <c>false</c> ifthekey is not found. |
14 | 15 | /// </summary>
|
15 |
| -TSave<T>(stringname,Tvalue); |
| 16 | +boolStartOnLogin{get;set;} |
16 | 17 |
|
17 | 18 | /// <summary>
|
18 |
| -///Reads thesetting or returns <paramref name="defaultValue"/> when the key ismissing. |
| 19 | +///Returns thevalue of the ConnectOnLaunch setting. Returns <c>false</c> if the key isnot found. |
19 | 20 | /// </summary>
|
20 |
| -TRead<T>(stringname,TdefaultValue); |
| 21 | +boolConnectOnLaunch{get;set;} |
21 | 22 | }
|
| 23 | + |
22 | 24 | /// <summary>
|
23 |
| -/// JSON‑file implementation that works in unpackaged Win32/WinUI 3 apps. |
| 25 | +/// Implemention of <see cref="ISettingsManager"/> that persists settings to a JSON file |
| 26 | +/// located in the user's local application data folder. |
24 | 27 | /// </summary>
|
25 | 28 | publicsealedclassSettingsManager:ISettingsManager
|
26 | 29 | {
|
27 | 30 | privatereadonlystring_settingsFilePath;
|
28 | 31 | privatereadonlystring_fileName="app-settings.json";
|
| 32 | +privatereadonlystring_appName="CoderDesktop"; |
29 | 33 | privatereadonlyobject_lock=new();
|
30 | 34 | privateDictionary<string,JsonElement>_cache;
|
31 | 35 |
|
32 |
| -publicstaticreadonlystringConnectOnLaunchKey="ConnectOnLaunch"; |
33 |
| -publicstaticreadonlystringStartOnLoginKey="StartOnLogin"; |
| 36 | +publicconststringConnectOnLaunchKey="ConnectOnLaunch"; |
| 37 | +publicconststringStartOnLoginKey="StartOnLogin"; |
34 | 38 |
|
35 |
| -/// <param name="appName"> |
36 |
| -/// Sub‑folder under %LOCALAPPDATA% (e.g. "CoderDesktop"). |
37 |
| -/// If <c>null</c> the folder name defaults to the executable name. |
| 39 | +publicboolStartOnLogin |
| 40 | +{ |
| 41 | +get |
| 42 | +{ |
| 43 | +returnRead(StartOnLoginKey,false); |
| 44 | +} |
| 45 | +set |
| 46 | +{ |
| 47 | +Save(StartOnLoginKey,value); |
| 48 | +} |
| 49 | +} |
| 50 | + |
| 51 | +publicboolConnectOnLaunch |
| 52 | +{ |
| 53 | +get |
| 54 | +{ |
| 55 | +returnRead(ConnectOnLaunchKey,false); |
| 56 | +} |
| 57 | +set |
| 58 | +{ |
| 59 | +Save(ConnectOnLaunchKey,value); |
| 60 | +} |
| 61 | +} |
| 62 | + |
| 63 | +/// <param name="settingsFilePath"> |
38 | 64 | /// For unit‑tests you can pass an absolute path that already exists.
|
| 65 | +/// Otherwise the settings file will be created in the user's local application data folder. |
39 | 66 | /// </param>
|
40 |
| -publicSettingsManager(string?appName=null) |
| 67 | +publicSettingsManager(string?settingsFilePath=null) |
41 | 68 | {
|
42 |
| -// Allow unit‑tests to inject a fully‑qualified path. |
43 |
| -if(appNameis notnull&&Path.IsPathRooted(appName)) |
| 69 | +if(settingsFilePathisnull) |
44 | 70 | {
|
45 |
| -_settingsFilePath=Path.Combine(appName,_fileName); |
46 |
| -Directory.CreateDirectory(Path.GetDirectoryName(_settingsFilePath)!); |
| 71 | +settingsFilePath=Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData); |
47 | 72 | }
|
48 |
| -else |
| 73 | +elseif(!Path.IsPathRooted(settingsFilePath)) |
49 | 74 | {
|
50 |
| -stringfolder=Path.Combine( |
51 |
| -Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), |
52 |
| -appName??AppDomain.CurrentDomain.FriendlyName.ToLowerInvariant()); |
53 |
| -Directory.CreateDirectory(folder); |
54 |
| -_settingsFilePath=Path.Combine(folder,_fileName); |
| 75 | +thrownewArgumentException("settingsFilePath must be an absolute path if provided",nameof(settingsFilePath)); |
| 76 | +} |
| 77 | + |
| 78 | +stringfolder=Path.Combine( |
| 79 | +settingsFilePath, |
| 80 | +_appName); |
| 81 | + |
| 82 | +Directory.CreateDirectory(folder); |
| 83 | +_settingsFilePath=Path.Combine(folder,_fileName); |
| 84 | + |
| 85 | +if(!File.Exists(_settingsFilePath)) |
| 86 | +{ |
| 87 | +// Create the settings file if it doesn't exist |
| 88 | +stringemptyJson=JsonSerializer.Serialize(new{}); |
| 89 | +File.WriteAllText(_settingsFilePath,emptyJson); |
55 | 90 | }
|
56 | 91 |
|
57 | 92 | _cache=Load();
|
58 | 93 | }
|
59 | 94 |
|
60 |
| -publicTSave<T>(stringname,Tvalue) |
| 95 | +privatevoidSave(stringname,boolvalue) |
61 | 96 | {
|
62 | 97 | lock(_lock)
|
63 | 98 | {
|
64 |
| -_cache[name]=JsonSerializer.SerializeToElement(value); |
65 |
| -Persist(); |
66 |
| -returnvalue; |
| 99 | +try |
| 100 | +{ |
| 101 | +// Ensure cache is loaded before saving |
| 102 | +usingvarfs=newFileStream(_settingsFilePath, |
| 103 | +FileMode.OpenOrCreate, |
| 104 | +FileAccess.ReadWrite, |
| 105 | +FileShare.None); |
| 106 | + |
| 107 | +varcurrentCache=JsonSerializer.Deserialize<Dictionary<string,JsonElement>>(fs)??new(); |
| 108 | +_cache=currentCache; |
| 109 | +_cache[name]=JsonSerializer.SerializeToElement(value); |
| 110 | +fs.Position=0;// Reset stream position to the beginning before writing to override the file |
| 111 | +varoptions=newJsonSerializerOptions{WriteIndented=true}; |
| 112 | +JsonSerializer.Serialize(fs,_cache,options); |
| 113 | +} |
| 114 | +catch |
| 115 | +{ |
| 116 | +thrownewInvalidOperationException($"Failed to persist settings to{_settingsFilePath}. The file may be corrupted, malformed or locked."); |
| 117 | +} |
67 | 118 | }
|
68 | 119 | }
|
69 | 120 |
|
70 |
| -publicTRead<T>(stringname,TdefaultValue) |
| 121 | +privateboolRead(stringname,booldefaultValue) |
71 | 122 | {
|
72 | 123 | lock(_lock)
|
73 | 124 | {
|
74 | 125 | if(_cache.TryGetValue(name,outvarelement))
|
75 | 126 | {
|
76 | 127 | try
|
77 | 128 | {
|
78 |
| -returnelement.Deserialize<T>()??defaultValue; |
| 129 | +returnelement.Deserialize<bool?>()??defaultValue; |
79 | 130 | }
|
80 | 131 | catch
|
81 | 132 | {
|
82 |
| -//Malformed value –fall back. |
| 133 | +//malformed value –return default value |
83 | 134 | returndefaultValue;
|
84 | 135 | }
|
85 | 136 | }
|
86 |
| -returndefaultValue;// key not found – returncaller‑supplieddefault(false etc.) |
| 137 | +returndefaultValue;// key not found – return defaultvalue |
87 | 138 | }
|
88 | 139 | }
|
89 | 140 |
|
90 | 141 | privateDictionary<string,JsonElement>Load()
|
91 | 142 | {
|
92 |
| -if(!File.Exists(_settingsFilePath)) |
93 |
| -returnnew(); |
94 |
| - |
95 | 143 | try
|
96 | 144 | {
|
97 | 145 | usingvarfs=File.OpenRead(_settingsFilePath);
|
98 | 146 | returnJsonSerializer.Deserialize<Dictionary<string,JsonElement>>(fs)??new();
|
99 | 147 | }
|
100 |
| -catch |
| 148 | +catch(Exceptionex) |
101 | 149 | {
|
102 |
| -// Corrupted file – start fresh. |
103 |
| -returnnew(); |
| 150 | +thrownewInvalidOperationException($"Failed to load settings from{_settingsFilePath}. The file may be corrupted or malformed. Exception:{ex.Message}"); |
104 | 151 | }
|
105 | 152 | }
|
106 |
| - |
107 |
| -privatevoidPersist() |
108 |
| -{ |
109 |
| -usingvarfs=File.Create(_settingsFilePath); |
110 |
| -varoptions=newJsonSerializerOptions{WriteIndented=true}; |
111 |
| -JsonSerializer.Serialize(fs,_cache,options); |
112 |
| -} |
113 | 153 | }
|