code_style: change static methods of ViewModels.Preference to member function

This commit is contained in:
leo 2024-07-22 15:34:31 +08:00
parent 1e0a2ab5f7
commit 720b2b65f3
No known key found for this signature in database
13 changed files with 65 additions and 71 deletions

View file

@ -520,7 +520,7 @@ namespace SourceGit
desktop.MainWindow = new Views.Launcher() { DataContext = _launcher }; desktop.MainWindow = new Views.Launcher() { DataContext = _launcher };
var pref = ViewModels.Preference.Instance; var pref = ViewModels.Preference.Instance;
if (pref.ShouldCheck4UpdateOnStartup) if (pref.ShouldCheck4UpdateOnStartup())
{ {
pref.Save(); pref.Save();
Check4Update(); Check4Update();

View file

@ -130,7 +130,7 @@ namespace SourceGit.ViewModels
CallUIThread(() => CallUIThread(() =>
{ {
var normalizedPath = path.Replace("\\", "/"); var normalizedPath = path.Replace("\\", "/");
var node = Preference.FindOrAddNodeByRepositoryPath(normalizedPath, null, true); var node = Preference.Instance.FindOrAddNodeByRepositoryPath(normalizedPath, null, true);
_launcher.OpenRepositoryInTab(node, _page); _launcher.OpenRepositoryInTab(node, _page);
}); });

View file

@ -21,7 +21,7 @@ namespace SourceGit.ViewModels
public override Task<bool> Sure() public override Task<bool> Sure()
{ {
Preference.AddNode(new RepositoryNode() Preference.Instance.AddNode(new RepositoryNode()
{ {
Id = Guid.NewGuid().ToString(), Id = Guid.NewGuid().ToString(),
Name = _name, Name = _name,

View file

@ -18,7 +18,7 @@ namespace SourceGit.ViewModels
public override Task<bool> Sure() public override Task<bool> Sure()
{ {
Preference.RemoveNode(_node); Preference.Instance.RemoveNode(_node);
return null; return null;
} }

View file

@ -48,7 +48,7 @@ namespace SourceGit.ViewModels
_node.Bookmark = _bookmark; _node.Bookmark = _bookmark;
if (needSort) if (needSort)
Preference.SortByRenamedNode(_node); Preference.Instance.SortByRenamedNode(_node);
return null; return null;
} }

View file

@ -31,7 +31,7 @@ namespace SourceGit.ViewModels
CallUIThread(() => CallUIThread(() =>
{ {
var normalizedPath = _targetPath.Replace("\\", "/"); var normalizedPath = _targetPath.Replace("\\", "/");
Preference.FindOrAddNodeByRepositoryPath(normalizedPath, _parentNode, true); Preference.Instance.FindOrAddNodeByRepositoryPath(normalizedPath, _parentNode, true);
}); });
return true; return true;

View file

@ -1,11 +1,9 @@
using System; using System;
using System.IO; using System.IO;
using Avalonia;
using Avalonia.Collections; using Avalonia.Collections;
using Avalonia.Controls; using Avalonia.Controls;
using Avalonia.Input; using Avalonia.Input;
using Avalonia.Media;
using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.ComponentModel;
@ -34,6 +32,7 @@ namespace SourceGit.ViewModels
Pages = new AvaloniaList<LauncherPage>(); Pages = new AvaloniaList<LauncherPage>();
AddNewTab(); AddNewTab();
var pref = Preference.Instance;
if (!string.IsNullOrEmpty(startupRepo)) if (!string.IsNullOrEmpty(startupRepo))
{ {
var root = new Commands.QueryRepositoryRootPath(startupRepo).Result(); var root = new Commands.QueryRepositoryRootPath(startupRepo).Result();
@ -48,14 +47,14 @@ namespace SourceGit.ViewModels
} }
var normalized = root.Replace("\\", "/"); var normalized = root.Replace("\\", "/");
var node = Preference.FindOrAddNodeByRepositoryPath(normalized, null, false); var node = pref.FindOrAddNodeByRepositoryPath(normalized, null, false);
OpenRepositoryInTab(node, null); OpenRepositoryInTab(node, null);
} }
else if (Preference.Instance.RestoreTabs) else if (pref.RestoreTabs)
{ {
foreach (var id in Preference.Instance.OpenedTabs) foreach (var id in pref.OpenedTabs)
{ {
var node = Preference.FindNode(id); var node = pref.FindNode(id);
if (node == null) if (node == null)
{ {
node = new RepositoryNode() node = new RepositoryNode()
@ -70,7 +69,7 @@ namespace SourceGit.ViewModels
OpenRepositoryInTab(node, null); OpenRepositoryInTab(node, null);
} }
var lastActiveIdx = Preference.Instance.LastActiveTabIdx; var lastActiveIdx = pref.LastActiveTabIdx;
if (lastActiveIdx >= 0 && lastActiveIdx < Pages.Count) if (lastActiveIdx >= 0 && lastActiveIdx < Pages.Count)
ActivePage = Pages[lastActiveIdx]; ActivePage = Pages[lastActiveIdx];
} }

View file

@ -43,7 +43,7 @@ namespace SourceGit.ViewModels
if (_instance.MonospaceFont == null) if (_instance.MonospaceFont == null)
_instance.MonospaceFont = new FontFamily("fonts:SourceGit#JetBrains Mono"); _instance.MonospaceFont = new FontFamily("fonts:SourceGit#JetBrains Mono");
if (!_instance.IsGitConfigured) if (!_instance.IsGitConfigured())
_instance.GitInstallPath = Native.OS.FindGitExecutable(); _instance.GitInstallPath = Native.OS.FindGitExecutable();
return _instance; return _instance;
@ -213,12 +213,6 @@ namespace SourceGit.ViewModels
set => SetProperty(ref _commitChangeViewMode, value); set => SetProperty(ref _commitChangeViewMode, value);
} }
[JsonIgnore]
public bool IsGitConfigured
{
get => !string.IsNullOrEmpty(GitInstallPath) && File.Exists(GitInstallPath);
}
public string GitInstallPath public string GitInstallPath
{ {
get => Native.OS.GitExecutable; get => Native.OS.GitExecutable;
@ -326,10 +320,13 @@ namespace SourceGit.ViewModels
set; set;
} = 0; } = 0;
[JsonIgnore] public bool IsGitConfigured()
public bool ShouldCheck4UpdateOnStartup
{ {
get var path = GitInstallPath;
return !string.IsNullOrEmpty(path) && File.Exists(path);
}
public bool ShouldCheck4UpdateOnStartup()
{ {
if (!_check4UpdatesOnStartup) if (!_check4UpdatesOnStartup)
return false; return false;
@ -343,11 +340,10 @@ namespace SourceGit.ViewModels
LastCheckUpdateTime = now.Subtract(DateTime.UnixEpoch.ToLocalTime()).TotalSeconds; LastCheckUpdateTime = now.Subtract(DateTime.UnixEpoch.ToLocalTime()).TotalSeconds;
return true; return true;
} }
}
public static void AddNode(RepositoryNode node, RepositoryNode to = null) public void AddNode(RepositoryNode node, RepositoryNode to = null)
{ {
var collection = to == null ? _instance._repositoryNodes : to.SubNodes; var collection = to == null ? _repositoryNodes : to.SubNodes;
var list = new List<RepositoryNode>(); var list = new List<RepositoryNode>();
list.AddRange(collection); list.AddRange(collection);
list.Add(node); list.Add(node);
@ -364,14 +360,14 @@ namespace SourceGit.ViewModels
collection.Add(one); collection.Add(one);
} }
public static RepositoryNode FindNode(string id) public RepositoryNode FindNode(string id)
{ {
return FindNodeRecursive(id, _instance.RepositoryNodes); return FindNodeRecursive(id, RepositoryNodes);
} }
public static RepositoryNode FindOrAddNodeByRepositoryPath(string repo, RepositoryNode parent, bool shouldMoveNode) public RepositoryNode FindOrAddNodeByRepositoryPath(string repo, RepositoryNode parent, bool shouldMoveNode)
{ {
var node = FindNodeRecursive(repo, _instance.RepositoryNodes); var node = FindNodeRecursive(repo, RepositoryNodes);
if (node == null) if (node == null)
{ {
node = new RepositoryNode() node = new RepositoryNode()
@ -392,9 +388,9 @@ namespace SourceGit.ViewModels
return node; return node;
} }
public static void MoveNode(RepositoryNode node, RepositoryNode to = null) public void MoveNode(RepositoryNode node, RepositoryNode to = null)
{ {
if (to == null && _instance._repositoryNodes.Contains(node)) if (to == null && _repositoryNodes.Contains(node))
return; return;
if (to != null && to.SubNodes.Contains(node)) if (to != null && to.SubNodes.Contains(node))
return; return;
@ -403,14 +399,14 @@ namespace SourceGit.ViewModels
AddNode(node, to); AddNode(node, to);
} }
public static void RemoveNode(RepositoryNode node) public void RemoveNode(RepositoryNode node)
{ {
RemoveNodeRecursive(node, _instance._repositoryNodes); RemoveNodeRecursive(node, _repositoryNodes);
} }
public static void SortByRenamedNode(RepositoryNode node) public void SortByRenamedNode(RepositoryNode node)
{ {
var container = FindNodeContainer(node, _instance._repositoryNodes); var container = FindNodeContainer(node, _repositoryNodes);
if (container == null) if (container == null)
return; return;
@ -435,7 +431,7 @@ namespace SourceGit.ViewModels
File.WriteAllText(_savePath, data); File.WriteAllText(_savePath, data);
} }
private static RepositoryNode FindNodeRecursive(string id, AvaloniaList<RepositoryNode> collection) private RepositoryNode FindNodeRecursive(string id, AvaloniaList<RepositoryNode> collection)
{ {
foreach (var node in collection) foreach (var node in collection)
{ {
@ -450,7 +446,7 @@ namespace SourceGit.ViewModels
return null; return null;
} }
private static AvaloniaList<RepositoryNode> FindNodeContainer(RepositoryNode node, AvaloniaList<RepositoryNode> collection) private AvaloniaList<RepositoryNode> FindNodeContainer(RepositoryNode node, AvaloniaList<RepositoryNode> collection)
{ {
foreach (var sub in collection) foreach (var sub in collection)
{ {
@ -465,7 +461,7 @@ namespace SourceGit.ViewModels
return null; return null;
} }
private static bool RemoveNodeRecursive(RepositoryNode node, AvaloniaList<RepositoryNode> collection) private bool RemoveNodeRecursive(RepositoryNode node, AvaloniaList<RepositoryNode> collection)
{ {
if (collection.Contains(node)) if (collection.Contains(node))
{ {

View file

@ -965,7 +965,7 @@ namespace SourceGit.ViewModels
var root = Path.GetFullPath(Path.Combine(_fullpath, submodule)); var root = Path.GetFullPath(Path.Combine(_fullpath, submodule));
var normalizedPath = root.Replace("\\", "/"); var normalizedPath = root.Replace("\\", "/");
var node = Preference.FindNode(normalizedPath); var node = Preference.Instance.FindNode(normalizedPath);
if (node == null) if (node == null)
{ {
node = new RepositoryNode() node = new RepositoryNode()
@ -996,7 +996,7 @@ namespace SourceGit.ViewModels
public void OpenWorktree(Models.Worktree worktree) public void OpenWorktree(Models.Worktree worktree)
{ {
var node = Preference.FindNode(worktree.FullPath); var node = Preference.Instance.FindNode(worktree.FullPath);
if (node == null) if (node == null)
{ {
node = new RepositoryNode() node = new RepositoryNode()

View file

@ -1,4 +1,5 @@
using System; using System;
using Avalonia; using Avalonia;
using Avalonia.Collections; using Avalonia.Collections;
using Avalonia.Controls; using Avalonia.Controls;
@ -23,13 +24,13 @@ namespace SourceGit.ViewModels
set set
{ {
if (SetProperty(ref _searchFilter, value)) if (SetProperty(ref _searchFilter, value))
Referesh(); Refresh();
} }
} }
public void InitRepository(string path, RepositoryNode parent) public void InitRepository(string path, RepositoryNode parent)
{ {
if (!Preference.Instance.IsGitConfigured) if (!Preference.Instance.IsGitConfigured())
{ {
App.RaiseException(PopupHost.Active.GetId(), App.Text("NotConfigured")); App.RaiseException(PopupHost.Active.GetId(), App.Text("NotConfigured"));
return; return;
@ -43,7 +44,7 @@ namespace SourceGit.ViewModels
public void Clone() public void Clone()
{ {
if (!Preference.Instance.IsGitConfigured) if (!Preference.Instance.IsGitConfigured())
{ {
App.RaiseException(string.Empty, App.Text("NotConfigured")); App.RaiseException(string.Empty, App.Text("NotConfigured"));
return; return;
@ -58,7 +59,7 @@ namespace SourceGit.ViewModels
public void OpenTerminal() public void OpenTerminal()
{ {
if (!Preference.Instance.IsGitConfigured) if (!Preference.Instance.IsGitConfigured())
{ {
App.RaiseException(PopupHost.Active.GetId(), App.Text("NotConfigured")); App.RaiseException(PopupHost.Active.GetId(), App.Text("NotConfigured"));
} }
@ -81,7 +82,7 @@ namespace SourceGit.ViewModels
public void MoveNode(RepositoryNode from, RepositoryNode to) public void MoveNode(RepositoryNode from, RepositoryNode to)
{ {
Preference.MoveNode(from, to); Preference.Instance.MoveNode(from, to);
} }
public ContextMenu CreateContextMenu(RepositoryNode node) public ContextMenu CreateContextMenu(RepositoryNode node)
@ -166,7 +167,7 @@ namespace SourceGit.ViewModels
return menu; return menu;
} }
private void Referesh() private void Refresh()
{ {
if (string.IsNullOrWhiteSpace(_searchFilter)) if (string.IsNullOrWhiteSpace(_searchFilter))
{ {

View file

@ -139,7 +139,7 @@ namespace SourceGit.Views
}); });
var ver = string.Empty; var ver = string.Empty;
if (pref.IsGitConfigured) if (pref.IsGitConfigured())
{ {
var config = new Commands.Config(null).ListAll(); var config = new Commands.Config(null).ListAll();

View file

@ -37,9 +37,9 @@ namespace SourceGit.Views
private void OnTreeNodeContextRequested(object sender, ContextRequestedEventArgs e) private void OnTreeNodeContextRequested(object sender, ContextRequestedEventArgs e)
{ {
if (sender is Grid grid && DataContext is ViewModels.Welcome vm) if (sender is Grid grid)
{ {
var menu = vm.CreateContextMenu(grid.DataContext as ViewModels.RepositoryNode); var menu = ViewModels.Welcome.Instance.CreateContextMenu(grid.DataContext as ViewModels.RepositoryNode);
grid.OpenContextMenu(menu); grid.OpenContextMenu(menu);
e.Handled = true; e.Handled = true;
} }
@ -109,9 +109,7 @@ namespace SourceGit.Views
if (e.Data.Get("MovedRepositoryTreeNode") is ViewModels.RepositoryNode moved) if (e.Data.Get("MovedRepositoryTreeNode") is ViewModels.RepositoryNode moved)
{ {
e.Handled = true; e.Handled = true;
ViewModels.Welcome.Instance.MoveNode(moved, null);
if (DataContext is ViewModels.Welcome vm)
vm.MoveNode(moved, null);
} }
else if (e.Data.Contains(DataFormats.Files)) else if (e.Data.Contains(DataFormats.Files))
{ {
@ -173,8 +171,8 @@ namespace SourceGit.Views
{ {
e.Handled = true; e.Handled = true;
if (to != moved && DataContext is ViewModels.Welcome vm) if (to != moved)
vm.MoveNode(moved, to); ViewModels.Welcome.Instance.MoveNode(moved, to);
} }
else if (e.Data.Contains(DataFormats.Files)) else if (e.Data.Contains(DataFormats.Files))
{ {
@ -222,12 +220,12 @@ namespace SourceGit.Views
var root = new Commands.QueryRepositoryRootPath(path).Result(); var root = new Commands.QueryRepositoryRootPath(path).Result();
if (string.IsNullOrEmpty(root)) if (string.IsNullOrEmpty(root))
{ {
(DataContext as ViewModels.Welcome)?.InitRepository(path, parent); ViewModels.Welcome.Instance.InitRepository(path, parent);
return; return;
} }
var normalizedPath = root.Replace("\\", "/"); var normalizedPath = root.Replace("\\", "/");
var node = ViewModels.Preference.FindOrAddNodeByRepositoryPath(normalizedPath, parent, true); var node = ViewModels.Preference.Instance.FindOrAddNodeByRepositoryPath(normalizedPath, parent, true);
var launcher = this.FindAncestorOfType<Launcher>()?.DataContext as ViewModels.Launcher; var launcher = this.FindAncestorOfType<Launcher>()?.DataContext as ViewModels.Launcher;
launcher?.OpenRepositoryInTab(node, launcher.ActivePage); launcher?.OpenRepositoryInTab(node, launcher.ActivePage);
} }

View file

@ -44,12 +44,12 @@ namespace SourceGit.Views
var root = new Commands.QueryRepositoryRootPath(path).Result(); var root = new Commands.QueryRepositoryRootPath(path).Result();
if (string.IsNullOrEmpty(root)) if (string.IsNullOrEmpty(root))
{ {
(DataContext as ViewModels.Welcome)?.InitRepository(path, parent); ViewModels.Welcome.Instance.InitRepository(path, parent);
return; return;
} }
var normalizedPath = root.Replace("\\", "/"); var normalizedPath = root.Replace("\\", "/");
var node = ViewModels.Preference.FindOrAddNodeByRepositoryPath(normalizedPath, parent, true); var node = ViewModels.Preference.Instance.FindOrAddNodeByRepositoryPath(normalizedPath, parent, true);
var launcher = this.FindAncestorOfType<Launcher>()?.DataContext as ViewModels.Launcher; var launcher = this.FindAncestorOfType<Launcher>()?.DataContext as ViewModels.Launcher;
launcher?.OpenRepositoryInTab(node, launcher.ActivePage); launcher?.OpenRepositoryInTab(node, launcher.ActivePage);
} }