From c76d14e66d7861adde17a9a74368ced6626cf25c Mon Sep 17 00:00:00 2001 From: Tinyfish Date: Wed, 3 Jul 2024 23:52:17 +0800 Subject: [PATCH] Load settings synchronized and save asynchronized. --- App.axaml.cs | 4 ++-- AppSettings.cs | 23 +++++++++++++++++++---- MainWindow/MainViewModel.cs | 2 +- Tools/JsonFile.cs | 23 +++++++++++++++++++++-- 4 files changed, 43 insertions(+), 9 deletions(-) diff --git a/App.axaml.cs b/App.axaml.cs index cc59acf..f2833ae 100644 --- a/App.axaml.cs +++ b/App.axaml.cs @@ -12,9 +12,9 @@ public override void Initialize() AvaloniaXamlLoader.Load(this); } - public override async void OnFrameworkInitializationCompleted() + public override void OnFrameworkInitializationCompleted() { - await AppSettings.LoadSettings(); + AppSettings.Load(); if (Settings.Locale == "") Settings.Locale = CultureInfo.CurrentCulture.Name; diff --git a/AppSettings.cs b/AppSettings.cs index 92c70ad..9535cab 100644 --- a/AppSettings.cs +++ b/AppSettings.cs @@ -15,19 +15,34 @@ public class AppSettings private static readonly JsonFile _settingsFile = new(SettingsFile); - public static async Task LoadSettings() + public static void Load() { - var settings = await _settingsFile.Load(); + var settings = _settingsFile.Load(); if (settings != null) Settings = settings; } - public static async Task SaveSettings() + public static async Task LoadAsync() + { + var settings = await _settingsFile.LoadAsync(); + if (settings != null) + Settings = settings; + } + + public static void Save() + { + if (Design.IsDesignMode) + return; + + _settingsFile.Save(Settings); + } + + public static async Task SaveAsync() { if (Design.IsDesignMode) return; - await _settingsFile.Save(Settings); + await _settingsFile.SaveAsync(Settings); } public string TriggerKey { get; set; } = "Ctrl"; diff --git a/MainWindow/MainViewModel.cs b/MainWindow/MainViewModel.cs index 505f135..91e91b8 100644 --- a/MainWindow/MainViewModel.cs +++ b/MainWindow/MainViewModel.cs @@ -709,7 +709,7 @@ private async Task CheckAndSaveSettings() Settings.StratagemSets.Add(GetKeyStratagemString()); foreach (var item in StratagemSets) Settings.StratagemSets.Add(item); - await AppSettings.SaveSettings(); + await AppSettings.SaveAsync(); SettingsChanged = false; } diff --git a/Tools/JsonFile.cs b/Tools/JsonFile.cs index 763e41c..d82aaa0 100644 --- a/Tools/JsonFile.cs +++ b/Tools/JsonFile.cs @@ -19,7 +19,16 @@ public JsonFile(string filePath) Encoder = JavaScriptEncoder.Create(UnicodeRanges.All), }; - public async Task Load() + public T? Load() + { + if (!File.Exists(FilePath)) + return null; + + using var fileStream = File.OpenRead(FilePath); + return JsonSerializer.Deserialize(fileStream, JsonSerializerOptions); + } + + public async Task LoadAsync() { if (!File.Exists(FilePath)) return null; @@ -28,7 +37,17 @@ public JsonFile(string filePath) return await JsonSerializer.DeserializeAsync(fileStream, JsonSerializerOptions); } - public async Task Save(T obj) + public void Save(T obj) + { + var dir = Path.GetDirectoryName(FilePath); + if (dir != null && !Directory.Exists(dir)) + Directory.CreateDirectory(dir); + + using var fileStream = File.Create(FilePath); + JsonSerializer.Serialize(fileStream, obj, JsonSerializerOptions); + } + + public async Task SaveAsync(T obj) { var dir = Path.GetDirectoryName(FilePath); if (dir != null && !Directory.Exists(dir))