mirror of
https://github.com/sourcegit-scm/sourcegit.git
synced 2024-12-24 20:57:19 -08:00
code_review: PR #436
It's ok to call `Save()` multiple times after property of `ViewModels.Preference` changed immediately. If we delay the saving operation 3 seconds after the property changed event, we reduce the times calling `Save`, but the issue metioned in #436 may still exists.
This commit is contained in:
parent
9ee9f921f4
commit
9fe8ffcea9
2 changed files with 18 additions and 54 deletions
|
@ -75,12 +75,8 @@ namespace SourceGit
|
||||||
AvaloniaXamlLoader.Load(this);
|
AvaloniaXamlLoader.Load(this);
|
||||||
|
|
||||||
var pref = ViewModels.Preference.Instance;
|
var pref = ViewModels.Preference.Instance;
|
||||||
pref.PropertyChanged += (_1, _2) =>
|
pref.PropertyChanged += (_1, _2) => pref.Save();
|
||||||
{
|
|
||||||
#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);
|
||||||
|
@ -527,9 +523,7 @@ namespace SourceGit
|
||||||
|
|
||||||
var pref = ViewModels.Preference.Instance;
|
var pref = ViewModels.Preference.Instance;
|
||||||
if (pref.ShouldCheck4UpdateOnStartup())
|
if (pref.ShouldCheck4UpdateOnStartup())
|
||||||
{
|
|
||||||
Check4Update();
|
Check4Update();
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private ViewModels.Launcher _launcher = null;
|
private ViewModels.Launcher _launcher = null;
|
||||||
|
|
|
@ -1,11 +1,9 @@
|
||||||
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
|
||||||
|
@ -155,9 +153,9 @@ namespace SourceGit.ViewModels
|
||||||
|
|
||||||
public string IgnoreUpdateTag
|
public string IgnoreUpdateTag
|
||||||
{
|
{
|
||||||
get;
|
get => _ignoreUpdateTag;
|
||||||
set;
|
set => SetProperty(ref _ignoreUpdateTag, value);
|
||||||
} = string.Empty;
|
}
|
||||||
|
|
||||||
public bool ShowTagsAsTree
|
public bool ShowTagsAsTree
|
||||||
{
|
{
|
||||||
|
@ -320,9 +318,9 @@ namespace SourceGit.ViewModels
|
||||||
|
|
||||||
public double LastCheckUpdateTime
|
public double LastCheckUpdateTime
|
||||||
{
|
{
|
||||||
get;
|
get => _lastCheckUpdateTime;
|
||||||
set;
|
set => SetProperty(ref _lastCheckUpdateTime, value);
|
||||||
} = 0;
|
}
|
||||||
|
|
||||||
public bool IsGitConfigured()
|
public bool IsGitConfigured()
|
||||||
{
|
{
|
||||||
|
@ -356,9 +354,8 @@ 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();
|
Save();
|
||||||
#pragma warning restore CS4014
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public RepositoryNode FindNode(string id)
|
public RepositoryNode FindNode(string id)
|
||||||
|
@ -398,14 +395,13 @@ namespace SourceGit.ViewModels
|
||||||
|
|
||||||
RemoveNode(node);
|
RemoveNode(node);
|
||||||
AddNode(node, to);
|
AddNode(node, to);
|
||||||
|
Save();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void RemoveNode(RepositoryNode node)
|
public void RemoveNode(RepositoryNode node)
|
||||||
{
|
{
|
||||||
RemoveNodeRecursive(node, RepositoryNodes);
|
RemoveNodeRecursive(node, RepositoryNodes);
|
||||||
#pragma warning disable CS4014
|
Save();
|
||||||
SaveAsync();
|
|
||||||
#pragma warning restore CS4014
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void SortByRenamedNode(RepositoryNode node)
|
public void SortByRenamedNode(RepositoryNode node)
|
||||||
|
@ -418,43 +414,16 @@ 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();
|
Save();
|
||||||
#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)
|
||||||
|
@ -505,8 +474,6 @@ 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";
|
||||||
|
@ -522,7 +489,10 @@ namespace SourceGit.ViewModels
|
||||||
private int _subjectGuideLength = 50;
|
private int _subjectGuideLength = 50;
|
||||||
private bool _restoreTabs = false;
|
private bool _restoreTabs = false;
|
||||||
private bool _useFixedTabWidth = true;
|
private bool _useFixedTabWidth = true;
|
||||||
|
|
||||||
private bool _check4UpdatesOnStartup = true;
|
private bool _check4UpdatesOnStartup = true;
|
||||||
|
private double _lastCheckUpdateTime = 0;
|
||||||
|
private string _ignoreUpdateTag = string.Empty;
|
||||||
|
|
||||||
private bool _showTagsAsTree = false;
|
private bool _showTagsAsTree = false;
|
||||||
private bool _useTwoColumnsLayoutInHistories = false;
|
private bool _useTwoColumnsLayoutInHistories = false;
|
||||||
|
|
Loading…
Reference in a new issue