mirror of
https://github.com/bitwarden/android.git
synced 2024-10-31 23:25:45 +03:00
nested tree node traversal helpers
This commit is contained in:
parent
8175af4e84
commit
8611501423
3 changed files with 89 additions and 0 deletions
8
src/App/Abstractions/ITreeNodeObject.cs
Normal file
8
src/App/Abstractions/ITreeNodeObject.cs
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
namespace Bit.App.Abstractions
|
||||||
|
{
|
||||||
|
public interface ITreeNodeObject
|
||||||
|
{
|
||||||
|
string Id { get; set; }
|
||||||
|
string Name { get; set; }
|
||||||
|
}
|
||||||
|
}
|
19
src/App/Models/TreeNode.cs
Normal file
19
src/App/Models/TreeNode.cs
Normal 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>>();
|
||||||
|
}
|
||||||
|
}
|
|
@ -569,5 +569,67 @@ namespace Bit.App.Utilities
|
||||||
var appSettingsService = Resolver.Resolve<IAppSettingsService>();
|
var appSettingsService = Resolver.Resolve<IAppSettingsService>();
|
||||||
return appSettingsService?.OrganizationGivesPremium ?? false;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue