Cotton code
87 lines · 3.5 KB · SwiftRaw
// One picture whose bytes are on this device, drawn at the size its row states.//// It was the version reader's own view, because a published version's track was// the only page that drew a stored screenshot. A note's attachment and a code// session's attachment are the other two, and a picture has to look and behave// the SAME on all of them — so this is scaffold, one view rather than three, and// the reason it reports an absence in words lives here rather than in each// caller.import CottonKitimport SwiftUI/// THREE STATES and it says all three: resolving, here, and not on this phone./// It is the one place in this lane that REPORTS a missing picture rather than/// sitting quiet, and the reason is what the picture IS: on a catalogue tile the/// photo decorates a product, and here the picture is the subject of the page.////// **MOUNTING NEVER FETCHES.** The `.task` asks `read.mediaPath` — where the/// bytes are on THIS phone — because a strip is a row of these and a shelf is a/// grid of them, and a view that called `media.ensure` as it scrolled into place/// opened the radio once per picture nobody had asked for. An absence draws/// the sentence and stays put; the one viewer (`MediaViewer`), opened by a/// tap, is what fetches.struct CapturedImage: View {    let key: String?    let title: String    let width: CGFloat    let height: CGFloat    /// The picture AND the key it is about: `.task(id:)` re-runs on a pop-back    /// with the very same key, and this is what stops that re-run blanking a    /// picture that is already decoded and drawn.    @State private var slot = MediaImageSlot()    @Environment(\.displayScale) private var scale    /// The open database, read from the environment rather than threaded down    /// the strip: this is a View, and a View is not handed a database.    @Environment(\.cottonCore) private var database    @Environment(\.tableDefinitions) private var definitions    var body: some View {        Group {            if let image = slot.image {                Image(uiImage: image)                    .resizable()                    .scaledToFill()                    .accessibilityLabel(title)            } else if slot.isAbsent {                absence            } else {                CottonColors.groundMuted            }        }        .frame(width: width, height: height)        .clipped()        .clipShape(RoundedRectangle(cornerRadius: 12, style: .continuous))        .task(id: key) { await load() }        // Bytes moved on this phone: an absent picture asks again, a drawn one        // does nothing.        .onMediaArrival {            guard !slot.isReady else { return }            await load()        }    }    /// Not on this phone: the slip. Nothing here fetches — opening the    /// picture does.    private var absence: some View {        ZStack {            CottonColors.groundMuted            AbsentSlip(kind: .photo, words: ViewerWords.reading(definitions), compact: true)                .padding(AbsentSlipKnobs.gap)        }        .accessibilityIdentifier("captured-image-absent")    }    /// The RENDER ask: where the bytes are on this phone, and never a fetch.    private func load() async {        let number = slot.begin(key)        guard let key, !key.isEmpty, let database else {            slot.finish(.absent, for: key, ask: number)            return        }        let answer = await MediaImages.load(            from: database, key: key, edge: max(width, height) * scale        )        slot.finish(answer, for: key, ask: number)    }}