// Groups (Spec 6, note 8c089dd6): a table whose records the owner gathers // into named, hand-picked groups says so with `groups`, naming the existing // face its stacks are piled from — a tool's groups are a row of small piles // above its list, photographs for People and paper slips for Todos. // // A group is a record of the `group` table and lives in its own tool: one // "Family", read by the People list, Home and anything else that names // it. A record may be in many groups. Every group is hand-picked; there is no // other kind. // // The reading and the verbs live here, in one place, so the surfaces only // draw. The core answers them (`groups.list`, `groups.create`, // `groups.update`, `groups.delete`): a table's groups with the first three // records of each, a group made from a name (and the records to start it // with), ONE change per write — a rename, records put in, records taken out — // each a single change with its receipt and its Undo, and a delete that // leaves the records where they are. A group's records are the table's own // list, narrowed by `group` (`records.list`). import CottonKit import CottonValues import Foundation /// A table's `groups`: its records can be gathered into named groups. public typealias TableGroups = DefinitionGroups extension DefinitionGroups { public typealias Stack = DefinitionStackFace public typealias KeptIn = DefinitionGroupsKeptIn public init(stack: Stack, keptIn: KeptIn? = nil) { self.init(_generated: (), stack: stack) self.keptIn = keptIn } } extension DefinitionGroupsKeptIn { public init(table: String, items: String) { self.init(_generated: (), table: table, items: items) } } /// One group, as the core answers it. public struct RecordGroup: Identifiable, Hashable, Sendable, Decodable { public var id: String public var name: String /// The table whose records it gathers. public var of: String /// How many records it holds. public var count: Int? /// The table the group is a record of, when its groups are kept in one /// (`TableGroups.keptIn`): an album, whose page opens it. public var keptIn: String? /// The word that record's slip wears, drawn under its stack in red: an /// album Cotton suggested says "suggested". public var byline: String? /// A filter group's `where` — the Email list's Junk stack is /// `{sender_kind: junk}`, and offers Empty Junk now on its page. public var filter: [String: RecordValue]? enum CodingKeys: String, CodingKey { case id, name, count, byline case of = "table" case keptIn = "kept_in" case filter = "where" } public init( id: String, name: String, of: String, count: Int? = nil, keptIn: String? = nil, byline: String? = nil, filter: [String: RecordValue]? = nil ) { self.id = id self.name = name self.of = of self.count = count self.keptIn = keptIn self.byline = byline self.filter = filter } } /// A group as a stack: the group, the first few of its records as the /// table's own records, and — when the read named a record — whether the /// group holds it. public struct GroupStack: Identifiable, Hashable, Sendable { public var group: RecordGroup public var top: [TableRecord] public var holds: Bool? public var id: String { group.id } public init(group: RecordGroup, top: [TableRecord] = [], holds: Bool? = nil) { self.group = group self.top = top self.holds = holds } } /// The ONE thing a write changes about a group. public enum GroupChange: Hashable, Sendable { case name(String) /// Records put in, after the last. case add([String]) /// Records taken out; the records themselves are untouched. case remove([String]) /// Members in their new order, the rest after them as they stood — a /// drag on the group's own page, which never touches the table's order. case order([String]) /// The write's request: the group's id and exactly one of the keys. public func request(id: String) -> [String: RecordValue] { switch self { case .name(let name): return ["id": .text(id), "name": .text(name)] case .add(let ids): return ["id": .text(id), "add": .list(ids.map(RecordValue.text))] case .remove(let ids): return ["id": .text(id), "remove": .list(ids.map(RecordValue.text))] case .order(let ids): return ["id": .text(id), "order": .list(ids.map(RecordValue.text))] } } } /// A store that keeps groups (the core's). A store that does not — the /// fixtures, Settings' own tables — draws no stacks and offers no group act. public protocol GroupingRecords: AnyObject, Sendable { /// The groups of `table`, in the order they were made; with `record`, /// each says whether it holds that record. func groups(of table: String, holding record: String?) async throws -> [GroupStack] /// Makes a group, holding `records` in order when given. func createGroup(named name: String, of table: String, records: [String]) async throws -> (change: ChangeRecord, group: RecordGroup?) func updateGroup(_ id: String, _ change: GroupChange) async throws -> ChangeRecord /// Deletes the group; its records stay where they are. func deleteGroup(_ id: String) async throws -> ChangeRecord } /// What the surfaces ask of groups, over whichever store is in the environment. public enum GroupRecords { /// The `where` key that narrows a table's list to one group's records. public static let memberKey = "group" /// What narrows a table's list to `group`'s records, in the group's order. public static func members(of group: RecordGroup) -> [String: RecordValue] { [memberKey: .text(group.id)] } /// The store's groups half, or nil when it keeps none. public static func grouping(_ store: (any Records)?) -> (any GroupingRecords)? { store as? any GroupingRecords } /// A group's name as typed: trimmed, nil when nothing is left. public static func name(typed: String) -> String? { let name = typed.trimmingCharacters(in: .whitespacesAndNewlines) return name.isEmpty ? nil : name } }