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

@ -178,9 +178,9 @@ namespace SourceGit.ViewModels
public bool ShowTagsAsTree
{
get => _showTagsAsTree;
set => SetProperty(ref _showTagsAsTree, value);
}
get;
set;
} = false;
public bool ShowTagsInGraph
{
@ -677,7 +677,6 @@ namespace SourceGit.ViewModels
private double _lastCheckUpdateTime = 0;
private string _ignoreUpdateTag = string.Empty;
private bool _showTagsAsTree = false;
private bool _showTagsInGraph = true;
private bool _useTwoColumnsLayoutInHistories = false;
private bool _displayTimeAsPeriodInHistories = false;

View file

@ -198,7 +198,21 @@ namespace SourceGit.ViewModels
private set => SetProperty(ref _tags, value);
}
public List<Models.Tag> VisibleTags
public bool ShowTagsAsTree
{
get => Preferences.Instance.ShowTagsAsTree;
set
{
if (value != Preferences.Instance.ShowTagsAsTree)
{
Preferences.Instance.ShowTagsAsTree = value;
VisibleTags = BuildVisibleTags();
OnPropertyChanged();
}
}
}
public object VisibleTags
{
get => _visibleTags;
private set => SetProperty(ref _visibleTags, value);
@ -548,7 +562,7 @@ namespace SourceGit.ViewModels
_localBranchTrees.Clear();
_remoteBranchTrees.Clear();
_tags.Clear();
_visibleTags.Clear();
_visibleTags = null;
_submodules.Clear();
_visibleSubmodules = null;
_searchedCommits.Clear();
@ -2492,7 +2506,7 @@ namespace SourceGit.ViewModels
return builder;
}
private List<Models.Tag> BuildVisibleTags()
private object BuildVisibleTags()
{
switch (_settings.TagSortMode)
{
@ -2523,7 +2537,11 @@ namespace SourceGit.ViewModels
var historiesFilters = _settings.CollectHistoriesFilters();
UpdateTagFilterMode(historiesFilters);
return visible;
if (Preferences.Instance.ShowTagsAsTree)
return TagCollectionAsTree.Build(visible, _visibleTags as TagCollectionAsTree);
else
return new TagCollectionAsList() { Tags = visible };
}
private object BuildVisibleSubmodules()
@ -2775,7 +2793,7 @@ namespace SourceGit.ViewModels
private List<BranchTreeNode> _remoteBranchTrees = new List<BranchTreeNode>();
private List<Models.Worktree> _worktrees = new List<Models.Worktree>();
private List<Models.Tag> _tags = new List<Models.Tag>();
private List<Models.Tag> _visibleTags = new List<Models.Tag>();
private object _visibleTags = null;
private List<Models.Submodule> _submodules = new List<Models.Submodule>();
private object _visibleSubmodules = null;

View file

@ -150,18 +150,12 @@ namespace SourceGit.ViewModels
collection.Tree = SubmoduleTreeNode.Build(submodules, oldExpanded);
var rows = new List<SubmoduleTreeNode>();
collection.MakeTreeRows(rows, collection.Tree);
MakeTreeRows(rows, collection.Tree);
collection.Rows.AddRange(rows);
return collection;
}
public void Clear()
{
Tree.Clear();
Rows.Clear();
}
public void ToggleExpand(SubmoduleTreeNode node)
{
node.IsExpanded = !node.IsExpanded;
@ -193,7 +187,7 @@ namespace SourceGit.ViewModels
}
}
private void MakeTreeRows(List<SubmoduleTreeNode> rows, List<SubmoduleTreeNode> nodes)
private static void MakeTreeRows(List<SubmoduleTreeNode> rows, List<SubmoduleTreeNode> nodes)
{
foreach (var node in nodes)
{

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);
}
}
}
}