Files
Chronos/src/Titano.App/Archive/ArchiveTree.cs
T
Claude f7affe7840 feat(app): import and archive manager logic (plans, card pool, card gate, server sink, tree, charts, reports)
Plain classes behind the Import and Archive screens: plan totals, session
rows and the Show plan listing; duplicate and title choices without a new
scan; folder and server destinations with the protocol client's chunked,
hash-verified uploads; the after-import gate that allows deleting or
formatting a card only after a verified archive import and a dry run; the
archive tree, chart aggregation, mass rename/retag and maintenance reports.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
2026-10-02 17:39:03 +00:00

131 lines
5.0 KiB
C#

using System.Globalization;
using Titano.App.Infrastructure;
using Titano.Core.Archiving;
using Titano.Core.Localization;
using Titano.Core.Model;
namespace Titano.App.Archiving;
public enum TreeNodeKind
{
Archive,
Year,
Sequence,
}
/// <summary>A node of the archive tree: the archive, a capture year, or a sequence.</summary>
public sealed class ArchiveTreeNode
{
private readonly List<ArchiveTreeNode> _children = [];
internal ArchiveTreeNode(TreeNodeKind kind, string key, int depth)
{
Kind = kind;
Key = key;
Depth = depth;
}
public TreeNodeKind Kind { get; }
/// <summary>Stable across rebuilds: the archive id, the year, or the sequence id.</summary>
public string Key { get; }
public int Depth { get; }
public required string Title { get; init; }
/// <summary>"18 sequences · 2.1 TB", "3,600 photos · 72 GB".</summary>
public required string Detail { get; init; }
public Sequence? Sequence { get; init; }
public bool IsExpanded { get; set; }
public IReadOnlyList<ArchiveTreeNode> Children => _children;
public bool HasChildren => _children.Count > 0;
internal void Add(ArchiveTreeNode child) => _children.Add(child);
/// <summary>The sequences this node stands for (itself, or all below it).</summary>
public IEnumerable<Sequence> Sequences() => Sequence is { } own ? [own] : _children.SelectMany(c => c.Sequences());
}
/// <summary>
/// The Archive screen's tree (BRIEF §12.5): the archive with its availability, its capture years (newest first) and
/// their sequences (newest first), flattened into the visible rows of a virtualised list. Undated sequences sit under
/// their own node after the years.
/// </summary>
public sealed class ArchiveTree
{
private ArchiveTree(ArchiveTreeNode root, ArchiveAvailability availability)
{
Root = root;
Availability = availability;
}
public ArchiveTreeNode Root { get; }
public ArchiveAvailability Availability { get; }
public static ArchiveTree Build(Core.Model.Archive archive, ArchiveAvailability availability, IReadOnlyList<Sequence> sequences)
{
var root = new ArchiveTreeNode(TreeNodeKind.Archive, archive.Id.ToString("N"), 0)
{
Title = archive.Name,
Detail = Summary(sequences),
IsExpanded = true,
};
foreach (var year in sequences.GroupBy(s => s.FirstCapture?.Year).OrderByDescending(g => g.Key ?? int.MinValue))
{
var members = year.OrderByDescending(s => s.FirstCapture).ThenBy(s => s.DisplayTitle, StringComparer.CurrentCultureIgnoreCase).ToList();
var node = new ArchiveTreeNode(TreeNodeKind.Year, year.Key?.ToString(CultureInfo.InvariantCulture) ?? "undated", 1)
{
Title = year.Key?.ToString(CultureInfo.InvariantCulture) ?? Loc.L("Archive.Tree.Undated"),
Detail = Summary(members),
};
foreach (var s in members)
{
node.Add(new ArchiveTreeNode(TreeNodeKind.Sequence, s.Id.ToString("N"), 2)
{
Title = s.DisplayTitle,
Detail = Loc.Plural("Shell.Status.Photos", s.FrameCount) + " · " + Formatting.Bytes(s.TotalBytes),
Sequence = s,
});
}
root.Add(node);
}
return new ArchiveTree(root, availability);
}
/// <summary>Rows to show: every node whose ancestors are all expanded, depth first.</summary>
public IReadOnlyList<ArchiveTreeNode> Visible()
{
var rows = new List<ArchiveTreeNode>();
void Walk(ArchiveTreeNode node)
{
rows.Add(node);
if (!node.IsExpanded) return;
foreach (var child in node.Children) Walk(child);
}
Walk(Root);
return rows;
}
/// <summary>Expands the nodes whose keys are given (restoring the state after a refresh).</summary>
public void Expand(IReadOnlySet<string> keys)
{
foreach (var year in Root.Children) year.IsExpanded = keys.Contains(year.Key);
}
/// <summary>Keys of the expanded year nodes.</summary>
public IReadOnlySet<string> ExpandedKeys() => Root.Children.Where(c => c.IsExpanded).Select(c => c.Key).ToHashSet(StringComparer.Ordinal);
/// <summary>The set mass operations act on: the sequences of the selected nodes, each once; all of them when nothing is selected.</summary>
public static IReadOnlyList<Sequence> SetOf(ArchiveTree tree, IEnumerable<ArchiveTreeNode> selected)
{
var chosen = selected.ToList();
var source = chosen.Count == 0 ? tree.Root.Sequences() : chosen.SelectMany(n => n.Sequences());
var seen = new HashSet<Guid>();
return [.. source.Where(s => seen.Add(s.Id))];
}
private static string Summary(IReadOnlyCollection<Sequence> sequences) =>
Loc.Plural("Shell.Status.Sequences", sequences.Count) + " · " + Formatting.Bytes(sequences.Sum(s => s.TotalBytes));
}