Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitc21072f

Browse files
committed
created Settings class to handle versioning
1 parent07ec725 commitc21072f

File tree

2 files changed

+37
-10
lines changed

2 files changed

+37
-10
lines changed

‎App/Services/SettingsManager.cs

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
usingSystem.Collections.Generic;
33
usingSystem.IO;
44
usingSystem.Text.Json;
5+
usingSystem.Text.Json.Serialization;
56

67
namespaceCoder.Desktop.App.Services;
78

@@ -28,10 +29,10 @@ public interface ISettingsManager
2829
publicsealedclassSettingsManager:ISettingsManager
2930
{
3031
privatereadonlystring_settingsFilePath;
32+
privateSettings_settings;
3133
privatereadonlystring_fileName="app-settings.json";
3234
privatereadonlystring_appName="CoderDesktop";
3335
privatereadonlyobject_lock=new();
34-
privateDictionary<string,JsonElement>_cache;
3536

3637
publicconststringConnectOnLaunchKey="ConnectOnLaunch";
3738
publicconststringStartOnLoginKey="StartOnLogin";
@@ -87,9 +88,12 @@ public SettingsManager(string? settingsFilePath = null)
8788
// Create the settings file if it doesn't exist
8889
stringemptyJson=JsonSerializer.Serialize(new{});
8990
File.WriteAllText(_settingsFilePath,emptyJson);
91+
_settings=new();
92+
}
93+
else
94+
{
95+
_settings=Load();
9096
}
91-
92-
_cache=Load();
9397
}
9498

9599
privatevoidSave(stringname,boolvalue)
@@ -105,12 +109,12 @@ private void Save(string name, bool value)
105109
FileShare.None);
106110

107111
// Ensure cache is loaded before saving
108-
varcurrentCache=JsonSerializer.Deserialize<Dictionary<string,JsonElement>>(fs)??[];
109-
_cache=currentCache;
110-
_cache[name]=JsonSerializer.SerializeToElement(value);
112+
varfreshCache=JsonSerializer.Deserialize<Settings>(fs)??new();
113+
_settings=freshCache;
114+
_settings.Options[name]=JsonSerializer.SerializeToElement(value);
111115
fs.Position=0;// Reset stream position to the beginning before writing
112116

113-
JsonSerializer.Serialize(fs,_cache,newJsonSerializerOptions{WriteIndented=true});
117+
JsonSerializer.Serialize(fs,_settings,newJsonSerializerOptions{WriteIndented=true});
114118

115119
// This ensures the file is truncated to the new length
116120
// if the new content is shorter than the old content
@@ -127,7 +131,7 @@ private bool Read(string name, bool defaultValue)
127131
{
128132
lock(_lock)
129133
{
130-
if(_cache.TryGetValue(name,outvarelement))
134+
if(_settings.Options.TryGetValue(name,outvarelement))
131135
{
132136
try
133137
{
@@ -143,16 +147,38 @@ private bool Read(string name, bool defaultValue)
143147
}
144148
}
145149

146-
privateDictionary<string,JsonElement>Load()
150+
privateSettingsLoad()
147151
{
148152
try
149153
{
150154
usingvarfs=File.OpenRead(_settingsFilePath);
151-
returnJsonSerializer.Deserialize<Dictionary<string,JsonElement>>(fs)??new();
155+
returnJsonSerializer.Deserialize<Settings>(fs)??new(null,newDictionary<string,JsonElement>());
152156
}
153157
catch(Exceptionex)
154158
{
155159
thrownewInvalidOperationException($"Failed to load settings from{_settingsFilePath}. The file may be corrupted or malformed. Exception:{ex.Message}");
156160
}
157161
}
162+
163+
[JsonSerializable(typeof(Settings))]
164+
privateclassSettings
165+
{
166+
/// <summary>
167+
/// User settings version. Increment this when the settings schema changes.
168+
/// In future iterations we will be able to handle migrations when the user has
169+
/// an older version.
170+
/// </summary>
171+
publicintVersion{get;set;}=1;
172+
publicDictionary<string,JsonElement>Options{get;set;}
173+
publicSettings()
174+
{
175+
Options=newDictionary<string,JsonElement>();
176+
}
177+
178+
publicSettings(int?version,Dictionary<string,JsonElement>options)
179+
{
180+
Version=version??Version;
181+
Options=options;
182+
}
183+
}
158184
}

‎App/Views/Pages/SettingsMainPage.xaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
>
5252
<ToggleSwitchIsOn="{x:Bind ViewModel.ConnectOnLaunch, Mode=TwoWay}" />
5353
</controls:SettingsCard>
54+
5455
</StackPanel>
5556
</Grid>
5657
</ScrollViewer>

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp