1
0
mirror of https://github.com/bitwarden/mobile.git synced 2024-12-24 16:38:22 +01:00

nested tree node traversal helpers

This commit is contained in:
Kyle Spearrin 2018-11-29 19:42:24 -05:00
parent 8175af4e84
commit 8611501423
3 changed files with 89 additions and 0 deletions

View File

@ -0,0 +1,8 @@
namespace Bit.App.Abstractions
{
public interface ITreeNodeObject
{
string Id { get; set; }
string Name { get; set; }
}
}

View File

@ -0,0 +1,19 @@
using Bit.App.Abstractions;
using System.Collections.Generic;
namespace Bit.App.Models
{
public class TreeNode<T> where T : ITreeNodeObject
{
public TreeNode(T node, string name, T parent)
{
Parent = parent;
Node = node;
Node.Name = name;
}
public T Parent { get; set; }
public T Node { get; set; }
public List<TreeNode<T>> Children { get; set; } = new List<TreeNode<T>>();
}
}

View File

@ -569,5 +569,67 @@ namespace Bit.App.Utilities
var appSettingsService = Resolver.Resolve<IAppSettingsService>();
return appSettingsService?.OrganizationGivesPremium ?? false;
}
public static void NestedTraverse(List<TreeNode<ITreeNodeObject>> nodeTree, int partIndex, string[] parts,
ITreeNodeObject obj, ITreeNodeObject parent, string delimiter)
{
if(parts.Length <= partIndex)
{
return;
}
var end = partIndex == parts.Length - 1;
var partName = parts[partIndex];
foreach(var n in nodeTree)
{
if(n.Node.Name != parts[partIndex])
{
continue;
}
if(end && n.Node.Id != obj.Id)
{
// Another node with the same name.
nodeTree.Add(new TreeNode<ITreeNodeObject>(obj, partName, parent));
return;
}
NestedTraverse(n.Children, partIndex + 1, parts, obj, n.Node, delimiter);
return;
}
if(!nodeTree.Any(n => n.Node.Name == partName))
{
if(end)
{
nodeTree.Add(new TreeNode<ITreeNodeObject>(obj, partName, parent));
return;
}
var newPartName = string.Concat(parts[partIndex], delimiter, parts[partIndex + 1]);
var newParts = new List<string> { newPartName };
var newPartsStartFrom = partIndex + 2;
newParts.AddRange(new ArraySegment<string>(parts, newPartsStartFrom, parts.Length - newPartsStartFrom));
NestedTraverse(nodeTree, 0, newParts.ToArray(), obj, parent, delimiter);
}
}
public static TreeNode<ITreeNodeObject> GetTreeNodeObject(List<TreeNode<ITreeNodeObject>> nodeTree, string id)
{
foreach(var n in nodeTree)
{
if(n.Node.Id == id)
{
return n;
}
else if(n.Children != null)
{
var node = GetTreeNodeObject(n.Children, id);
if(node != null)
{
return node;
}
}
}
return null;
}
}
}