Skip to content

Commit 720b2b6

Browse files
committed
code_style: change static methods of ViewModels.Preference to member function
1 parent 1e0a2ab commit 720b2b6

File tree

13 files changed

+62
-68
lines changed

13 files changed

+62
-68
lines changed

src/App.axaml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,7 @@ private void TryLaunchedAsNormal(IClassicDesktopStyleApplicationLifetime desktop
520520
desktop.MainWindow = new Views.Launcher() { DataContext = _launcher };
521521

522522
var pref = ViewModels.Preference.Instance;
523-
if (pref.ShouldCheck4UpdateOnStartup)
523+
if (pref.ShouldCheck4UpdateOnStartup())
524524
{
525525
pref.Save();
526526
Check4Update();

src/ViewModels/Clone.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ public override Task<bool> Sure()
130130
CallUIThread(() =>
131131
{
132132
var normalizedPath = path.Replace("\\", "/");
133-
var node = Preference.FindOrAddNodeByRepositoryPath(normalizedPath, null, true);
133+
var node = Preference.Instance.FindOrAddNodeByRepositoryPath(normalizedPath, null, true);
134134
_launcher.OpenRepositoryInTab(node, _page);
135135
});
136136

src/ViewModels/CreateGroup.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public CreateGroup(RepositoryNode parent)
2121

2222
public override Task<bool> Sure()
2323
{
24-
Preference.AddNode(new RepositoryNode()
24+
Preference.Instance.AddNode(new RepositoryNode()
2525
{
2626
Id = Guid.NewGuid().ToString(),
2727
Name = _name,

src/ViewModels/DeleteRepositoryNode.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public DeleteRepositoryNode(RepositoryNode node)
1818

1919
public override Task<bool> Sure()
2020
{
21-
Preference.RemoveNode(_node);
21+
Preference.Instance.RemoveNode(_node);
2222
return null;
2323
}
2424

src/ViewModels/EditRepositoryNode.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public override Task<bool> Sure()
4848
_node.Bookmark = _bookmark;
4949

5050
if (needSort)
51-
Preference.SortByRenamedNode(_node);
51+
Preference.Instance.SortByRenamedNode(_node);
5252

5353
return null;
5454
}

src/ViewModels/Init.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public override Task<bool> Sure()
3131
CallUIThread(() =>
3232
{
3333
var normalizedPath = _targetPath.Replace("\\", "/");
34-
Preference.FindOrAddNodeByRepositoryPath(normalizedPath, _parentNode, true);
34+
Preference.Instance.FindOrAddNodeByRepositoryPath(normalizedPath, _parentNode, true);
3535
});
3636

3737
return true;

src/ViewModels/Launcher.cs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
using System;
22
using System.IO;
33

4-
using Avalonia;
54
using Avalonia.Collections;
65
using Avalonia.Controls;
76
using Avalonia.Input;
8-
using Avalonia.Media;
97

108
using CommunityToolkit.Mvvm.ComponentModel;
119

@@ -34,6 +32,7 @@ public Launcher(string startupRepo)
3432
Pages = new AvaloniaList<LauncherPage>();
3533
AddNewTab();
3634

35+
var pref = Preference.Instance;
3736
if (!string.IsNullOrEmpty(startupRepo))
3837
{
3938
var root = new Commands.QueryRepositoryRootPath(startupRepo).Result();
@@ -48,14 +47,14 @@ public Launcher(string startupRepo)
4847
}
4948

5049
var normalized = root.Replace("\\", "/");
51-
var node = Preference.FindOrAddNodeByRepositoryPath(normalized, null, false);
50+
var node = pref.FindOrAddNodeByRepositoryPath(normalized, null, false);
5251
OpenRepositoryInTab(node, null);
5352
}
54-
else if (Preference.Instance.RestoreTabs)
53+
else if (pref.RestoreTabs)
5554
{
56-
foreach (var id in Preference.Instance.OpenedTabs)
55+
foreach (var id in pref.OpenedTabs)
5756
{
58-
var node = Preference.FindNode(id);
57+
var node = pref.FindNode(id);
5958
if (node == null)
6059
{
6160
node = new RepositoryNode()
@@ -70,7 +69,7 @@ public Launcher(string startupRepo)
7069
OpenRepositoryInTab(node, null);
7170
}
7271

73-
var lastActiveIdx = Preference.Instance.LastActiveTabIdx;
72+
var lastActiveIdx = pref.LastActiveTabIdx;
7473
if (lastActiveIdx >= 0 && lastActiveIdx < Pages.Count)
7574
ActivePage = Pages[lastActiveIdx];
7675
}

src/ViewModels/Preference.cs

Lines changed: 31 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public static Preference Instance
4343
if (_instance.MonospaceFont == null)
4444
_instance.MonospaceFont = new FontFamily("fonts:SourceGit#JetBrains Mono");
4545

46-
if (!_instance.IsGitConfigured)
46+
if (!_instance.IsGitConfigured())
4747
_instance.GitInstallPath = Native.OS.FindGitExecutable();
4848

4949
return _instance;
@@ -213,12 +213,6 @@ public Models.ChangeViewMode CommitChangeViewMode
213213
set => SetProperty(ref _commitChangeViewMode, value);
214214
}
215215

216-
[JsonIgnore]
217-
public bool IsGitConfigured
218-
{
219-
get => !string.IsNullOrEmpty(GitInstallPath) && File.Exists(GitInstallPath);
220-
}
221-
222216
public string GitInstallPath
223217
{
224218
get => Native.OS.GitExecutable;
@@ -326,28 +320,30 @@ public double LastCheckUpdateTime
326320
set;
327321
} = 0;
328322

329-
[JsonIgnore]
330-
public bool ShouldCheck4UpdateOnStartup
323+
public bool IsGitConfigured()
331324
{
332-
get
333-
{
334-
if (!_check4UpdatesOnStartup)
335-
return false;
325+
var path = GitInstallPath;
326+
return !string.IsNullOrEmpty(path) && File.Exists(path);
327+
}
328+
329+
public bool ShouldCheck4UpdateOnStartup()
330+
{
331+
if (!_check4UpdatesOnStartup)
332+
return false;
336333

337-
var lastCheck = DateTime.UnixEpoch.AddSeconds(LastCheckUpdateTime).ToLocalTime();
338-
var now = DateTime.Now;
334+
var lastCheck = DateTime.UnixEpoch.AddSeconds(LastCheckUpdateTime).ToLocalTime();
335+
var now = DateTime.Now;
339336

340-
if (lastCheck.Year == now.Year && lastCheck.Month == now.Month && lastCheck.Day == now.Day)
341-
return false;
337+
if (lastCheck.Year == now.Year && lastCheck.Month == now.Month && lastCheck.Day == now.Day)
338+
return false;
342339

343-
LastCheckUpdateTime = now.Subtract(DateTime.UnixEpoch.ToLocalTime()).TotalSeconds;
344-
return true;
345-
}
340+
LastCheckUpdateTime = now.Subtract(DateTime.UnixEpoch.ToLocalTime()).TotalSeconds;
341+
return true;
346342
}
347343

348-
public static void AddNode(RepositoryNode node, RepositoryNode to = null)
344+
public void AddNode(RepositoryNode node, RepositoryNode to = null)
349345
{
350-
var collection = to == null ? _instance._repositoryNodes : to.SubNodes;
346+
var collection = to == null ? _repositoryNodes : to.SubNodes;
351347
var list = new List<RepositoryNode>();
352348
list.AddRange(collection);
353349
list.Add(node);
@@ -364,14 +360,14 @@ public static void AddNode(RepositoryNode node, RepositoryNode to = null)
364360
collection.Add(one);
365361
}
366362

367-
public static RepositoryNode FindNode(string id)
363+
public RepositoryNode FindNode(string id)
368364
{
369-
return FindNodeRecursive(id, _instance.RepositoryNodes);
365+
return FindNodeRecursive(id, RepositoryNodes);
370366
}
371367

372-
public static RepositoryNode FindOrAddNodeByRepositoryPath(string repo, RepositoryNode parent, bool shouldMoveNode)
368+
public RepositoryNode FindOrAddNodeByRepositoryPath(string repo, RepositoryNode parent, bool shouldMoveNode)
373369
{
374-
var node = FindNodeRecursive(repo, _instance.RepositoryNodes);
370+
var node = FindNodeRecursive(repo, RepositoryNodes);
375371
if (node == null)
376372
{
377373
node = new RepositoryNode()
@@ -392,9 +388,9 @@ public static RepositoryNode FindOrAddNodeByRepositoryPath(string repo, Reposito
392388
return node;
393389
}
394390

395-
public static void MoveNode(RepositoryNode node, RepositoryNode to = null)
391+
public void MoveNode(RepositoryNode node, RepositoryNode to = null)
396392
{
397-
if (to == null && _instance._repositoryNodes.Contains(node))
393+
if (to == null && _repositoryNodes.Contains(node))
398394
return;
399395
if (to != null && to.SubNodes.Contains(node))
400396
return;
@@ -403,14 +399,14 @@ public static void MoveNode(RepositoryNode node, RepositoryNode to = null)
403399
AddNode(node, to);
404400
}
405401

406-
public static void RemoveNode(RepositoryNode node)
402+
public void RemoveNode(RepositoryNode node)
407403
{
408-
RemoveNodeRecursive(node, _instance._repositoryNodes);
404+
RemoveNodeRecursive(node, _repositoryNodes);
409405
}
410406

411-
public static void SortByRenamedNode(RepositoryNode node)
407+
public void SortByRenamedNode(RepositoryNode node)
412408
{
413-
var container = FindNodeContainer(node, _instance._repositoryNodes);
409+
var container = FindNodeContainer(node, _repositoryNodes);
414410
if (container == null)
415411
return;
416412

@@ -435,7 +431,7 @@ public void Save()
435431
File.WriteAllText(_savePath, data);
436432
}
437433

438-
private static RepositoryNode FindNodeRecursive(string id, AvaloniaList<RepositoryNode> collection)
434+
private RepositoryNode FindNodeRecursive(string id, AvaloniaList<RepositoryNode> collection)
439435
{
440436
foreach (var node in collection)
441437
{
@@ -450,7 +446,7 @@ private static RepositoryNode FindNodeRecursive(string id, AvaloniaList<Reposito
450446
return null;
451447
}
452448

453-
private static AvaloniaList<RepositoryNode> FindNodeContainer(RepositoryNode node, AvaloniaList<RepositoryNode> collection)
449+
private AvaloniaList<RepositoryNode> FindNodeContainer(RepositoryNode node, AvaloniaList<RepositoryNode> collection)
454450
{
455451
foreach (var sub in collection)
456452
{
@@ -465,7 +461,7 @@ private static AvaloniaList<RepositoryNode> FindNodeContainer(RepositoryNode nod
465461
return null;
466462
}
467463

468-
private static bool RemoveNodeRecursive(RepositoryNode node, AvaloniaList<RepositoryNode> collection)
464+
private bool RemoveNodeRecursive(RepositoryNode node, AvaloniaList<RepositoryNode> collection)
469465
{
470466
if (collection.Contains(node))
471467
{

src/ViewModels/Repository.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -965,7 +965,7 @@ public void OpenSubmodule(string submodule)
965965
var root = Path.GetFullPath(Path.Combine(_fullpath, submodule));
966966
var normalizedPath = root.Replace("\\", "/");
967967

968-
var node = Preference.FindNode(normalizedPath);
968+
var node = Preference.Instance.FindNode(normalizedPath);
969969
if (node == null)
970970
{
971971
node = new RepositoryNode()
@@ -996,7 +996,7 @@ public void PruneWorktrees()
996996

997997
public void OpenWorktree(Models.Worktree worktree)
998998
{
999-
var node = Preference.FindNode(worktree.FullPath);
999+
var node = Preference.Instance.FindNode(worktree.FullPath);
10001000
if (node == null)
10011001
{
10021002
node = new RepositoryNode()

src/ViewModels/Welcome.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
23
using Avalonia;
34
using Avalonia.Collections;
45
using Avalonia.Controls;
@@ -23,13 +24,13 @@ public string SearchFilter
2324
set
2425
{
2526
if (SetProperty(ref _searchFilter, value))
26-
Referesh();
27+
Refresh();
2728
}
2829
}
2930

3031
public void InitRepository(string path, RepositoryNode parent)
3132
{
32-
if (!Preference.Instance.IsGitConfigured)
33+
if (!Preference.Instance.IsGitConfigured())
3334
{
3435
App.RaiseException(PopupHost.Active.GetId(), App.Text("NotConfigured"));
3536
return;
@@ -43,7 +44,7 @@ public void InitRepository(string path, RepositoryNode parent)
4344

4445
public void Clone()
4546
{
46-
if (!Preference.Instance.IsGitConfigured)
47+
if (!Preference.Instance.IsGitConfigured())
4748
{
4849
App.RaiseException(string.Empty, App.Text("NotConfigured"));
4950
return;
@@ -58,7 +59,7 @@ public void Clone()
5859

5960
public void OpenTerminal()
6061
{
61-
if (!Preference.Instance.IsGitConfigured)
62+
if (!Preference.Instance.IsGitConfigured())
6263
{
6364
App.RaiseException(PopupHost.Active.GetId(), App.Text("NotConfigured"));
6465
}
@@ -81,7 +82,7 @@ public void AddRootNode()
8182

8283
public void MoveNode(RepositoryNode from, RepositoryNode to)
8384
{
84-
Preference.MoveNode(from, to);
85+
Preference.Instance.MoveNode(from, to);
8586
}
8687

8788
public ContextMenu CreateContextMenu(RepositoryNode node)
@@ -166,7 +167,7 @@ public ContextMenu CreateContextMenu(RepositoryNode node)
166167
return menu;
167168
}
168169

169-
private void Referesh()
170+
private void Refresh()
170171
{
171172
if (string.IsNullOrWhiteSpace(_searchFilter))
172173
{

0 commit comments

Comments
 (0)