refactor: build tags view data in viewmodels instead of views

Signed-off-by: leo <longshuang@msn.cn>
This commit is contained in:
leo 2025-05-16 12:22:37 +08:00
parent f46bbd01cd
commit fd935259aa
No known key found for this signature in database
7 changed files with 151 additions and 172 deletions

View file

@ -1,6 +1,8 @@
using System;
using System.Collections.Generic;
using Avalonia.Collections;
using CommunityToolkit.Mvvm.ComponentModel;
namespace SourceGit.ViewModels
@ -61,7 +63,7 @@ namespace SourceGit.ViewModels
Counter = 1;
}
public static List<TagTreeNode> Build(IList<Models.Tag> tags, HashSet<string> expaneded)
public static List<TagTreeNode> Build(List<Models.Tag> tags, HashSet<string> expaneded)
{
var nodes = new List<TagTreeNode>();
var folders = new Dictionary<string, TagTreeNode>();
@ -131,7 +133,7 @@ namespace SourceGit.ViewModels
public class TagCollectionAsList
{
public AvaloniaList<Models.Tag> Tags
public List<Models.Tag> Tags
{
get;
set;
@ -151,5 +153,71 @@ namespace SourceGit.ViewModels
get;
set;
} = [];
public static TagCollectionAsTree Build(List<Models.Tag> tags, TagCollectionAsTree old)
{
var oldExpanded = new HashSet<string>();
if (old != null)
{
foreach (var row in old.Rows)
{
if (row.IsFolder && row.IsExpanded)
oldExpanded.Add(row.FullPath);
}
}
var collection = new TagCollectionAsTree();
collection.Tree = TagTreeNode.Build(tags, oldExpanded);
var rows = new List<TagTreeNode>();
MakeTreeRows(rows, collection.Tree);
collection.Rows.AddRange(rows);
return collection;
}
public void ToggleExpand(TagTreeNode node)
{
node.IsExpanded = !node.IsExpanded;
var rows = Rows;
var depth = node.Depth;
var idx = rows.IndexOf(node);
if (idx == -1)
return;
if (node.IsExpanded)
{
var subrows = new List<TagTreeNode>();
MakeTreeRows(subrows, node.Children);
rows.InsertRange(idx + 1, subrows);
}
else
{
var removeCount = 0;
for (int i = idx + 1; i < rows.Count; i++)
{
var row = rows[i];
if (row.Depth <= depth)
break;
removeCount++;
}
rows.RemoveRange(idx + 1, removeCount);
}
}
private static void MakeTreeRows(List<TagTreeNode> rows, List<TagTreeNode> nodes)
{
foreach (var node in nodes)
{
rows.Add(node);
if (!node.IsExpanded || !node.IsFolder)
continue;
MakeTreeRows(rows, node.Children);
}
}
}
}