Cotton code
63 lines · 2.9 KB · SwiftRaw
// The open database, in the environment.//// `RootView` is handed the core once and builds the app-lifetime models in// its own `init`; every pushed screen used to be handed it AGAIN, by hand,// through a host, a screen and every sub-view between — 352 `core:` parameters// for one value that never changes after boot. It travels down the tree now,// beside `cottonSync`, and `Hosted` is the one reader that builds a MODEL from// it. The other readers are leaf components with no model of their own that// still ask the database a question — `CapturedImage` asks where a picture's// bytes are, `ChatComposerBar` builds its dictation engine — and each draws// something honest without one. A screen is never a reader: its read is// `ScreenRead`, its writes are `ScreenWriter`.//// Optional, with no default to fall back to, for `cottonSync`'s reason: a// screen rendered outside the app — a preview, a gallery specimen, a test// that mounted a bare view — has no database, and a core that answered// refusals in its place would be a page saying "this could not be read" for// a reason nobody wrote down. `Hosted` draws the honest page instead.//// Wherever the core is set, the faces' services are the phone's: a face's// long words are cut into pages by the database whose words they are// (`facePageCutter`, `CorePageCutter`; with no core they stay one page), and// a face's pictures are drawn from the phone (`facePictureDrawer`,// `PagePicture`, which draws honestly with no core too).import CottonFacesimport CottonKitimport CottonValuesimport SwiftUIprivate struct CottonCoreKey: EnvironmentKey {    static let defaultValue: (any CottonCalling)? = nil}extension EnvironmentValues {    public var cottonCore: (any CottonCalling)? {        get { self[CottonCoreKey.self] }        set {            self[CottonCoreKey.self] = newValue            facePageCutter = newValue.map { CorePageCutter(core: $0) }            facePictureDrawer = DeckFace.pictures        }    }}// The environment boundary owns the engine adapter; column editors receive// only AttachmentStore, never the database. The scaffold sweep guards this./// The engine's local lookup and explicit fetch remain distinct.struct CoreAttachmentStore: AttachmentStore {    let core: any CottonCalling    func localURL(for ref: String) async -> URL? {        let answer = try? await core.callJSON(CottonMethod.readMediaPath,            ReadRequest(args: .object(["key": .string(ref)])))        return answer?.text("path").map { URL(fileURLWithPath: $0) }    }    func ensure(for ref: String) async throws -> URL? {        let answer = try await core.callJSON(CottonMethod.mediaEnsure, ReadRequest(args: .object(["key": .string(ref)])))        return answer.text("path").map { URL(fileURLWithPath: $0) }    }    func store(fileAt url: URL, name: String?, for type: ColumnType) async throws -> AttachmentValue {        throw CocoaError(.featureUnsupported)    }}