Add nested (hierarchical) conversation folders
Folders can now contain other folders, arbitrarily deep — e.g. "Work" containing "Project A"/"Project B". v10 migration adds a self-referencing parentId column; tree ordering, depth, and cycle detection are pure Swift (Folder.orderedTree/isDescendant/ visibleFolderIds), not SQL, so listFolders() stays a simple flat query. - Create nested folders via "New Subfolder…" (context menu, both list views) or by dragging a folder onto another to reparent it. Dragging onto an existing descendant is rejected (cycle guard). - Deleting a folder reparents its children and any conversations filed directly in it up one level to the deleted folder's own parent — conversations are never deleted. This also fixes a real bug: the previous deleteFolder never persisted unfiling to the database, only patched in-memory state, so a conversation whose folder was deleted kept a dangling folderId and silently vanished from view after the next relaunch. - All "Move to Folder" pickers (sidebar, advanced list, per-row context menus, the Save dialog's folder popup) show an indented flat list reflecting the tree. - New DraggedItem enum disambiguates a dragged folder from dragged conversation(s) in the shared string-based drag payload, and unifies both list views on the same bundled-multi-selection format — closes a gap where dragging a multi-selection in the advanced list (⌘L) only moved the one row grabbed, unlike the sidebar. Confirmed working live, including relaunch-survival of the delete/reparent fix.
This commit is contained in:
+55
-1
@@ -28,16 +28,70 @@ struct Folder: Identifiable, Codable, Sendable {
|
||||
var name: String
|
||||
var sortOrder: Int
|
||||
let createdAt: Date
|
||||
var parentId: UUID?
|
||||
|
||||
nonisolated init(
|
||||
id: UUID = UUID(),
|
||||
name: String,
|
||||
sortOrder: Int = 0,
|
||||
createdAt: Date = Date()
|
||||
createdAt: Date = Date(),
|
||||
parentId: UUID? = nil
|
||||
) {
|
||||
self.id = id
|
||||
self.name = name
|
||||
self.sortOrder = sortOrder
|
||||
self.createdAt = createdAt
|
||||
self.parentId = parentId
|
||||
}
|
||||
}
|
||||
|
||||
extension Folder {
|
||||
/// Depth-first, indented ordering for flat-list display. Assumes `folders` already has the
|
||||
/// desired sibling order (e.g. listFolders()'s alphabetical order) — only re-groups by
|
||||
/// parent/child, preserving each existing sibling ordering.
|
||||
nonisolated static func orderedTree(from folders: [Folder]) -> [(folder: Folder, depth: Int)] {
|
||||
var childrenByParent: [UUID?: [Folder]] = [:]
|
||||
for folder in folders {
|
||||
childrenByParent[folder.parentId, default: []].append(folder)
|
||||
}
|
||||
var result: [(folder: Folder, depth: Int)] = []
|
||||
func walk(parentId: UUID?, depth: Int, visiting: Set<UUID>) {
|
||||
for folder in childrenByParent[parentId] ?? [] {
|
||||
guard !visiting.contains(folder.id) else { continue } // defensive cycle guard
|
||||
result.append((folder, depth))
|
||||
walk(parentId: folder.id, depth: depth + 1, visiting: visiting.union([folder.id]))
|
||||
}
|
||||
}
|
||||
walk(parentId: nil, depth: 0, visiting: [])
|
||||
return result
|
||||
}
|
||||
|
||||
/// True if `candidateId` is `ancestorId` itself, or nested anywhere below it. A single call
|
||||
/// `isDescendant(target.id, of: source.id, in: folders)` rejects both a self-drop (target ==
|
||||
/// source) and any deeper cycle (target currently lives under source).
|
||||
nonisolated static func isDescendant(_ candidateId: UUID, of ancestorId: UUID, in folders: [Folder]) -> Bool {
|
||||
var current: UUID? = candidateId
|
||||
var visited: Set<UUID> = []
|
||||
while let id = current, !visited.contains(id) {
|
||||
if id == ancestorId { return true }
|
||||
visited.insert(id)
|
||||
current = folders.first(where: { $0.id == id })?.parentId
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
/// Given an ordered tree and the set of explicitly-collapsed folder ids, returns ids whose
|
||||
/// header should still render — collapsing a folder hides its whole subtree, but its own
|
||||
/// header stays visible so it can be expanded again.
|
||||
nonisolated static func visibleFolderIds(tree: [(folder: Folder, depth: Int)], collapsed: Set<UUID>) -> Set<UUID> {
|
||||
var visible: Set<UUID> = []
|
||||
var hiddenAtOrBelowDepth: Int? = nil
|
||||
for (folder, depth) in tree {
|
||||
if let hiddenDepth = hiddenAtOrBelowDepth, depth > hiddenDepth { continue }
|
||||
hiddenAtOrBelowDepth = nil
|
||||
visible.insert(folder.id)
|
||||
if collapsed.contains(folder.id) { hiddenAtOrBelowDepth = depth }
|
||||
}
|
||||
return visible
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user