Cotton code
109 lines · 4.6 KB · SwiftRaw
// What the phone draws for a face beyond its card (CottonFaces' hooks,// installed on every card the deck draws, `DeckFace.view`): a document's// first page off the record's own file (`faceFilePage`), and the player a// face plays under (`faceTrackPlayer`): on a show's radio, its episode in the// app's one podcast player (`DeckRadioPlaying`); on Listening's player, its// song in the one music player (`DeckMusicPlaying`); on an album's tray, the// album in it (`DeckAlbumPlaying`); every other card stands still.import CottonFacesimport CottonKitimport CottonPaperimport CottonValuesimport SwiftUIenum DeckFaceKnobs {    /// The pixels a card's photograph is decoded at, along its longest side.    static let photoEdge: CGFloat = 420}/// A document's first page from its file, drawn as the Documents covers draw/// theirs (`FirstPageThumbnail`): once per file, kept for the session, and a/// blank page until it is drawn. It tells its card the page's shape.////// A card in the deck is the document being looked at, so its bytes are/// fetched when they are not on this phone (`ensure`, as the viewer fetches/// them): a document has no small copy a sync brings, as a photo has, and a/// card that only read the bytes already here stood blank until its file was/// opened (walk 332: every document card on a fresh phone).struct DeckFirstPage: View {    let ref: String    @Binding var aspect: CGFloat    @Environment(\.attachmentStore) private var store    @Environment(\.cottonCore) private var core    @State private var image: UIImage?    /// How a face asks for it (`faceFilePage`).    static let drawer: FaceFilePage = { ref, aspect in AnyView(DeckFirstPage(ref: ref, aspect: aspect)) }    /// The file holding `ref`'s bytes: the one on this phone, else fetched.    static func file(_ ref: String, from source: any AttachmentStore) async -> URL? {        if let here = await source.localURL(for: ref), FileManager.default.fileExists(atPath: here.path) { return here }        return try? await source.ensure(for: ref)    }    var body: some View {        Group {            if let image {                Image(uiImage: image)                    .resizable()                    .scaledToFill()                    .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .top)                    .clipped()                    .blendMode(.multiply)            } else {                BlankPage().padding(.horizontal, 12).padding(.vertical, 16)            }        }        .task(id: ref) {            let drawn: UIImage            if let kept = FirstPageThumbnail.cache.object(forKey: ref as NSString) {                drawn = kept            } else {                guard let source = store ?? core.map({ CoreAttachmentStore(core: $0) }),                    let url = await Self.file(ref, from: source),                    let made = await Task.detached(priority: .utility, operation: { FirstPageThumbnail.draw(url) }).value,                    !Task.isCancelled                else { return }                FirstPageThumbnail.cache.setObject(made, forKey: ref as NSString)                drawn = made            }            image = drawn            if drawn.size.width > 0, drawn.size.height > 0 { aspect = drawn.size.width / drawn.size.height }        }    }}/// A card under the player: a show's radio plays its episode in the app's/// one podcast player (`DeckRadioPlaying`), Listening's player its song in/// the one music player (`DeckMusicPlaying`), a cassette its list or its song/// (`DeckCassettePlaying`); every other card stands still.struct DeckPlaying: View {    let card: DeckCard    let drawn: (FaceTrackPlay) -> AnyView    /// How a face asks for it (`faceTrackPlayer`).    static let player: FaceTrackPlayer = { card, drawn in AnyView(DeckPlaying(card: card, drawn: drawn)) }    var body: some View {        if ListeningCard.song(of: card) != nil {            DeckMusicPlaying(card: card, drawn: drawn)        } else if card.face == DefinitionCardKind.albumTray.rawValue {            DeckAlbumPlaying(card: card, drawn: drawn)        } else if [DefinitionCardKind.listCassette.rawValue, DefinitionCardKind.songCassette.rawValue].contains(card.face) {            DeckCassettePlaying(card: card, drawn: drawn)        } else {            #if os(iOS)            if case .object(let episode)? = card.fields["episode"] {                DeckRadioPlaying(card: card, episode: episode, drawn: drawn)            } else {                still            }            #else            still            #endif        }    }    private var still: AnyView { drawn(FaceTrackPlay(play: .still, toggle: nil, seek: { _ in })) }}