Cotton code
48 lines · 1.9 KB · SwiftRaw
// A feed row's tap: its own database first, then its route.//// Today, search and "Asked of you" draw rows from several databases// (`CottonKit/FeedRows.swift`). A row from a database opens the way a link// does — `DatabaseLinkPlacing` shows that database in this window, then the// route opens — so the page that opens is read from the database the row was// in. A row with no database opens at once, exactly as before.import CottonKitimport SwiftUI/// Opens feed rows in ONE window.@MainActorstruct FeedRowOpening {    /// The window's placing, or nil where the window has no held list or    /// scene (a gallery specimen, a test host): every row then opens as    /// before.    let placing: DatabaseLinkPlacing?    init(placing: DatabaseLinkPlacing?) {        self.placing = placing    }    /// The window's own placing, from what its root put in the environment.    init(model: DatabasesModel?, scene: DatabaseScene?, toasts: ToastCenter?) {        if let model, let scene, let toasts {            placing = DatabaseLinkPlacing(model: model, scene: scene, toasts: toasts)        } else {            placing = nil        }    }    /// Push `route` from a row in `database` — a row is a record you looked    /// into, so back belongs on the feed: at once with no database, else once    /// the window shows that database, and not at all when this device does    /// not hold it. Answers the placing's task, or nil when it pushed at once.    @discardableResult    func open(_ route: Route, in database: FeedRowDatabase?, on navigator: Navigator) -> Task<Void, Never>? {        guard let database, let placing else {            navigator.push(route)            return nil        }        let link = FeedRowDatabase.link(database, route: route)        return Task { @MainActor in            if let placed = await placing.place(link) { navigator.push(placed) }        }    }}