feat(ui): compute dependency-chain step/grouping in TasksIslandViewModel

Slice 1 of dependency-chain display: TaskRowViewModel gains DependsOnTaskId
plus the extrinsic ShowAsChainMember/ChainStep/ChainAfterLabel contract for
Slice 2's rail/badge rendering. Regroup's ClassifyItems walks each row's
DependsOnTaskId chain per section, pulls dependents directly under their
head regardless of SortOrder, and falls back to a flat row + label when the
head isn't in the same section (mirrors the ParentInView precedent).
Planning children never join a chain group - parent indent wins - and only
ever carry the label.

No AXAML changes; that's Slice 2.
This commit is contained in:
mika kuns
2026-08-11 14:38:16 +02:00
parent 0e12be480b
commit 18babe3dee
3 changed files with 368 additions and 0 deletions
@@ -28,12 +28,19 @@ public sealed partial class TaskRowViewModel : ViewModelBase
[ObservableProperty] private string? _parentTaskId;
[ObservableProperty] private string? _createdBy;
[ObservableProperty] private string? _blockedByTaskId;
[ObservableProperty] private string? _dependsOnTaskId;
[ObservableProperty] private bool _isExpanded = true;
[ObservableProperty] private bool _hasPlanningChildren;
[ObservableProperty] private bool _hasQueuedSubtasks;
[ObservableProperty] private bool _showListChip = true;
[ObservableProperty] private bool _parentFinalized;
[ObservableProperty] private bool _parentInView = true;
// The three below are extrinsic — computed and assigned by TasksIslandViewModel.Regroup
// (it alone has the cross-row graph needed to walk DependsOnTaskId chains), mirroring how
// ParentInView is assigned by the same pass rather than derived locally.
[ObservableProperty] private bool _showAsChainMember;
[ObservableProperty] private int? _chainStep;
[ObservableProperty] private string? _chainAfterLabel;
[ObservableProperty] private int _roadblockCount;
// Only meaningful when Status=Failed; stamped by TaskRunner.MarkFailed. "unknown" for a
// Failed task that predates this field. Null on every other status.
@@ -370,6 +377,7 @@ public sealed partial class TaskRowViewModel : ViewModelBase
ParentTaskId = t.ParentTaskId;
CreatedBy = t.CreatedBy;
BlockedByTaskId = t.BlockedByTaskId;
DependsOnTaskId = t.DependsOnTaskId;
RoadblockCount = t.RoadblockCount;
FailureReason = t.FailureReason;
FailureTurnsUsed = t.FailureTurnsUsed;
@@ -580,9 +580,124 @@ public sealed partial class TasksIslandViewModel : ViewModelBase, IDisposable
open.Add(r);
}
// Dependency chains are resolved and pulled together per section (not on the
// pre-split `flat` list): a chain head might land in a different section than its
// dependent (e.g. a Done head in Completed, an open dependent in Open) — in that case
// the two are never rendered adjacent, so the dependent must fall back to a flat row
// with a label rather than an orphaned rail. Whether the head is resolvable at all
// still uses the whole-Items graph (rowsById), independent of section.
var rowsById = Items.ToDictionary(r => r.Id);
overdue = ApplyChainGrouping(overdue, rowsById);
open = ApplyChainGrouping(open, rowsById);
completed = ApplyChainGrouping(completed, rowsById);
return (overdue, open, completed);
}
// Cycles are rejected by TaskStateService.SetDependsOnAsync, so this should always
// terminate quickly — capped anyway against a corrupt/legacy row forming a loop.
private const int MaxChainWalkDepth = 64;
private readonly record struct ChainWalkResult(TaskRowViewModel Head, int Step);
// Walks DependsOnTaskId back to its root. Returns null when the chain can't be resolved —
// either the row has no predecessor at all, or a predecessor along the way isn't loaded
// into the current Items (e.g. filtered out of this list/view).
private static ChainWalkResult? WalkChainHead(TaskRowViewModel row, Dictionary<string, TaskRowViewModel> rowsById)
{
var current = row;
var step = 0;
var visited = new HashSet<string> { row.Id };
while (!string.IsNullOrEmpty(current.DependsOnTaskId))
{
if (!rowsById.TryGetValue(current.DependsOnTaskId, out var predecessor)) return null;
if (step >= MaxChainWalkDepth || !visited.Add(predecessor.Id)) return null;
current = predecessor;
step++;
}
return step == 0 ? null : new ChainWalkResult(current, step);
}
// Content only — "after " + localization is Slice 2's concern (the view), which is why this
// holds just the identifying fragment rather than an assembled sentence.
private static string? BuildAfterLabel(TaskRowViewModel row, Dictionary<string, TaskRowViewModel> rowsById)
{
if (string.IsNullOrEmpty(row.DependsOnTaskId)) return null;
if (!rowsById.TryGetValue(row.DependsOnTaskId, out var predecessor)) return null;
return predecessor.Number > 0 ? $"#{predecessor.Number}" : predecessor.Title;
}
// Assigns ShowAsChainMember/ChainStep/ChainAfterLabel for every row in `section`, then
// returns a re-ordered copy with chain dependents pulled directly under their head
// (ascending ChainStep), regardless of their SortOrder-derived position — mirroring how
// planning children already sit right after their parent regardless of Items order.
private static List<TaskRowViewModel> ApplyChainGrouping(
List<TaskRowViewModel> section, Dictionary<string, TaskRowViewModel> rowsById)
{
var sectionIds = section.Select(r => r.Id).ToHashSet();
var headIdOf = new Dictionary<TaskRowViewModel, string>();
var stepOf = new Dictionary<TaskRowViewModel, int>();
foreach (var r in section)
{
if (string.IsNullOrEmpty(r.DependsOnTaskId))
{
r.ShowAsChainMember = false;
r.ChainStep = null;
r.ChainAfterLabel = null;
continue;
}
// A planning child keeps its parent indent — chain membership only ever shows as a
// label for it, never as a second, nested rail (design: "parent wins").
if (r.IsChild)
{
r.ShowAsChainMember = false;
r.ChainStep = null;
r.ChainAfterLabel = BuildAfterLabel(r, rowsById);
continue;
}
var walk = WalkChainHead(r, rowsById);
if (walk is { } w && sectionIds.Contains(w.Head.Id))
{
r.ShowAsChainMember = true;
r.ChainStep = w.Step;
r.ChainAfterLabel = null;
headIdOf[r] = w.Head.Id;
stepOf[r] = w.Step;
}
else
{
r.ShowAsChainMember = false;
r.ChainStep = null;
r.ChainAfterLabel = BuildAfterLabel(r, rowsById);
}
}
if (headIdOf.Count == 0) return section;
var membersByHeadId = section
.Where(headIdOf.ContainsKey)
.GroupBy(r => headIdOf[r])
.ToDictionary(g => g.Key, g => g.OrderBy(r => stepOf[r]).ToList());
var ordered = new List<TaskRowViewModel>(section.Count);
var consumed = new HashSet<string>();
foreach (var r in section)
{
if (consumed.Contains(r.Id)) continue;
if (headIdOf.ContainsKey(r)) continue; // placed via its head below, in step order
ordered.Add(r);
consumed.Add(r.Id);
if (membersByHeadId.TryGetValue(r.Id, out var members))
foreach (var m in members)
if (consumed.Add(m.Id))
ordered.Add(m);
}
return ordered;
}
private void UpdateSubtitle()
{
var now = DateTime.Now;