1- using Google . Protobuf . WellKnownTypes ;
2- using Serilog ;
31using System ;
4- using System . Collections . Generic ;
52using System . IO ;
63using System . Text . Json ;
7- using System . Text . Json . Serialization ;
84using System . Threading ;
95using System . Threading . Tasks ;
10- using System . Xml . Linq ;
6+ using Coder . Desktop . App . Models ;
117
128namespace Coder . Desktop . App . Services ;
139
1410/// <summary>
1511/// Settings contract exposing properties for app settings.
1612/// </summary>
17- public interface ISettingsManager < T > where T : ISettings , new ( )
13+ public interface ISettingsManager < T > where T : ISettings < T > , new ( )
1814{
1915 /// <summary>
2016 /// Reads the settings from the file system.
@@ -23,26 +19,25 @@ namespace Coder.Desktop.App.Services;
2319 /// </summary>
2420 /// <param name="ct"></param>
2521 /// <returns></returns>
26- public Task < T > Read ( CancellationToken ct = default ) ;
22+ Task < T > Read ( CancellationToken ct = default ) ;
2723 /// <summary>
2824 /// Writes the settings to the file system.
2925 /// </summary>
3026 /// <param name="settings">Object containing the settings.</param>
3127 /// <param name="ct"></param>
3228 /// <returns></returns>
33- public Task Write ( T settings , CancellationToken ct = default ) ;
29+ Task Write ( T settings , CancellationToken ct = default ) ;
3430}
3531
3632/// <summary>
3733/// Implemention of <see cref="ISettingsManager"/> that persists settings to a JSON file
3834/// located in the user's local application data folder.
3935/// </summary>
40- public sealed class SettingsManager < T > : ISettingsManager < T > where T : ISettings , new ( )
36+ public sealed class SettingsManager < T > : ISettingsManager < T > where T : ISettings < T > , new ( )
4137{
4238 private readonly string _settingsFilePath ;
4339 private readonly string _appName = "CoderDesktop" ;
4440 private string _fileName ;
45- private readonly object _lock = new ( ) ;
4641
4742 private T ? _cachedSettings ;
4843
@@ -79,7 +74,7 @@ public async Task<T> Read(CancellationToken ct = default)
7974 if ( _cachedSettings is not null )
8075 {
8176 // return cached settings if available
82- return ( T ) _cachedSettings . Clone ( ) ;
77+ return _cachedSettings . Clone ( ) ;
8378 }
8479
8580 // try to get the lock with short timeout
@@ -98,7 +93,7 @@ public async Task<T> Read(CancellationToken ct = default)
9893 // deserialize; fall back to default(T) if empty or malformed
9994 var result = JsonSerializer . Deserialize < T > ( json ) ! ;
10095 _cachedSettings = result ;
101- return result ;
96+ return _cachedSettings . Clone ( ) ; // return a fresh instance of the settings
10297 }
10398 catch ( OperationCanceledException )
10499 {
@@ -148,57 +143,3 @@ await File.WriteAllTextAsync(_settingsFilePath, json, ct)
148143 }
149144 }
150145}
151-
152- public interface ISettings
153- {
154- /// <summary>
155- /// FileName where the settings are stored.
156- /// </summary>
157- static abstract string SettingsFileName { get ; }
158-
159- /// <summary>
160- /// Gets the version of the settings schema.
161- /// </summary>
162- int Version { get ; }
163-
164- ISettings Clone ( ) ;
165- }
166-
167- /// <summary>
168- /// CoderConnect settings class that holds the settings for the CoderConnect feature.
169- /// </summary>
170- public class CoderConnectSettings : ISettings
171- {
172- public static string SettingsFileName { get ; } = "coder-connect-settings.json" ;
173- public int Version { get ; set ; }
174- public bool ConnectOnLaunch { get ; set ; }
175-
176- /// <summary>
177- /// CoderConnect current settings version. Increment this when the settings schema changes.
178- /// In future iterations we will be able to handle migrations when the user has
179- /// an older version.
180- /// </summary>
181- private const int VERSION = 1 ;
182-
183- public CoderConnectSettings ( )
184- {
185- Version = VERSION ;
186- ConnectOnLaunch = false ;
187- }
188-
189- public CoderConnectSettings ( int ? version , bool connectOnLogin )
190- {
191- Version = version ?? VERSION ;
192- ConnectOnLaunch = connectOnLogin ;
193- }
194-
195- ISettings ISettings . Clone ( )
196- {
197- return Clone ( ) ;
198- }
199-
200- public CoderConnectSettings Clone ( )
201- {
202- return new CoderConnectSettings ( Version , ConnectOnLaunch ) ;
203- }
204- }
0 commit comments