Merge pull request #436 from gadfly3173/feat/save-preference

enhance: save preference.json instantly
This commit is contained in:
leo 2024-09-02 12:09:58 +08:00 committed by GitHub
commit 9ee9f921f4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 47 additions and 4 deletions

View file

@ -75,6 +75,12 @@ namespace SourceGit
AvaloniaXamlLoader.Load(this); AvaloniaXamlLoader.Load(this);
var pref = ViewModels.Preference.Instance; var pref = ViewModels.Preference.Instance;
pref.PropertyChanged += (_1, _2) =>
{
#pragma warning disable CS4014
pref.SaveAsync();
#pragma warning restore CS4014
};
SetLocale(pref.Locale); SetLocale(pref.Locale);
SetTheme(pref.Theme, pref.ThemeOverrides); SetTheme(pref.Theme, pref.ThemeOverrides);
SetFonts(pref.DefaultFontFamily, pref.MonospaceFontFamily, pref.OnlyUseMonoFontInEditor); SetFonts(pref.DefaultFontFamily, pref.MonospaceFontFamily, pref.OnlyUseMonoFontInEditor);
@ -522,7 +528,6 @@ namespace SourceGit
var pref = ViewModels.Preference.Instance; var pref = ViewModels.Preference.Instance;
if (pref.ShouldCheck4UpdateOnStartup()) if (pref.ShouldCheck4UpdateOnStartup())
{ {
pref.Save();
Check4Update(); Check4Update();
} }
} }

View file

@ -1,9 +1,11 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel;
using System.IO; using System.IO;
using System.Text.Json; using System.Text.Json;
using System.Text.Json.Serialization; using System.Text.Json.Serialization;
using System.Threading;
using System.Threading.Tasks;
using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.ComponentModel;
namespace SourceGit.ViewModels namespace SourceGit.ViewModels
@ -354,6 +356,9 @@ namespace SourceGit.ViewModels
return string.Compare(l.Name, r.Name, StringComparison.Ordinal); return string.Compare(l.Name, r.Name, StringComparison.Ordinal);
}); });
#pragma warning disable CS4014
SaveAsync();
#pragma warning restore CS4014
} }
public RepositoryNode FindNode(string id) public RepositoryNode FindNode(string id)
@ -398,6 +403,9 @@ namespace SourceGit.ViewModels
public void RemoveNode(RepositoryNode node) public void RemoveNode(RepositoryNode node)
{ {
RemoveNodeRecursive(node, RepositoryNodes); RemoveNodeRecursive(node, RepositoryNodes);
#pragma warning disable CS4014
SaveAsync();
#pragma warning restore CS4014
} }
public void SortByRenamedNode(RepositoryNode node) public void SortByRenamedNode(RepositoryNode node)
@ -410,14 +418,43 @@ namespace SourceGit.ViewModels
return string.Compare(l.Name, r.Name, StringComparison.Ordinal); return string.Compare(l.Name, r.Name, StringComparison.Ordinal);
}); });
#pragma warning disable CS4014
SaveAsync();
#pragma warning restore CS4014
} }
public void Save() public void Save()
{ {
lock (_saveCtsLock)
{
_saveCts.Cancel();
_saveCts = new CancellationTokenSource();
}
var data = JsonSerializer.Serialize(this, JsonCodeGen.Default.Preference); var data = JsonSerializer.Serialize(this, JsonCodeGen.Default.Preference);
File.WriteAllText(_savePath, data); 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);
}
private RepositoryNode FindNodeRecursive(string id, List<RepositoryNode> collection) private RepositoryNode FindNodeRecursive(string id, List<RepositoryNode> collection)
{ {
foreach (var node in collection) foreach (var node in collection)
@ -468,6 +505,8 @@ namespace SourceGit.ViewModels
private static Preference _instance = null; private static Preference _instance = null;
private static bool _isLoading = false; private static bool _isLoading = false;
private static readonly string _savePath = Path.Combine(Native.OS.DataDir, "preference.json"); 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 _locale = "en_US";
private string _theme = "Default"; private string _theme = "Default";

View file

@ -261,7 +261,6 @@ namespace SourceGit.Views
if (sender is CheckBox box) if (sender is CheckBox box)
{ {
ViewModels.Preference.Instance.UseSystemWindowFrame = box.IsChecked == true; ViewModels.Preference.Instance.UseSystemWindowFrame = box.IsChecked == true;
ViewModels.Preference.Instance.Save();
var dialog = new ConfirmRestart(); var dialog = new ConfirmRestart();
App.OpenDialog(dialog); App.OpenDialog(dialog);