refactor: there's no need to use AvaloniaList since we have replaced the TreeView with custom control

This commit is contained in:
leo 2024-08-23 11:24:31 +08:00
parent 9bcadf3523
commit 4120331eda
No known key found for this signature in database
3 changed files with 21 additions and 42 deletions

View file

@ -4,8 +4,6 @@ using System.IO;
using System.Text.Json; using System.Text.Json;
using System.Text.Json.Serialization; using System.Text.Json.Serialization;
using Avalonia.Collections;
using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.ComponentModel;
namespace SourceGit.ViewModels namespace SourceGit.ViewModels
@ -306,11 +304,11 @@ namespace SourceGit.ViewModels
set => SetProperty(ref _externalMergeToolPath, value); set => SetProperty(ref _externalMergeToolPath, value);
} }
public AvaloniaList<RepositoryNode> RepositoryNodes public List<RepositoryNode> RepositoryNodes
{ {
get => _repositoryNodes; get;
set => SetProperty(ref _repositoryNodes, value); set;
} } = [];
public List<string> OpenedTabs public List<string> OpenedTabs
{ {
@ -353,21 +351,15 @@ namespace SourceGit.ViewModels
public void AddNode(RepositoryNode node, RepositoryNode to = null) public void AddNode(RepositoryNode node, RepositoryNode to = null)
{ {
var collection = to == null ? _repositoryNodes : to.SubNodes; var collection = to == null ? RepositoryNodes : to.SubNodes;
var list = new List<RepositoryNode>(); collection.Add(node);
list.AddRange(collection); collection.Sort((l, r) =>
list.Add(node);
list.Sort((l, r) =>
{ {
if (l.IsRepository != r.IsRepository) if (l.IsRepository != r.IsRepository)
return l.IsRepository ? 1 : -1; return l.IsRepository ? 1 : -1;
return string.Compare(l.Name, r.Name, StringComparison.Ordinal); return string.Compare(l.Name, r.Name, StringComparison.Ordinal);
}); });
collection.Clear();
foreach (var one in list)
collection.Add(one);
} }
public RepositoryNode FindNode(string id) public RepositoryNode FindNode(string id)
@ -400,7 +392,7 @@ namespace SourceGit.ViewModels
public void MoveNode(RepositoryNode node, RepositoryNode to = null) public void MoveNode(RepositoryNode node, RepositoryNode to = null)
{ {
if (to == null && _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;
@ -411,28 +403,19 @@ namespace SourceGit.ViewModels
public void RemoveNode(RepositoryNode node) public void RemoveNode(RepositoryNode node)
{ {
RemoveNodeRecursive(node, _repositoryNodes); RemoveNodeRecursive(node, RepositoryNodes);
} }
public void SortByRenamedNode(RepositoryNode node) public void SortByRenamedNode(RepositoryNode node)
{ {
var container = FindNodeContainer(node, _repositoryNodes); var container = FindNodeContainer(node, RepositoryNodes);
if (container == null) container?.Sort((l, r) =>
return;
var list = new List<RepositoryNode>();
list.AddRange(container);
list.Sort((l, r) =>
{ {
if (l.IsRepository != r.IsRepository) if (l.IsRepository != r.IsRepository)
return l.IsRepository ? 1 : -1; return l.IsRepository ? 1 : -1;
return string.Compare(l.Name, r.Name, StringComparison.Ordinal); return string.Compare(l.Name, r.Name, StringComparison.Ordinal);
}); });
container.Clear();
foreach (var one in list)
container.Add(one);
} }
public void Save() public void Save()
@ -441,7 +424,7 @@ namespace SourceGit.ViewModels
File.WriteAllText(_savePath, data); File.WriteAllText(_savePath, data);
} }
private RepositoryNode FindNodeRecursive(string id, AvaloniaList<RepositoryNode> collection) private RepositoryNode FindNodeRecursive(string id, List<RepositoryNode> collection)
{ {
foreach (var node in collection) foreach (var node in collection)
{ {
@ -456,7 +439,7 @@ namespace SourceGit.ViewModels
return null; return null;
} }
private AvaloniaList<RepositoryNode> FindNodeContainer(RepositoryNode node, AvaloniaList<RepositoryNode> collection) private List<RepositoryNode> FindNodeContainer(RepositoryNode node, List<RepositoryNode> collection)
{ {
foreach (var sub in collection) foreach (var sub in collection)
{ {
@ -471,7 +454,7 @@ namespace SourceGit.ViewModels
return null; return null;
} }
private bool RemoveNodeRecursive(RepositoryNode node, AvaloniaList<RepositoryNode> collection) private bool RemoveNodeRecursive(RepositoryNode node, List<RepositoryNode> collection)
{ {
if (collection.Contains(node)) if (collection.Contains(node))
{ {
@ -525,7 +508,5 @@ namespace SourceGit.ViewModels
private int _externalMergeToolType = 0; private int _externalMergeToolType = 0;
private string _externalMergeToolPath = string.Empty; private string _externalMergeToolPath = string.Empty;
private AvaloniaList<RepositoryNode> _repositoryNodes = new AvaloniaList<RepositoryNode>();
} }
} }

View file

@ -1,6 +1,5 @@
using System.Text.Json.Serialization; using System.Collections.Generic;
using System.Text.Json.Serialization;
using Avalonia.Collections;
using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.ComponentModel;
@ -56,11 +55,11 @@ namespace SourceGit.ViewModels
set; set;
} = 0; } = 0;
public AvaloniaList<RepositoryNode> SubNodes public List<RepositoryNode> SubNodes
{ {
get => _subNodes; get;
set => SetProperty(ref _subNodes, value); set;
} } = [];
public void Edit() public void Edit()
{ {
@ -100,6 +99,5 @@ namespace SourceGit.ViewModels
private int _bookmark = 0; private int _bookmark = 0;
private bool _isExpanded = false; private bool _isExpanded = false;
private bool _isVisible = true; private bool _isVisible = true;
private AvaloniaList<RepositoryNode> _subNodes = new AvaloniaList<RepositoryNode>();
} }
} }

View file

@ -242,7 +242,7 @@ namespace SourceGit.ViewModels
} }
} }
private void MakeTreeRows(List<RepositoryNode> rows, AvaloniaList<RepositoryNode> nodes, int depth = 0) private void MakeTreeRows(List<RepositoryNode> rows, List<RepositoryNode> nodes, int depth = 0)
{ {
foreach (var node in nodes) foreach (var node in nodes)
{ {