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:
@@ -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
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user