From 28b6ce49936402eda7479971334370a44a3d8496 Mon Sep 17 00:00:00 2001 From: Gadfly Date: Mon, 2 Sep 2024 10:51:48 +0800 Subject: [PATCH] enhance: save preference.json instantly --- src/App.axaml.cs | 1 - src/ViewModels/Preference.cs | 40 ++++++++++++++++++++++++++++++++++- src/Views/Preference.axaml.cs | 6 ++++-- 3 files changed, 43 insertions(+), 4 deletions(-) diff --git a/src/App.axaml.cs b/src/App.axaml.cs index 98d671d3..ffce8a51 100644 --- a/src/App.axaml.cs +++ b/src/App.axaml.cs @@ -522,7 +522,6 @@ namespace SourceGit var pref = ViewModels.Preference.Instance; if (pref.ShouldCheck4UpdateOnStartup()) { - pref.Save(); Check4Update(); } } diff --git a/src/ViewModels/Preference.cs b/src/ViewModels/Preference.cs index ed3c0724..afe40cf5 100644 --- a/src/ViewModels/Preference.cs +++ b/src/ViewModels/Preference.cs @@ -1,9 +1,11 @@ using System; using System.Collections.Generic; +using System.ComponentModel; using System.IO; using System.Text.Json; using System.Text.Json.Serialization; - +using System.Threading; +using System.Threading.Tasks; using CommunityToolkit.Mvvm.ComponentModel; namespace SourceGit.ViewModels @@ -414,10 +416,44 @@ namespace SourceGit.ViewModels public void Save() { + lock (_saveCtsLock) + { + _saveCts.Cancel(); + _saveCts = new CancellationTokenSource(); + } var data = JsonSerializer.Serialize(this, JsonCodeGen.Default.Preference); File.WriteAllText(_savePath, data); } + public async Task SaveAsync() + { + lock (_saveCtsLock) + { + _saveCts.Cancel(); + _saveCts = new CancellationTokenSource(); + } + + try + { + await Task.Delay(3000, _saveCts.Token); + } + catch (TaskCanceledException) + { + return; + } + + var data = JsonSerializer.Serialize(this, JsonCodeGen.Default.Preference); + await File.WriteAllTextAsync(_savePath, data); + } + + protected override void OnPropertyChanged(PropertyChangedEventArgs e) + { + base.OnPropertyChanged(e); +#pragma warning disable CS4014 + SaveAsync(); +#pragma warning restore CS4014 + } + private RepositoryNode FindNodeRecursive(string id, List collection) { foreach (var node in collection) @@ -468,6 +504,8 @@ namespace SourceGit.ViewModels private static Preference _instance = null; private static bool _isLoading = false; private static readonly string _savePath = Path.Combine(Native.OS.DataDir, "preference.json"); + private static CancellationTokenSource _saveCts = new(); + private static readonly object _saveCtsLock = new(); private string _locale = "en_US"; private string _theme = "Default"; diff --git a/src/Views/Preference.axaml.cs b/src/Views/Preference.axaml.cs index 2621bcaf..6fedd052 100644 --- a/src/Views/Preference.axaml.cs +++ b/src/Views/Preference.axaml.cs @@ -138,7 +138,7 @@ namespace SourceGit.Views if (!GPGFormat.Value.Equals("ssh", StringComparison.Ordinal)) SetIfChanged(config, $"gpg.{GPGFormat.Value}.program", GPGExecutableFile); - + base.OnClosing(e); } @@ -261,7 +261,9 @@ namespace SourceGit.Views if (sender is CheckBox box) { ViewModels.Preference.Instance.UseSystemWindowFrame = box.IsChecked == true; - ViewModels.Preference.Instance.Save(); +#pragma warning disable CS4014 + ViewModels.Preference.Instance.SaveAsync(); +#pragma warning restore CS4014 var dialog = new ConfirmRestart(); App.OpenDialog(dialog);