From 28b6ce49936402eda7479971334370a44a3d8496 Mon Sep 17 00:00:00 2001 From: Gadfly Date: Mon, 2 Sep 2024 10:51:48 +0800 Subject: [PATCH 1/2] 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); From c85293c86a247fe0bde5c3ba28a02755fdc4fb03 Mon Sep 17 00:00:00 2001 From: Gadfly Date: Mon, 2 Sep 2024 12:05:24 +0800 Subject: [PATCH 2/2] enhance: support save RepositoryNode changes instantly --- src/App.axaml.cs | 6 ++++++ src/ViewModels/Preference.cs | 17 +++++++++-------- src/Views/Preference.axaml.cs | 3 --- 3 files changed, 15 insertions(+), 11 deletions(-) diff --git a/src/App.axaml.cs b/src/App.axaml.cs index ffce8a51..c6ae4513 100644 --- a/src/App.axaml.cs +++ b/src/App.axaml.cs @@ -75,6 +75,12 @@ namespace SourceGit AvaloniaXamlLoader.Load(this); var pref = ViewModels.Preference.Instance; + pref.PropertyChanged += (_1, _2) => + { +#pragma warning disable CS4014 + pref.SaveAsync(); +#pragma warning restore CS4014 + }; SetLocale(pref.Locale); SetTheme(pref.Theme, pref.ThemeOverrides); SetFonts(pref.DefaultFontFamily, pref.MonospaceFontFamily, pref.OnlyUseMonoFontInEditor); diff --git a/src/ViewModels/Preference.cs b/src/ViewModels/Preference.cs index afe40cf5..a7aa6532 100644 --- a/src/ViewModels/Preference.cs +++ b/src/ViewModels/Preference.cs @@ -356,6 +356,9 @@ namespace SourceGit.ViewModels return string.Compare(l.Name, r.Name, StringComparison.Ordinal); }); +#pragma warning disable CS4014 + SaveAsync(); +#pragma warning restore CS4014 } public RepositoryNode FindNode(string id) @@ -400,6 +403,9 @@ namespace SourceGit.ViewModels public void RemoveNode(RepositoryNode node) { RemoveNodeRecursive(node, RepositoryNodes); +#pragma warning disable CS4014 + SaveAsync(); +#pragma warning restore CS4014 } public void SortByRenamedNode(RepositoryNode node) @@ -412,6 +418,9 @@ namespace SourceGit.ViewModels return string.Compare(l.Name, r.Name, StringComparison.Ordinal); }); +#pragma warning disable CS4014 + SaveAsync(); +#pragma warning restore CS4014 } public void Save() @@ -446,14 +455,6 @@ namespace SourceGit.ViewModels 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) diff --git a/src/Views/Preference.axaml.cs b/src/Views/Preference.axaml.cs index 6fedd052..25dbfd96 100644 --- a/src/Views/Preference.axaml.cs +++ b/src/Views/Preference.axaml.cs @@ -261,9 +261,6 @@ namespace SourceGit.Views if (sender is CheckBox box) { ViewModels.Preference.Instance.UseSystemWindowFrame = box.IsChecked == true; -#pragma warning disable CS4014 - ViewModels.Preference.Instance.SaveAsync(); -#pragma warning restore CS4014 var dialog = new ConfirmRestart(); App.OpenDialog(dialog);