From 720b2b65f3bf673dde6295267395d1a2bdb8581b Mon Sep 17 00:00:00 2001 From: leo Date: Mon, 22 Jul 2024 15:34:31 +0800 Subject: [PATCH] code_style: change static methods of ViewModels.Preference to member function --- src/App.axaml.cs | 2 +- src/ViewModels/Clone.cs | 2 +- src/ViewModels/CreateGroup.cs | 2 +- src/ViewModels/DeleteRepositoryNode.cs | 2 +- src/ViewModels/EditRepositoryNode.cs | 2 +- src/ViewModels/Init.cs | 2 +- src/ViewModels/Launcher.cs | 13 +++-- src/ViewModels/Preference.cs | 72 ++++++++++++-------------- src/ViewModels/Repository.cs | 4 +- src/ViewModels/Welcome.cs | 13 ++--- src/Views/Preference.axaml.cs | 2 +- src/Views/Welcome.axaml.cs | 16 +++--- src/Views/WelcomeToolbar.axaml.cs | 4 +- 13 files changed, 65 insertions(+), 71 deletions(-) diff --git a/src/App.axaml.cs b/src/App.axaml.cs index 7abf8293..6d09fe00 100644 --- a/src/App.axaml.cs +++ b/src/App.axaml.cs @@ -520,7 +520,7 @@ namespace SourceGit desktop.MainWindow = new Views.Launcher() { DataContext = _launcher }; var pref = ViewModels.Preference.Instance; - if (pref.ShouldCheck4UpdateOnStartup) + if (pref.ShouldCheck4UpdateOnStartup()) { pref.Save(); Check4Update(); diff --git a/src/ViewModels/Clone.cs b/src/ViewModels/Clone.cs index 819ef83d..7f03e0fd 100644 --- a/src/ViewModels/Clone.cs +++ b/src/ViewModels/Clone.cs @@ -130,7 +130,7 @@ namespace SourceGit.ViewModels CallUIThread(() => { var normalizedPath = path.Replace("\\", "/"); - var node = Preference.FindOrAddNodeByRepositoryPath(normalizedPath, null, true); + var node = Preference.Instance.FindOrAddNodeByRepositoryPath(normalizedPath, null, true); _launcher.OpenRepositoryInTab(node, _page); }); diff --git a/src/ViewModels/CreateGroup.cs b/src/ViewModels/CreateGroup.cs index cfe9d9ee..08a03a70 100644 --- a/src/ViewModels/CreateGroup.cs +++ b/src/ViewModels/CreateGroup.cs @@ -21,7 +21,7 @@ namespace SourceGit.ViewModels public override Task Sure() { - Preference.AddNode(new RepositoryNode() + Preference.Instance.AddNode(new RepositoryNode() { Id = Guid.NewGuid().ToString(), Name = _name, diff --git a/src/ViewModels/DeleteRepositoryNode.cs b/src/ViewModels/DeleteRepositoryNode.cs index a42fc8d6..3b6115c9 100644 --- a/src/ViewModels/DeleteRepositoryNode.cs +++ b/src/ViewModels/DeleteRepositoryNode.cs @@ -18,7 +18,7 @@ namespace SourceGit.ViewModels public override Task Sure() { - Preference.RemoveNode(_node); + Preference.Instance.RemoveNode(_node); return null; } diff --git a/src/ViewModels/EditRepositoryNode.cs b/src/ViewModels/EditRepositoryNode.cs index a4172d92..2df9ce29 100644 --- a/src/ViewModels/EditRepositoryNode.cs +++ b/src/ViewModels/EditRepositoryNode.cs @@ -48,7 +48,7 @@ namespace SourceGit.ViewModels _node.Bookmark = _bookmark; if (needSort) - Preference.SortByRenamedNode(_node); + Preference.Instance.SortByRenamedNode(_node); return null; } diff --git a/src/ViewModels/Init.cs b/src/ViewModels/Init.cs index 96b9d01f..67f54d5f 100644 --- a/src/ViewModels/Init.cs +++ b/src/ViewModels/Init.cs @@ -31,7 +31,7 @@ namespace SourceGit.ViewModels CallUIThread(() => { var normalizedPath = _targetPath.Replace("\\", "/"); - Preference.FindOrAddNodeByRepositoryPath(normalizedPath, _parentNode, true); + Preference.Instance.FindOrAddNodeByRepositoryPath(normalizedPath, _parentNode, true); }); return true; diff --git a/src/ViewModels/Launcher.cs b/src/ViewModels/Launcher.cs index d5363911..19c328c4 100644 --- a/src/ViewModels/Launcher.cs +++ b/src/ViewModels/Launcher.cs @@ -1,11 +1,9 @@ using System; using System.IO; -using Avalonia; using Avalonia.Collections; using Avalonia.Controls; using Avalonia.Input; -using Avalonia.Media; using CommunityToolkit.Mvvm.ComponentModel; @@ -34,6 +32,7 @@ namespace SourceGit.ViewModels Pages = new AvaloniaList(); AddNewTab(); + var pref = Preference.Instance; if (!string.IsNullOrEmpty(startupRepo)) { var root = new Commands.QueryRepositoryRootPath(startupRepo).Result(); @@ -48,14 +47,14 @@ namespace SourceGit.ViewModels } var normalized = root.Replace("\\", "/"); - var node = Preference.FindOrAddNodeByRepositoryPath(normalized, null, false); + var node = pref.FindOrAddNodeByRepositoryPath(normalized, null, false); 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) { node = new RepositoryNode() @@ -70,7 +69,7 @@ namespace SourceGit.ViewModels OpenRepositoryInTab(node, null); } - var lastActiveIdx = Preference.Instance.LastActiveTabIdx; + var lastActiveIdx = pref.LastActiveTabIdx; if (lastActiveIdx >= 0 && lastActiveIdx < Pages.Count) ActivePage = Pages[lastActiveIdx]; } diff --git a/src/ViewModels/Preference.cs b/src/ViewModels/Preference.cs index 4b3f1cee..fbc2d14c 100644 --- a/src/ViewModels/Preference.cs +++ b/src/ViewModels/Preference.cs @@ -43,7 +43,7 @@ namespace SourceGit.ViewModels if (_instance.MonospaceFont == null) _instance.MonospaceFont = new FontFamily("fonts:SourceGit#JetBrains Mono"); - if (!_instance.IsGitConfigured) + if (!_instance.IsGitConfigured()) _instance.GitInstallPath = Native.OS.FindGitExecutable(); return _instance; @@ -213,12 +213,6 @@ namespace SourceGit.ViewModels set => SetProperty(ref _commitChangeViewMode, value); } - [JsonIgnore] - public bool IsGitConfigured - { - get => !string.IsNullOrEmpty(GitInstallPath) && File.Exists(GitInstallPath); - } - public string GitInstallPath { get => Native.OS.GitExecutable; @@ -326,28 +320,30 @@ namespace SourceGit.ViewModels set; } = 0; - [JsonIgnore] - public bool ShouldCheck4UpdateOnStartup + public bool IsGitConfigured() { - get - { - if (!_check4UpdatesOnStartup) - return false; - - var lastCheck = DateTime.UnixEpoch.AddSeconds(LastCheckUpdateTime).ToLocalTime(); - var now = DateTime.Now; - - if (lastCheck.Year == now.Year && lastCheck.Month == now.Month && lastCheck.Day == now.Day) - return false; - - LastCheckUpdateTime = now.Subtract(DateTime.UnixEpoch.ToLocalTime()).TotalSeconds; - return true; - } + var path = GitInstallPath; + return !string.IsNullOrEmpty(path) && File.Exists(path); } - public static void AddNode(RepositoryNode node, RepositoryNode to = null) + public bool ShouldCheck4UpdateOnStartup() { - var collection = to == null ? _instance._repositoryNodes : to.SubNodes; + if (!_check4UpdatesOnStartup) + return false; + + var lastCheck = DateTime.UnixEpoch.AddSeconds(LastCheckUpdateTime).ToLocalTime(); + var now = DateTime.Now; + + if (lastCheck.Year == now.Year && lastCheck.Month == now.Month && lastCheck.Day == now.Day) + return false; + + LastCheckUpdateTime = now.Subtract(DateTime.UnixEpoch.ToLocalTime()).TotalSeconds; + return true; + } + + public void AddNode(RepositoryNode node, RepositoryNode to = null) + { + var collection = to == null ? _repositoryNodes : to.SubNodes; var list = new List(); list.AddRange(collection); list.Add(node); @@ -364,14 +360,14 @@ namespace SourceGit.ViewModels 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) { node = new RepositoryNode() @@ -392,9 +388,9 @@ namespace SourceGit.ViewModels 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; if (to != null && to.SubNodes.Contains(node)) return; @@ -403,14 +399,14 @@ namespace SourceGit.ViewModels 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) return; @@ -435,7 +431,7 @@ namespace SourceGit.ViewModels File.WriteAllText(_savePath, data); } - private static RepositoryNode FindNodeRecursive(string id, AvaloniaList collection) + private RepositoryNode FindNodeRecursive(string id, AvaloniaList collection) { foreach (var node in collection) { @@ -450,7 +446,7 @@ namespace SourceGit.ViewModels return null; } - private static AvaloniaList FindNodeContainer(RepositoryNode node, AvaloniaList collection) + private AvaloniaList FindNodeContainer(RepositoryNode node, AvaloniaList collection) { foreach (var sub in collection) { @@ -465,7 +461,7 @@ namespace SourceGit.ViewModels return null; } - private static bool RemoveNodeRecursive(RepositoryNode node, AvaloniaList collection) + private bool RemoveNodeRecursive(RepositoryNode node, AvaloniaList collection) { if (collection.Contains(node)) { diff --git a/src/ViewModels/Repository.cs b/src/ViewModels/Repository.cs index dad551d0..709b0f2b 100644 --- a/src/ViewModels/Repository.cs +++ b/src/ViewModels/Repository.cs @@ -965,7 +965,7 @@ namespace SourceGit.ViewModels var root = Path.GetFullPath(Path.Combine(_fullpath, submodule)); var normalizedPath = root.Replace("\\", "/"); - var node = Preference.FindNode(normalizedPath); + var node = Preference.Instance.FindNode(normalizedPath); if (node == null) { node = new RepositoryNode() @@ -996,7 +996,7 @@ namespace SourceGit.ViewModels public void OpenWorktree(Models.Worktree worktree) { - var node = Preference.FindNode(worktree.FullPath); + var node = Preference.Instance.FindNode(worktree.FullPath); if (node == null) { node = new RepositoryNode() diff --git a/src/ViewModels/Welcome.cs b/src/ViewModels/Welcome.cs index de525309..a1bc0a46 100644 --- a/src/ViewModels/Welcome.cs +++ b/src/ViewModels/Welcome.cs @@ -1,4 +1,5 @@ using System; + using Avalonia; using Avalonia.Collections; using Avalonia.Controls; @@ -23,13 +24,13 @@ namespace SourceGit.ViewModels set { if (SetProperty(ref _searchFilter, value)) - Referesh(); + Refresh(); } } public void InitRepository(string path, RepositoryNode parent) { - if (!Preference.Instance.IsGitConfigured) + if (!Preference.Instance.IsGitConfigured()) { App.RaiseException(PopupHost.Active.GetId(), App.Text("NotConfigured")); return; @@ -43,7 +44,7 @@ namespace SourceGit.ViewModels public void Clone() { - if (!Preference.Instance.IsGitConfigured) + if (!Preference.Instance.IsGitConfigured()) { App.RaiseException(string.Empty, App.Text("NotConfigured")); return; @@ -58,7 +59,7 @@ namespace SourceGit.ViewModels public void OpenTerminal() { - if (!Preference.Instance.IsGitConfigured) + if (!Preference.Instance.IsGitConfigured()) { App.RaiseException(PopupHost.Active.GetId(), App.Text("NotConfigured")); } @@ -81,7 +82,7 @@ namespace SourceGit.ViewModels public void MoveNode(RepositoryNode from, RepositoryNode to) { - Preference.MoveNode(from, to); + Preference.Instance.MoveNode(from, to); } public ContextMenu CreateContextMenu(RepositoryNode node) @@ -166,7 +167,7 @@ namespace SourceGit.ViewModels return menu; } - private void Referesh() + private void Refresh() { if (string.IsNullOrWhiteSpace(_searchFilter)) { diff --git a/src/Views/Preference.axaml.cs b/src/Views/Preference.axaml.cs index 20a98795..1d46b591 100644 --- a/src/Views/Preference.axaml.cs +++ b/src/Views/Preference.axaml.cs @@ -139,7 +139,7 @@ namespace SourceGit.Views }); var ver = string.Empty; - if (pref.IsGitConfigured) + if (pref.IsGitConfigured()) { var config = new Commands.Config(null).ListAll(); diff --git a/src/Views/Welcome.axaml.cs b/src/Views/Welcome.axaml.cs index 82f78980..91c156e6 100644 --- a/src/Views/Welcome.axaml.cs +++ b/src/Views/Welcome.axaml.cs @@ -37,9 +37,9 @@ namespace SourceGit.Views 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); e.Handled = true; } @@ -109,9 +109,7 @@ namespace SourceGit.Views if (e.Data.Get("MovedRepositoryTreeNode") is ViewModels.RepositoryNode moved) { e.Handled = true; - - if (DataContext is ViewModels.Welcome vm) - vm.MoveNode(moved, null); + ViewModels.Welcome.Instance.MoveNode(moved, null); } else if (e.Data.Contains(DataFormats.Files)) { @@ -173,8 +171,8 @@ namespace SourceGit.Views { e.Handled = true; - if (to != moved && DataContext is ViewModels.Welcome vm) - vm.MoveNode(moved, to); + if (to != moved) + ViewModels.Welcome.Instance.MoveNode(moved, to); } else if (e.Data.Contains(DataFormats.Files)) { @@ -222,12 +220,12 @@ namespace SourceGit.Views var root = new Commands.QueryRepositoryRootPath(path).Result(); if (string.IsNullOrEmpty(root)) { - (DataContext as ViewModels.Welcome)?.InitRepository(path, parent); + ViewModels.Welcome.Instance.InitRepository(path, parent); return; } 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()?.DataContext as ViewModels.Launcher; launcher?.OpenRepositoryInTab(node, launcher.ActivePage); } diff --git a/src/Views/WelcomeToolbar.axaml.cs b/src/Views/WelcomeToolbar.axaml.cs index 7294ff4f..597d964d 100644 --- a/src/Views/WelcomeToolbar.axaml.cs +++ b/src/Views/WelcomeToolbar.axaml.cs @@ -44,12 +44,12 @@ namespace SourceGit.Views var root = new Commands.QueryRepositoryRootPath(path).Result(); if (string.IsNullOrEmpty(root)) { - (DataContext as ViewModels.Welcome)?.InitRepository(path, parent); + ViewModels.Welcome.Instance.InitRepository(path, parent); return; } 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()?.DataContext as ViewModels.Launcher; launcher?.OpenRepositoryInTab(node, launcher.ActivePage); }