Cotton code
66 lines · 3.2 KB · SwiftRaw
// A link arriving in a window: its database first, then its route.//// Every way in — the phone's `onOpenURL`, the Mac's `DeepLinkInbox`, a// notification tap — reaches `PendingTaps` as a `DatabaseLink`, and// `RootView` hands each one here before the route is opened. The rule is// `DatabaseLinkArrival.decide` (`CottonKit/DatabaseLink.swift`); this is what// the window does with its answer.import CottonKitimport Foundation/// Places links in ONE window: the window's own `DatabaseScene`, the app's/// held list, and the app's toasts.@MainActorstruct DatabaseLinkPlacing {    let model: DatabasesModel    let scene: DatabaseScene    let toasts: ToastCenter    /// The core a link that names no database asks each held database    /// through, whether it holds the link's record; nil where there is none    /// (a gallery specimen), when such a link opens where the window is.    var core: (any CottonCalling)? = nil    /// Show the link's database in this window — which also makes it the last    /// one used — and answer the route to open. Nil when nothing opens.    func place(_ link: DatabaseLink) async -> Route? {        // A cold-start link can land before the window has read the list.        if model.held.isEmpty { await model.refresh() }        switch DatabaseLinkArrival.decide(link: link, held: model.held, shown: scene.shownId) {        case .open(let database, let route):            await scene.show(database, in: model)            return route        case .notHeld:            toasts.show(ShellLines.line("databases", "that_link_opens_a"), tone: .error)            return nil        case .ask(let route, let among):            // No database named and several held: each is asked whether it            // holds the record, and the only one that does is shown. With            // none or several — or a route that names no record — it opens            // in the database this window shows.            let holding = await holders(of: route, among: among)            if case .open(let database, _) = DatabaseLinkArrival.settle(route: route, among: among, holding: holding) {                await scene.show(database, in: model)            }            return route        case .here(let route):            return route        }    }    /// The held databases among `among` whose copy holds `route`'s record:    /// one `read.record` each, the personal database through the core as it    /// is and every other named. A refusal is "not here".    private func holders(of route: Route, among: [String]) async -> [String] {        guard let core, let record = DatabaseLinkArrival.record(of: route) else { return [] }        var holding: [String] = []        for database in among {            let personal = model.held.first { $0.databaseId == database }?.isPersonal ?? false            let caller: any CottonCalling = personal ? core : DatabaseScopedCore(underlying: core, database: database)            let answer = try? await caller.callJSON(                CottonMethod.readRecord, RecordReadRequest(kind: record.kind.rawValue, id: record.id))            if let answer, answer != .null { holding.append(database) }        }        return holding    }}