2024-03-17 18:37:06 -07:00
|
|
|
|
using System;
|
2024-02-05 23:08:37 -08:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
using CommunityToolkit.Mvvm.ComponentModel;
|
|
|
|
|
|
|
|
|
|
namespace SourceGit.ViewModels
|
|
|
|
|
{
|
|
|
|
|
public class FileTreeNode : ObservableObject
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
public string FullPath { get; set; } = string.Empty;
|
|
|
|
|
public bool IsFolder { get; set; } = false;
|
|
|
|
|
public object Backend { get; set; } = null;
|
|
|
|
|
public List<FileTreeNode> Children { get; set; } = new List<FileTreeNode>();
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
public bool IsExpanded
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
get => _isExpanded;
|
|
|
|
|
set => SetProperty(ref _isExpanded, value);
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-28 06:19:53 -07:00
|
|
|
|
public static List<FileTreeNode> Build(List<Models.Change> changes, bool expanded)
|
2024-03-17 18:37:06 -07:00
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
var nodes = new List<FileTreeNode>();
|
|
|
|
|
var folders = new Dictionary<string, FileTreeNode>();
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
foreach (var c in changes)
|
|
|
|
|
{
|
2024-03-13 20:09:05 -07:00
|
|
|
|
var sepIdx = c.Path.IndexOf('/', StringComparison.Ordinal);
|
2024-03-17 18:37:06 -07:00
|
|
|
|
if (sepIdx == -1)
|
|
|
|
|
{
|
|
|
|
|
nodes.Add(new FileTreeNode()
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
FullPath = c.Path,
|
|
|
|
|
Backend = c,
|
|
|
|
|
IsFolder = false,
|
|
|
|
|
IsExpanded = false
|
|
|
|
|
});
|
2024-03-17 18:37:06 -07:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
FileTreeNode lastFolder = null;
|
|
|
|
|
var start = 0;
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
while (sepIdx != -1)
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
var folder = c.Path.Substring(0, sepIdx);
|
2024-03-31 01:54:29 -07:00
|
|
|
|
if (folders.TryGetValue(folder, out var value))
|
2024-03-17 18:37:06 -07:00
|
|
|
|
{
|
2024-03-31 01:54:29 -07:00
|
|
|
|
lastFolder = value;
|
2024-03-17 18:37:06 -07:00
|
|
|
|
}
|
|
|
|
|
else if (lastFolder == null)
|
|
|
|
|
{
|
|
|
|
|
lastFolder = new FileTreeNode()
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
FullPath = folder,
|
|
|
|
|
Backend = null,
|
|
|
|
|
IsFolder = true,
|
|
|
|
|
IsExpanded = expanded
|
|
|
|
|
};
|
|
|
|
|
nodes.Add(lastFolder);
|
|
|
|
|
folders.Add(folder, lastFolder);
|
2024-03-17 18:37:06 -07:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
var cur = new FileTreeNode()
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
FullPath = folder,
|
|
|
|
|
Backend = null,
|
|
|
|
|
IsFolder = true,
|
|
|
|
|
IsExpanded = expanded
|
|
|
|
|
};
|
|
|
|
|
folders.Add(folder, cur);
|
|
|
|
|
lastFolder.Children.Add(cur);
|
|
|
|
|
lastFolder = cur;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
start = sepIdx + 1;
|
|
|
|
|
sepIdx = c.Path.IndexOf('/', start);
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
lastFolder.Children.Add(new FileTreeNode()
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
FullPath = c.Path,
|
|
|
|
|
Backend = c,
|
|
|
|
|
IsFolder = false,
|
|
|
|
|
IsExpanded = false
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
folders.Clear();
|
|
|
|
|
Sort(nodes);
|
|
|
|
|
return nodes;
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-28 06:19:53 -07:00
|
|
|
|
public static List<FileTreeNode> Build(List<Models.Object> files, bool expanded)
|
2024-03-17 18:37:06 -07:00
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
var nodes = new List<FileTreeNode>();
|
|
|
|
|
var folders = new Dictionary<string, FileTreeNode>();
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
foreach (var f in files)
|
|
|
|
|
{
|
2024-03-13 20:09:05 -07:00
|
|
|
|
var sepIdx = f.Path.IndexOf('/', StringComparison.Ordinal);
|
2024-03-17 18:37:06 -07:00
|
|
|
|
if (sepIdx == -1)
|
|
|
|
|
{
|
|
|
|
|
nodes.Add(new FileTreeNode()
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
FullPath = f.Path,
|
|
|
|
|
Backend = f,
|
|
|
|
|
IsFolder = false,
|
|
|
|
|
IsExpanded = false
|
|
|
|
|
});
|
2024-03-17 18:37:06 -07:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
FileTreeNode lastFolder = null;
|
|
|
|
|
var start = 0;
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
while (sepIdx != -1)
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
var folder = f.Path.Substring(0, sepIdx);
|
2024-03-31 01:54:29 -07:00
|
|
|
|
if (folders.TryGetValue(folder, out var value))
|
2024-03-17 18:37:06 -07:00
|
|
|
|
{
|
2024-03-31 01:54:29 -07:00
|
|
|
|
lastFolder = value;
|
2024-03-17 18:37:06 -07:00
|
|
|
|
}
|
|
|
|
|
else if (lastFolder == null)
|
|
|
|
|
{
|
|
|
|
|
lastFolder = new FileTreeNode()
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
FullPath = folder,
|
|
|
|
|
Backend = null,
|
|
|
|
|
IsFolder = true,
|
|
|
|
|
IsExpanded = expanded
|
|
|
|
|
};
|
|
|
|
|
nodes.Add(lastFolder);
|
|
|
|
|
folders.Add(folder, lastFolder);
|
2024-03-17 18:37:06 -07:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
var cur = new FileTreeNode()
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
FullPath = folder,
|
|
|
|
|
Backend = null,
|
|
|
|
|
IsFolder = true,
|
|
|
|
|
IsExpanded = expanded
|
|
|
|
|
};
|
|
|
|
|
folders.Add(folder, cur);
|
|
|
|
|
lastFolder.Children.Add(cur);
|
|
|
|
|
lastFolder = cur;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
start = sepIdx + 1;
|
|
|
|
|
sepIdx = f.Path.IndexOf('/', start);
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
lastFolder.Children.Add(new FileTreeNode()
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
FullPath = f.Path,
|
|
|
|
|
Backend = f,
|
|
|
|
|
IsFolder = false,
|
|
|
|
|
IsExpanded = false
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
folders.Clear();
|
|
|
|
|
Sort(nodes);
|
|
|
|
|
return nodes;
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
public static FileTreeNode SelectByPath(List<FileTreeNode> nodes, string path)
|
|
|
|
|
{
|
|
|
|
|
foreach (var node in nodes)
|
|
|
|
|
{
|
2024-03-31 01:54:29 -07:00
|
|
|
|
if (node.FullPath == path)
|
|
|
|
|
return node;
|
2024-03-17 18:37:06 -07:00
|
|
|
|
|
|
|
|
|
if (node.IsFolder && path.StartsWith(node.FullPath + "/", StringComparison.Ordinal))
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
var foundInChildren = SelectByPath(node.Children, path);
|
2024-03-17 18:37:06 -07:00
|
|
|
|
if (foundInChildren != null)
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
node.IsExpanded = true;
|
|
|
|
|
}
|
|
|
|
|
return foundInChildren;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
private static void Sort(List<FileTreeNode> nodes)
|
|
|
|
|
{
|
|
|
|
|
nodes.Sort((l, r) =>
|
|
|
|
|
{
|
|
|
|
|
if (l.IsFolder == r.IsFolder)
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
return l.FullPath.CompareTo(r.FullPath);
|
2024-03-17 18:37:06 -07:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
return l.IsFolder ? -1 : 1;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
foreach (var node in nodes)
|
|
|
|
|
{
|
2024-03-31 01:54:29 -07:00
|
|
|
|
if (node.Children.Count > 1)
|
|
|
|
|
Sort(node.Children);
|
2024-02-05 23:08:37 -08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private bool _isExpanded = true;
|
|
|
|
|
}
|
2024-03-31 01:54:29 -07:00
|
|
|
|
}
|