SPB Git

Repository Public

FolderTree.swift 3.8 KB 81 lines swift

1// SP Notes iOS2// File: Features/Folders/FolderTree.swift3// Description: Builds the folder tree from the flat list of folder pages and answers the questions the screens ask about it4//              (descendants, valid move targets, paths). Pure functions, tested.5//6// Author: Simon-Pierre Boucher7// Contact: contact@spboucher.ai8import Foundation9import SPNotesCore1011struct FolderNode: Identifiable, Equatable {12    var folder: PageSummary13    var children: [FolderNode] = []14    var id: String { folder.id }15    var title: String { NoteTitle.display(folder.title) }16}1718enum FolderTree {19    /// The root folders of each section with their children. A folder whose parent is not a listed folder (a plain note20    /// used as a parent, a parent in the trash) is shown at the root of its section rather than hidden.21    static func build(_ folders: [PageSummary]) -> [String: [FolderNode]] {22        let ids = Set(folders.map(\.id))23        var childrenOf: [String: [PageSummary]] = [:]24        var roots: [PageSummary] = []25        for folder in folders {26            if let parent = folder.parentId, ids.contains(parent), parent != folder.id { childrenOf[parent, default: []].append(folder) } else { roots.append(folder) }27        }28        func node(_ folder: PageSummary, _ visited: Set<String>) -> FolderNode {29            var seen = visited30            seen.insert(folder.id)31            let children = (childrenOf[folder.id] ?? []).filter { !seen.contains($0.id) }.sorted(by: order).map { node($0, seen) }32            return FolderNode(folder: folder, children: children)33        }34        var out: [String: [FolderNode]] = [:]35        for root in roots.sorted(by: order) { out[root.section, default: []].append(node(root, [])) }36        return out37    }3839    private static func order(_ a: PageSummary, _ b: PageSummary) -> Bool {40        if a.position != b.position { return a.position < b.position }41        return NoteTitle.display(a.title).localizedStandardCompare(NoteTitle.display(b.title)) == .orderedAscending42    }4344    /// The folder ids below `id` (and `id` itself).45    static func subtree(of id: String, in folders: [PageSummary]) -> Set<String> {46        var childrenOf: [String: [String]] = [:]47        for folder in folders { if let parent = folder.parentId { childrenOf[parent, default: []].append(folder.id) } }48        var out: Set<String> = [id]49        var queue = [id]50        while let next = queue.popLast() {51            for child in childrenOf[next] ?? [] where out.insert(child).inserted { queue.append(child) }52        }53        return out54    }5556    /// Folders that cannot receive the pages being moved: each moved page itself and everything below it (a folder cannot57    /// go into itself — the store refuses it with `StoreError.cycle`).58    static func invalidTargets(moving ids: [String], folders: [PageSummary]) -> Set<String> {59        ids.reduce(into: Set<String>()) { $0.formUnion(subtree(of: $1, in: folders)) }60    }6162    /// "Parent › Child" for a folder id, used where a folder is shown out of its tree (search filter, move sheet search).63    static func path(of id: String, in folders: [PageSummary]) -> String {64        let byId = Dictionary(folders.map { ($0.id, $0) }, uniquingKeysWith: { a, _ in a })65        var names: [String] = []66        var current = byId[id]67        var guardCount = 068        while let folder = current, guardCount < 64 {69            names.insert(NoteTitle.display(folder.title), at: 0)70            current = folder.parentId.flatMap { byId[$0] }71            guardCount += 172        }73        return names.joined(separator: " › ")74    }7576    /// Depth-first flattening with the depth of each folder (for indented pickers).77    static func flatten(_ nodes: [FolderNode], depth: Int = 0) -> [(node: FolderNode, depth: Int)] {78        nodes.flatMap { [($0, depth)] + flatten($0.children, depth: depth + 1) }79    }80}81
Files main
Menu

Theme