1
- using Google . Protobuf . WellKnownTypes ;
2
- using Serilog ;
3
1
using System ;
4
- using System . Collections . Generic ;
5
2
using System . IO ;
6
3
using System . Text . Json ;
7
- using System . Text . Json . Serialization ;
8
4
using System . Threading ;
9
5
using System . Threading . Tasks ;
10
- using System . Xml . Linq ;
6
+ using Coder . Desktop . App . Models ;
11
7
12
8
namespace Coder . Desktop . App . Services ;
13
9
14
10
/// <summary>
15
11
/// Settings contract exposing properties for app settings.
16
12
/// </summary>
17
- public interface ISettingsManager < T > where T : ISettings , new ( )
13
+ public interface ISettingsManager < T > where T : ISettings < T > , new ( )
18
14
{
19
15
/// <summary>
20
16
/// Reads the settings from the file system.
@@ -23,26 +19,25 @@ namespace Coder.Desktop.App.Services;
23
19
/// </summary>
24
20
/// <param name="ct"></param>
25
21
/// <returns></returns>
26
- public Task < T > Read ( CancellationToken ct = default ) ;
22
+ Task < T > Read ( CancellationToken ct = default ) ;
27
23
/// <summary>
28
24
/// Writes the settings to the file system.
29
25
/// </summary>
30
26
/// <param name="settings">Object containing the settings.</param>
31
27
/// <param name="ct"></param>
32
28
/// <returns></returns>
33
- public Task Write ( T settings , CancellationToken ct = default ) ;
29
+ Task Write ( T settings , CancellationToken ct = default ) ;
34
30
}
35
31
36
32
/// <summary>
37
33
/// Implemention of <see cref="ISettingsManager"/> that persists settings to a JSON file
38
34
/// located in the user's local application data folder.
39
35
/// </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 ( )
41
37
{
42
38
private readonly string _settingsFilePath ;
43
39
private readonly string _appName = "CoderDesktop" ;
44
40
private string _fileName ;
45
- private readonly object _lock = new ( ) ;
46
41
47
42
private T ? _cachedSettings ;
48
43
@@ -79,7 +74,7 @@ public async Task<T> Read(CancellationToken ct = default)
79
74
if ( _cachedSettings is not null )
80
75
{
81
76
// return cached settings if available
82
- return ( T ) _cachedSettings . Clone ( ) ;
77
+ return _cachedSettings . Clone ( ) ;
83
78
}
84
79
85
80
// try to get the lock with short timeout
@@ -98,7 +93,7 @@ public async Task<T> Read(CancellationToken ct = default)
98
93
// deserialize; fall back to default(T) if empty or malformed
99
94
var result = JsonSerializer . Deserialize < T > ( json ) ! ;
100
95
_cachedSettings = result ;
101
- return result ;
96
+ return _cachedSettings . Clone ( ) ; // return a fresh instance of the settings
102
97
}
103
98
catch ( OperationCanceledException )
104
99
{
@@ -148,57 +143,3 @@ await File.WriteAllTextAsync(_settingsFilePath, json, ct)
148
143
}
149
144
}
150
145
}
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