feat: add GroupSelection subtree select/deselect logic

Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WYqycDFsynHH9VnnK7LNSf
This commit is contained in:
2026-09-19 16:57:49 +02:00
co-authored by Claude Haiku 4.5
parent 5ca42a4e2a
commit 62835dcd38
2 changed files with 80 additions and 0 deletions
@@ -0,0 +1,38 @@
import Foundation
public enum GroupSelection {
/// `groupId` and all of its descendant subgroup IDs, found by searching `root`'s tree.
/// Returns an empty set if `groupId` isn't found anywhere in `root`.
public static func subtreeIds(of groupId: UUID, in root: Group) -> Set<UUID> {
guard let found = findGroup(groupId, in: root) else { return [] }
return allIds(in: found)
}
/// Toggles `groupId` and its full subtree in `current`: if the group is already selected,
/// it and its subtree are removed; otherwise they're added.
public static func toggling(_ groupId: UUID, in root: Group, current: Set<UUID>) -> Set<UUID> {
let subtree = subtreeIds(of: groupId, in: root)
guard !subtree.isEmpty else { return current }
if current.contains(groupId) {
return current.subtracting(subtree)
} else {
return current.union(subtree)
}
}
private static func findGroup(_ id: UUID, in group: Group) -> Group? {
if group.id == id { return group }
for sub in group.subgroups {
if let found = findGroup(id, in: sub) { return found }
}
return nil
}
private static func allIds(in group: Group) -> Set<UUID> {
var ids: Set<UUID> = [group.id]
for sub in group.subgroups {
ids.formUnion(allIds(in: sub))
}
return ids
}
}