Skip to content

Commit ced517e

Browse files
committed
PR adjustments
1 parent e7b2491 commit ced517e

File tree

2 files changed

+25
-26
lines changed

2 files changed

+25
-26
lines changed

App/Services/SettingsManager.cs

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Google.Protobuf.WellKnownTypes;
2+
using Serilog;
23
using System;
34
using System.Collections.Generic;
45
using System.IO;
@@ -30,11 +31,6 @@ namespace Coder.Desktop.App.Services;
3031
/// <param name="ct"></param>
3132
/// <returns></returns>
3233
public Task Write(T settings, CancellationToken ct = default);
33-
/// <summary>
34-
/// Returns null if the settings are not cached or not available.
35-
/// </summary>
36-
/// <returns></returns>
37-
public T? GetFromCache();
3834
}
3935

4036
/// <summary>
@@ -80,6 +76,12 @@ public SettingsManager(string? settingsFilePath = null)
8076

8177
public async Task<T> Read(CancellationToken ct = default)
8278
{
79+
if (_cachedSettings is not null)
80+
{
81+
// return cached settings if available
82+
return (T)_cachedSettings.Clone();
83+
}
84+
8385
// try to get the lock with short timeout
8486
if (!await _gate.WaitAsync(LockTimeout, ct).ConfigureAwait(false))
8587
throw new InvalidOperationException(
@@ -145,41 +147,39 @@ await File.WriteAllTextAsync(_settingsFilePath, json, ct)
145147
_gate.Release();
146148
}
147149
}
148-
149-
public T? GetFromCache()
150-
{
151-
return _cachedSettings;
152-
}
153150
}
154151

155152
public interface ISettings
156153
{
157154
/// <summary>
158-
/// Gets the version of the settings schema.
155+
/// FileName where the settings are stored.
159156
/// </summary>
160-
int Version { get; }
157+
static abstract string SettingsFileName { get; }
161158

162159
/// <summary>
163-
/// FileName where the settings are stored.
160+
/// Gets the version of the settings schema.
164161
/// </summary>
165-
static abstract string SettingsFileName { get; }
162+
int Version { get; }
163+
164+
ISettings Clone();
166165
}
167166

168167
/// <summary>
169168
/// CoderConnect settings class that holds the settings for the CoderConnect feature.
170169
/// </summary>
171170
public class CoderConnectSettings : ISettings
172171
{
172+
public static string SettingsFileName { get; } = "coder-connect-settings.json";
173+
public int Version { get; set; }
174+
public bool ConnectOnLaunch { get; set; }
175+
173176
/// <summary>
174-
/// CoderConnect settings version. Increment this when the settings schema changes.
177+
/// CoderConnect current settings version. Increment this when the settings schema changes.
175178
/// In future iterations we will be able to handle migrations when the user has
176179
/// an older version.
177180
/// </summary>
178-
public int Version { get; set; }
179-
public bool ConnectOnLaunch { get; set; }
180-
public static string SettingsFileName { get; } = "coder-connect-settings.json";
181+
private const int VERSION = 1;
181182

182-
private const int VERSION = 1; // Default version for backward compatibility
183183
public CoderConnectSettings()
184184
{
185185
Version = VERSION;
@@ -192,10 +192,13 @@ public CoderConnectSettings(int? version, bool connectOnLogin)
192192
ConnectOnLaunch = connectOnLogin;
193193
}
194194

195+
ISettings ISettings.Clone()
196+
{
197+
return Clone();
198+
}
199+
195200
public CoderConnectSettings Clone()
196201
{
197202
return new CoderConnectSettings(Version, ConnectOnLaunch);
198203
}
199-
200-
201204
}

App/ViewModels/SettingsViewModel.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,7 @@ public SettingsViewModel(ILogger<SettingsViewModel> logger, ISettingsManager<Cod
3131
_logger = logger;
3232
// Application settings are loaded on application startup,
3333
// so we expect the settings to be available immediately.
34-
var settingsCache = settingsManager.GetFromCache();
35-
if (settingsCache is not null)
36-
{
37-
_connectSettings = settingsCache.Clone();
38-
}
34+
var settingsCache = settingsManager.Read();
3935
StartOnLogin = startupManager.IsEnabled();
4036
ConnectOnLaunch = _connectSettings.ConnectOnLaunch;
4137

0 commit comments

Comments
 (0)