Skip to content

Commit dba9ff6

Browse files
committed
🐛 fix(Backups): Restore settings erroneous write default settings
1 parent 3eab637 commit dba9ff6

File tree

3 files changed

+25
-10
lines changed

3 files changed

+25
-10
lines changed

src/SwashbucklerDiary.Rcl/Components/DialogComponents/RadioDialog.razor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public partial class RadioDialog<TItem, TItemValue> : DialogComponentBase
2020
public string? Title { get; set; }
2121

2222
[Parameter, EditorRequired]
23-
public ICollection<TItem> Items { get; set; } = default!;
23+
public IEnumerable<TItem> Items { get; set; } = default!;
2424

2525
[Parameter]
2626
public Func<TItem, string>? ItemText { get; set; }

src/SwashbucklerDiary.Rcl/Layout/MainLayoutBase.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,9 @@ protected async void HandleSettingsChanged()
9292
{
9393
var theme = SettingService.Get(it => it.Theme);
9494
await ThemeService.SetThemeAsync((Shared.Theme)theme);
95-
var language = SettingService.Get(it => it.Language);
96-
I18n.SetCulture(language);
95+
// A bug, it will cause the language selection RadioDialog to fail to select
96+
//var language = SettingService.Get(it => it.Language);
97+
//I18n.SetCulture(language);
9798
}
9899
}
99100
}

src/SwashbucklerDiary.Rcl/Services/SettingService/ISettingService.cs

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ public Setting SaveSettingsToObject(Func<string, bool>? func = null)
146146
var obj = new Setting();
147147
var properties = obj.GetType().GetProperties();
148148

149-
var method = this.GetType()?.GetMethod(nameof(Get), [typeof(string)])
149+
var getMethod = this.GetType()?.GetMethod(nameof(Get), [typeof(string)])
150150
?? throw new InvalidOperationException($"{nameof(Get)} does not exist");
151151

152152
if (func is not null)
@@ -158,8 +158,8 @@ public Setting SaveSettingsToObject(Func<string, bool>? func = null)
158158
{
159159
if (property.CanWrite)
160160
{
161-
var genericMethod = method.MakeGenericMethod(property.PropertyType);
162-
var value = genericMethod.Invoke(this, [property.Name]);
161+
var getGenericMethod = getMethod.MakeGenericMethod(property.PropertyType);
162+
var value = getGenericMethod.Invoke(this, [property.Name]);
163163
property.SetValue(obj, value);
164164
}
165165
}
@@ -170,29 +170,43 @@ public Setting SaveSettingsToObject(Func<string, bool>? func = null)
170170
public async Task SetSettingsFromObjectAsync(Setting obj, Func<string, bool>? func = null)
171171
{
172172
var properties = obj.GetType().GetProperties();
173-
var method = this.GetType()?.GetMethod(nameof(SetAsync))
173+
var setAsyncMethod = this.GetType()?.GetMethod(nameof(SetAsync))
174174
?? throw new InvalidOperationException($"{nameof(SetAsync)} does not exist");
175+
var getMethod = this.GetType()?.GetMethod(nameof(Get), [typeof(string)])
176+
?? throw new InvalidOperationException($"{nameof(Get)} does not exist");
175177

176178
if (func is not null)
177179
{
178180
properties = properties.Where(it => func.Invoke(it.Name)).ToArray();
179181
}
180182

183+
var removeKeys = new List<string>();
181184
foreach (var property in properties)
182185
{
183186
if (property.CanRead && DefalutSettings.TryGetValue(property.Name, out var defaultValue))
184187
{
185188
var value = property.GetValue(obj);
186-
if (value == defaultValue)
189+
var getGenericMethod = getMethod.MakeGenericMethod(property.PropertyType);
190+
var currentValue = getGenericMethod.Invoke(this, [property.Name]);
191+
192+
if (Object.Equals(value, currentValue))
187193
{
188194
continue;
189195
}
190196

191-
var genericMethod = method.MakeGenericMethod(property.PropertyType);
192-
await (Task)genericMethod.Invoke(this, [property.Name, value])!;
197+
if (Object.Equals(value, defaultValue))
198+
{
199+
removeKeys.Add(property.Name);
200+
continue;
201+
}
202+
203+
var setAsyncGenericMethod = setAsyncMethod.MakeGenericMethod(property.PropertyType);
204+
await (Task)setAsyncGenericMethod.Invoke(this, [property.Name, value])!;
193205
}
194206
}
195207

208+
await RemoveAsync(removeKeys);
209+
196210
SettingsChanged?.Invoke();
197211
}
198212
}

0 commit comments

Comments
 (0)