enhance: save preference.json instantly

This commit is contained in:
Gadfly 2024-09-02 10:51:48 +08:00
parent fefcda7246
commit 28b6ce4993
No known key found for this signature in database
3 changed files with 43 additions and 4 deletions

View file

@ -522,7 +522,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
@ -414,10 +416,44 @@ namespace SourceGit.ViewModels
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);
}
protected override void OnPropertyChanged(PropertyChangedEventArgs e)
{
base.OnPropertyChanged(e);
#pragma warning disable CS4014
SaveAsync();
#pragma warning restore CS4014
}
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 +504,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

@ -138,7 +138,7 @@ namespace SourceGit.Views
if (!GPGFormat.Value.Equals("ssh", StringComparison.Ordinal)) if (!GPGFormat.Value.Equals("ssh", StringComparison.Ordinal))
SetIfChanged(config, $"gpg.{GPGFormat.Value}.program", GPGExecutableFile); SetIfChanged(config, $"gpg.{GPGFormat.Value}.program", GPGExecutableFile);
base.OnClosing(e); base.OnClosing(e);
} }
@ -261,7 +261,9 @@ 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(); #pragma warning disable CS4014
ViewModels.Preference.Instance.SaveAsync();
#pragma warning restore CS4014
var dialog = new ConfirmRestart(); var dialog = new ConfirmRestart();
App.OpenDialog(dialog); App.OpenDialog(dialog);