|
| 1 | +// |
| 2 | +// ConnectionGroupTree.swift |
| 3 | +// TablePro |
| 4 | +// |
| 5 | + |
| 6 | +import Foundation |
| 7 | + |
| 8 | +enum ConnectionGroupTreeNode: Identifiable, Hashable { |
| 9 | + case group(ConnectionGroup, children: [ConnectionGroupTreeNode]) |
| 10 | + case connection(DatabaseConnection) |
| 11 | + |
| 12 | + var id: String { |
| 13 | + switch self { |
| 14 | + case .group(let g, _): "group-\(g.id)" |
| 15 | + case .connection(let c): "conn-\(c.id)" |
| 16 | + } |
| 17 | + } |
| 18 | + |
| 19 | + static func == (lhs: Self, rhs: Self) -> Bool { lhs.id == rhs.id } |
| 20 | + func hash(into hasher: inout Hasher) { hasher.combine(id) } |
| 21 | +} |
| 22 | + |
| 23 | +// MARK: - Tree Building |
| 24 | + |
| 25 | +func buildGroupTree( |
| 26 | + groups: [ConnectionGroup], |
| 27 | + connections: [DatabaseConnection], |
| 28 | + parentId: UUID?, |
| 29 | + maxDepth: Int = 3, |
| 30 | + currentDepth: Int = 0 |
| 31 | +) -> [ConnectionGroupTreeNode] { |
| 32 | + var items: [ConnectionGroupTreeNode] = [] |
| 33 | + |
| 34 | + let validGroupIds = Set(groups.map(\.id)) |
| 35 | + |
| 36 | + let levelGroups: [ConnectionGroup] |
| 37 | + if parentId == nil { |
| 38 | + levelGroups = groups |
| 39 | + .filter { $0.parentId == nil || !validGroupIds.contains($0.parentId!) } |
| 40 | + .sorted { $0.sortOrder != $1.sortOrder ? $0.sortOrder < $1.sortOrder : $0.name.localizedStandardCompare($1.name) == .orderedAscending } |
| 41 | + } else { |
| 42 | + levelGroups = groups |
| 43 | + .filter { $0.parentId == parentId } |
| 44 | + .sorted { $0.sortOrder != $1.sortOrder ? $0.sortOrder < $1.sortOrder : $0.name.localizedStandardCompare($1.name) == .orderedAscending } |
| 45 | + } |
| 46 | + |
| 47 | + for group in levelGroups { |
| 48 | + var children: [ConnectionGroupTreeNode] = [] |
| 49 | + if currentDepth < maxDepth { |
| 50 | + children = buildGroupTree( |
| 51 | + groups: groups, |
| 52 | + connections: connections, |
| 53 | + parentId: group.id, |
| 54 | + maxDepth: maxDepth, |
| 55 | + currentDepth: currentDepth + 1 |
| 56 | + ) |
| 57 | + } |
| 58 | + |
| 59 | + let groupConnections = connections |
| 60 | + .filter { $0.groupId == group.id } |
| 61 | + for conn in groupConnections { |
| 62 | + children.append(.connection(conn)) |
| 63 | + } |
| 64 | + |
| 65 | + items.append(.group(group, children: children)) |
| 66 | + } |
| 67 | + |
| 68 | + if parentId == nil { |
| 69 | + let ungrouped = connections.filter { conn in |
| 70 | + guard let groupId = conn.groupId else { return true } |
| 71 | + return !validGroupIds.contains(groupId) |
| 72 | + } |
| 73 | + for conn in ungrouped { |
| 74 | + items.append(.connection(conn)) |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + return items |
| 79 | +} |
| 80 | + |
| 81 | +// MARK: - Tree Filtering |
| 82 | + |
| 83 | +func filterGroupTree(_ items: [ConnectionGroupTreeNode], searchText: String) -> [ConnectionGroupTreeNode] { |
| 84 | + guard !searchText.isEmpty else { return items } |
| 85 | + |
| 86 | + return items.compactMap { item in |
| 87 | + switch item { |
| 88 | + case .connection(let conn): |
| 89 | + if conn.name.localizedCaseInsensitiveContains(searchText) |
| 90 | + || conn.host.localizedCaseInsensitiveContains(searchText) |
| 91 | + || conn.database.localizedCaseInsensitiveContains(searchText) { |
| 92 | + return item |
| 93 | + } |
| 94 | + return nil |
| 95 | + case .group(let group, let children): |
| 96 | + if group.name.localizedCaseInsensitiveContains(searchText) { |
| 97 | + return item |
| 98 | + } |
| 99 | + let filteredChildren = filterGroupTree(children, searchText: searchText) |
| 100 | + if !filteredChildren.isEmpty { |
| 101 | + return .group(group, children: filteredChildren) |
| 102 | + } |
| 103 | + return nil |
| 104 | + } |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +// MARK: - Tree Traversal |
| 109 | + |
| 110 | +func flattenVisibleConnections( |
| 111 | + tree: [ConnectionGroupTreeNode], |
| 112 | + expandedGroupIds: Set<UUID> |
| 113 | +) -> [DatabaseConnection] { |
| 114 | + var result: [DatabaseConnection] = [] |
| 115 | + for item in tree { |
| 116 | + switch item { |
| 117 | + case .connection(let conn): |
| 118 | + result.append(conn) |
| 119 | + case .group(let group, let children): |
| 120 | + if expandedGroupIds.contains(group.id) { |
| 121 | + result.append(contentsOf: flattenVisibleConnections(tree: children, expandedGroupIds: expandedGroupIds)) |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + return result |
| 126 | +} |
| 127 | + |
| 128 | +func collectAllDescendantGroupIds(groupId: UUID, groups: [ConnectionGroup]) -> Set<UUID> { |
| 129 | + var result = Set<UUID>() |
| 130 | + let directChildren = groups.filter { $0.parentId == groupId } |
| 131 | + for child in directChildren { |
| 132 | + result.insert(child.id) |
| 133 | + result.formUnion(collectAllDescendantGroupIds(groupId: child.id, groups: groups)) |
| 134 | + } |
| 135 | + return result |
| 136 | +} |
| 137 | + |
| 138 | +func wouldCreateCircle(movingGroupId: UUID, toParentId: UUID?, groups: [ConnectionGroup]) -> Bool { |
| 139 | + guard let targetId = toParentId else { return false } |
| 140 | + if targetId == movingGroupId { return true } |
| 141 | + let descendants = collectAllDescendantGroupIds(groupId: movingGroupId, groups: groups) |
| 142 | + return descendants.contains(targetId) |
| 143 | +} |
| 144 | + |
| 145 | +func depthOf(groupId: UUID?, groups: [ConnectionGroup]) -> Int { |
| 146 | + guard let gid = groupId else { return 0 } |
| 147 | + guard let group = groups.first(where: { $0.id == gid }) else { return 0 } |
| 148 | + return 1 + depthOf(groupId: group.parentId, groups: groups) |
| 149 | +} |
| 150 | + |
| 151 | +func maxDescendantDepth(groupId: UUID, groups: [ConnectionGroup]) -> Int { |
| 152 | + let children = groups.filter { $0.parentId == groupId } |
| 153 | + if children.isEmpty { return 0 } |
| 154 | + return 1 + children.map { maxDescendantDepth(groupId: $0.id, groups: groups) }.max()! |
| 155 | +} |
| 156 | + |
| 157 | +func connectionCount(in groupId: UUID, connections: [DatabaseConnection], groups: [ConnectionGroup]) -> Int { |
| 158 | + let directCount = connections.filter { $0.groupId == groupId }.count |
| 159 | + let descendants = collectAllDescendantGroupIds(groupId: groupId, groups: groups) |
| 160 | + let descendantCount = connections.filter { conn in |
| 161 | + guard let gid = conn.groupId else { return false } |
| 162 | + return descendants.contains(gid) |
| 163 | + }.count |
| 164 | + return directCount + descendantCount |
| 165 | +} |
0 commit comments