| | | 1 | | using Chronicis.Client.Models; |
| | | 2 | | |
| | | 3 | | namespace Chronicis.Client.Services.Tree; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// Shared container for tree node state. |
| | | 7 | | /// Provides indexed access to all nodes in the tree and holds the root node collection. |
| | | 8 | | /// This class is shared between TreeDataBuilder, TreeUiState, and TreeMutations. |
| | | 9 | | /// </summary> |
| | | 10 | | internal sealed class TreeNodeIndex |
| | | 11 | | { |
| | 114 | 12 | | private readonly Dictionary<Guid, TreeNode> _nodes = new(); |
| | 114 | 13 | | private List<TreeNode> _rootNodes = new(); |
| | | 14 | | |
| | | 15 | | /// <summary> |
| | | 16 | | /// Gets the root-level nodes of the tree. |
| | | 17 | | /// </summary> |
| | 7 | 18 | | public IReadOnlyList<TreeNode> RootNodes => _rootNodes; |
| | | 19 | | |
| | | 20 | | /// <summary> |
| | | 21 | | /// Gets all nodes indexed by their ID. |
| | | 22 | | /// </summary> |
| | 0 | 23 | | public IReadOnlyDictionary<Guid, TreeNode> Nodes => _nodes; |
| | | 24 | | |
| | | 25 | | /// <summary> |
| | | 26 | | /// Gets the count of all indexed nodes. |
| | | 27 | | /// </summary> |
| | 4 | 28 | | public int Count => _nodes.Count; |
| | | 29 | | |
| | | 30 | | /// <summary> |
| | | 31 | | /// Attempts to get a node by its ID. |
| | | 32 | | /// </summary> |
| | | 33 | | public bool TryGetNode(Guid id, out TreeNode? node) |
| | | 34 | | { |
| | 81 | 35 | | if (_nodes.TryGetValue(id, out var foundNode)) |
| | | 36 | | { |
| | 72 | 37 | | node = foundNode; |
| | 72 | 38 | | return true; |
| | | 39 | | } |
| | 9 | 40 | | node = null; |
| | 9 | 41 | | return false; |
| | | 42 | | } |
| | | 43 | | |
| | | 44 | | /// <summary> |
| | | 45 | | /// Gets a node by ID, or null if not found. |
| | | 46 | | /// </summary> |
| | | 47 | | public TreeNode? GetNode(Guid id) |
| | | 48 | | { |
| | 1 | 49 | | return _nodes.TryGetValue(id, out var node) ? node : null; |
| | | 50 | | } |
| | | 51 | | |
| | | 52 | | /// <summary> |
| | | 53 | | /// Checks if a node with the given ID exists. |
| | | 54 | | /// </summary> |
| | 6 | 55 | | public bool ContainsNode(Guid id) => _nodes.ContainsKey(id); |
| | | 56 | | |
| | | 57 | | /// <summary> |
| | | 58 | | /// Adds a node to the index. |
| | | 59 | | /// </summary> |
| | | 60 | | public void AddNode(TreeNode node) |
| | | 61 | | { |
| | 69 | 62 | | _nodes[node.Id] = node; |
| | 69 | 63 | | } |
| | | 64 | | |
| | | 65 | | /// <summary> |
| | | 66 | | /// Adds a node to both the index and the root nodes collection. |
| | | 67 | | /// </summary> |
| | | 68 | | public void AddRootNode(TreeNode node) |
| | | 69 | | { |
| | 4 | 70 | | _nodes[node.Id] = node; |
| | 4 | 71 | | _rootNodes.Add(node); |
| | 4 | 72 | | } |
| | | 73 | | |
| | | 74 | | /// <summary> |
| | | 75 | | /// Sets the root nodes collection (used during tree building). |
| | | 76 | | /// </summary> |
| | | 77 | | public void SetRootNodes(List<TreeNode> rootNodes) |
| | | 78 | | { |
| | 1 | 79 | | _rootNodes = rootNodes; |
| | 1 | 80 | | } |
| | | 81 | | |
| | | 82 | | /// <summary> |
| | | 83 | | /// Clears all nodes and root nodes. |
| | | 84 | | /// </summary> |
| | | 85 | | public void Clear() |
| | | 86 | | { |
| | 1 | 87 | | _nodes.Clear(); |
| | 1 | 88 | | _rootNodes = new List<TreeNode>(); |
| | 1 | 89 | | } |
| | | 90 | | |
| | | 91 | | /// <summary> |
| | | 92 | | /// Gets all nodes as an enumerable (for iteration). |
| | | 93 | | /// </summary> |
| | 17 | 94 | | public IEnumerable<TreeNode> AllNodes => _nodes.Values; |
| | | 95 | | |
| | | 96 | | /// <summary> |
| | | 97 | | /// Finds the parent node that contains the given child in its Children collection. |
| | | 98 | | /// This searches through all nodes to find structural parents (for virtual groups, etc.). |
| | | 99 | | /// </summary> |
| | | 100 | | public TreeNode? FindParentNode(TreeNode child) |
| | | 101 | | { |
| | 54 | 102 | | foreach (var node in _nodes.Values) |
| | | 103 | | { |
| | 17 | 104 | | if (node.Children.Contains(child)) |
| | | 105 | | { |
| | 2 | 106 | | return node; |
| | | 107 | | } |
| | | 108 | | } |
| | 9 | 109 | | return null; |
| | 2 | 110 | | } |
| | | 111 | | } |