|
| 1 | +// |
| 2 | +// GroupStorage.swift |
| 3 | +// TablePro |
| 4 | +// |
| 5 | + |
| 6 | +import Foundation |
| 7 | +import os |
| 8 | + |
| 9 | +/// Service for persisting connection groups |
| 10 | +final class GroupStorage { |
| 11 | + static let shared = GroupStorage() |
| 12 | + private static let logger = Logger(subsystem: "com.TablePro", category: "GroupStorage") |
| 13 | + |
| 14 | + private let groupsKey = "com.TablePro.groups" |
| 15 | + private let expandedGroupsKey = "com.TablePro.expandedGroups" |
| 16 | + private let defaults = UserDefaults.standard |
| 17 | + private let encoder = JSONEncoder() |
| 18 | + private let decoder = JSONDecoder() |
| 19 | + |
| 20 | + private init() {} |
| 21 | + |
| 22 | + // MARK: - Group CRUD |
| 23 | + |
| 24 | + /// Load all groups |
| 25 | + func loadGroups() -> [ConnectionGroup] { |
| 26 | + guard let data = defaults.data(forKey: groupsKey) else { |
| 27 | + return [] |
| 28 | + } |
| 29 | + |
| 30 | + do { |
| 31 | + return try decoder.decode([ConnectionGroup].self, from: data) |
| 32 | + } catch { |
| 33 | + Self.logger.error("Failed to load groups: \(error)") |
| 34 | + return [] |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + /// Save all groups |
| 39 | + func saveGroups(_ groups: [ConnectionGroup]) { |
| 40 | + do { |
| 41 | + let data = try encoder.encode(groups) |
| 42 | + defaults.set(data, forKey: groupsKey) |
| 43 | + } catch { |
| 44 | + Self.logger.error("Failed to save groups: \(error)") |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + /// Add a new group |
| 49 | + func addGroup(_ group: ConnectionGroup) { |
| 50 | + var groups = loadGroups() |
| 51 | + groups.append(group) |
| 52 | + saveGroups(groups) |
| 53 | + } |
| 54 | + |
| 55 | + /// Update an existing group |
| 56 | + func updateGroup(_ group: ConnectionGroup) { |
| 57 | + var groups = loadGroups() |
| 58 | + if let index = groups.firstIndex(where: { $0.id == group.id }) { |
| 59 | + groups[index] = group |
| 60 | + saveGroups(groups) |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + /// Delete a group and all its descendants. |
| 65 | + /// Member connections become ungrouped. |
| 66 | + func deleteGroup(_ group: ConnectionGroup) { |
| 67 | + var groups = loadGroups() |
| 68 | + let deletedIds = collectDescendantIds(of: group.id, in: groups) |
| 69 | + let allDeletedIds = deletedIds.union([group.id]) |
| 70 | + |
| 71 | + // Remove deleted groups |
| 72 | + groups.removeAll { allDeletedIds.contains($0.id) } |
| 73 | + saveGroups(groups) |
| 74 | + |
| 75 | + // Ungroup connections that belonged to deleted groups |
| 76 | + let storage = ConnectionStorage.shared |
| 77 | + var connections = storage.loadConnections() |
| 78 | + var changed = false |
| 79 | + for index in connections.indices { |
| 80 | + if let gid = connections[index].groupId, allDeletedIds.contains(gid) { |
| 81 | + connections[index].groupId = nil |
| 82 | + changed = true |
| 83 | + } |
| 84 | + } |
| 85 | + if changed { |
| 86 | + storage.saveConnections(connections) |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + /// Get group by ID |
| 91 | + func group(for id: UUID) -> ConnectionGroup? { |
| 92 | + loadGroups().first { $0.id == id } |
| 93 | + } |
| 94 | + |
| 95 | + /// Get child groups of a parent, sorted by sortOrder |
| 96 | + func childGroups(of parentId: UUID?) -> [ConnectionGroup] { |
| 97 | + loadGroups() |
| 98 | + .filter { $0.parentGroupId == parentId } |
| 99 | + .sorted { $0.sortOrder < $1.sortOrder } |
| 100 | + } |
| 101 | + |
| 102 | + /// Get the next sort order for a new item in a parent context |
| 103 | + func nextSortOrder(parentId: UUID?) -> Int { |
| 104 | + let siblings = loadGroups().filter { $0.parentGroupId == parentId } |
| 105 | + return (siblings.map(\.sortOrder).max() ?? -1) + 1 |
| 106 | + } |
| 107 | + |
| 108 | + // MARK: - Expanded State |
| 109 | + |
| 110 | + /// Load the set of expanded group IDs |
| 111 | + func loadExpandedGroupIds() -> Set<UUID> { |
| 112 | + guard let data = defaults.data(forKey: expandedGroupsKey) else { |
| 113 | + return [] |
| 114 | + } |
| 115 | + |
| 116 | + do { |
| 117 | + let ids = try decoder.decode([UUID].self, from: data) |
| 118 | + return Set(ids) |
| 119 | + } catch { |
| 120 | + Self.logger.error("Failed to load expanded groups: \(error)") |
| 121 | + return [] |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + /// Save the set of expanded group IDs |
| 126 | + func saveExpandedGroupIds(_ ids: Set<UUID>) { |
| 127 | + do { |
| 128 | + let data = try encoder.encode(Array(ids)) |
| 129 | + defaults.set(data, forKey: expandedGroupsKey) |
| 130 | + } catch { |
| 131 | + Self.logger.error("Failed to save expanded groups: \(error)") |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + // MARK: - Helpers |
| 136 | + |
| 137 | + /// Recursively collect all descendant group IDs |
| 138 | + private func collectDescendantIds(of groupId: UUID, in groups: [ConnectionGroup]) -> Set<UUID> { |
| 139 | + var result = Set<UUID>() |
| 140 | + let children = groups.filter { $0.parentGroupId == groupId } |
| 141 | + for child in children { |
| 142 | + result.insert(child.id) |
| 143 | + result.formUnion(collectDescendantIds(of: child.id, in: groups)) |
| 144 | + } |
| 145 | + return result |
| 146 | + } |
| 147 | +} |
0 commit comments