Cotton code
166 lines · 8.3 KB · SwiftRaw
// Which face draws a card, by the face id its definition names. A tool's// list is a deck only for a face this build draws (`DeckSwitch.draws`); a// record always opens on its deck (`RecordOpening`), and a card naming a face// this build has none for is drawn by the plain face (`PlainFace`) — the old// record page is gone. Every face is Loom's (`CottonFaces`), drawn from the// registry by its id from its card alone: the deck fills the card with what// its record and table say beyond the core's words (`DeckCardFill`) and// installs what the phone draws for a face (its pictures, a document's first// page, the player). Nothing here asks the flavour: a business's faces are// the ones its kit names (`nancy.dish`, `sharmila.trainer`), never chosen by// the app it runs in — save a studio's class, which the deck picks itself for// an event card with a picture (`faceID`). Every card the deck draws gets the// core's page cutting (through `cottonCore`) and the deck's editing of its// parts (`DeckFaceParts`). A face lays its own paper, in the stock its file// names (CottonPaper's `onCardPaper`); the deck draws every face as it is,// a subscription's ticket and a membership's pass on their own sheets, with// nothing behind it.import CottonFacesimport CottonPaperimport CottonValuesimport SwiftUIenum DeckFace {    /// Does this build draw `face` as its own? It does when Loom's registry    /// has the id. A face newer than this build, or a pending one with no    /// face yet, is drawn plain on the deck, and keeps its own drawing where    /// one stands (Home's `TodayFace`).    static func draws(_ face: DefinitionCardKind) -> Bool {        FaceRegistry.ids.contains(face.rawValue)    }    /// Does this build draw `card`'s face as its own (`draws(_:)`)?    static func draws(_ card: DeckCard) -> Bool {        DefinitionCardKind(rawValue: card.face).map(draws) ?? false    }    /// Does the face `definition`'s card wears tag no part (`Face.tagsNoPart`,    /// a note's torn page)? Then nothing on the card writes, and the fan    /// offers its words (`EditingFan.items(writing:record:)`).    @MainActor static func tagsNoPart(_ definition: TableDefinition) -> Bool {        definition.card.flatMap { FaceRegistry.face($0.face.rawValue)?.tagsNoPart } ?? false    }    /// The face Loom registers for `card`'s id.    @MainActor static func registered(_ card: DeckCard) -> Face? {        FaceRegistry.face(faceID(card))    }    /// The id of the face `card` wears: its own, except an event card with a    /// picture, which is a studio's class (`StudioReading.isClass`).    @MainActor static func faceID(_ card: DeckCard) -> String {        card.face == DefinitionCardKind.event.rawValue && StudioReading.isClass(card) ? ClassFaceCatalogue.face.id : card.face    }    /// `open` is what a tap on a photo's print or a film's still does: the    /// deck opens the record in the one viewer (`DeckViewer`); nil where    /// the card stands outside a deck that opens it.    @MainActor    static func view(for card: DeckCard, record: TableRecord, open: (() -> Void)? = nil, toggle: @escaping () -> Void) -> some View {        DeckFaceDrawn(card: card, record: record, open: open, toggle: toggle)            .environment(\.facePictureDrawer, registered(card)?.steps == .turns ? rolodexPictures : pictures)            .environment(\.faceFilePage, DeckFirstPage.drawer)            .environment(\.faceTrackPlayer, DeckPlaying.player)    }    /// A face's picture as the phone draws it (`PagePicture`): from the    /// largest copy on this phone, or the business's photos. Installed    /// wherever the core is (`cottonCore`), and on every card the deck draws.    private static let rolodexPictures: FacePictureDrawer = { key, edge in        AnyView(PagePicture(key: key, edge: edge, blankWhileLoading: true))    }    static let pictures: FacePictureDrawer = { key, edge in AnyView(PagePicture(key: key, edge: edge)) }}/// A card drawn by its face from the registry, filled with its readings/// (`DeckFilledCard`), with its picture and its taps; an id this build has/// no face for is drawn plainly.private struct DeckFaceDrawn: View {    let card: DeckCard    let record: TableRecord    let open: (() -> Void)?    let toggle: () -> Void    @Environment(\.openURL) private var openURL    @Environment(\.deckEditor) private var editor    @Environment(\.cottonCore) private var core    @Environment(ToastCenter.self) private var toasts: ToastCenter?    @Environment(\.placesCall) private var placesCall    var body: some View {        DeckFilledCard(card: card, record: record) { filled in drawn(filled) }    }    private func drawn(_ card: DeckCard) -> some View {        let picture = card.text("photo").map { FacePicture(key: $0, edge: DeckFaceKnobs.photoEdge) }        return Group {            if let face = DeckFace.registered(card) {                face.view(card, picture: picture, taps: FaceTaps(toggle: toggle))            } else {                // The plain face, for every id with no face of its own yet.                PlainFace(card: card, picture: picture.map { AnyView($0) })            }        }        .environment(\.facePartTaps, parts(card))        .environment(\.facePlaysSong, playsSong())        // The part whose editor stands open, which the face rings.        .environment(\.selectedFacePart, editor?.selected(on: record.id))    }    /// A song played from a line on the card, the album from it, as its Play act plays it.    private func playsSong() -> ((String) -> Void)? {        #if os(iOS)        guard let core else { return nil }        let toasts = toasts        return { id in MusicPlayback.play(song: id, core: core) { toasts?.show($0, tone: .error) } }        #else        return nil        #endif    }    /// What a tap on each of the face's parts does past its editors: the    /// picture opens the viewer, the discs the ways its person is reached. A    /// document's card takes no open of the deck's.    private func parts(_ card: DeckCard) -> FacePartTaps {        let call = ContactHandoff.call(phones: card.reach.phones)        let message = ContactHandoff.message(phones: card.reach.phones, channel: card.reach.channel)        let email = card.reach.emails.first.flatMap { ContactHandoff.link(.mail, ContactTarget(value: $0, kind: .email)) }        let opens = card.face != DefinitionCardKind.document.rawValue        let openURL = openURL        // Listening's player moves through the queue (`MusicPlayback`); a        // song's single plays it from here, as its Play act does.        var next: (() -> Void)?        var previous: (() -> Void)?        var play: (() -> Void)?        #if os(iOS)        if ListeningCard.song(of: card) != nil {            next = { MusicPlayback.shared.next() }            previous = { MusicPlayback.shared.previous() }        }        // The song's tray plays its song the same way; on Listening the tray        // is the player, whose record plays and pauses on its own.        let song = [DefinitionCardKind.songSingle.rawValue, DefinitionCardKind.songTray.rawValue].contains(card.face)        if song, ListeningCard.song(of: card) == nil, let core {            let id = record.id            let toasts = toasts            play = { MusicPlayback.play(song: id, core: core) { toasts?.show($0, tone: .error) } }        }        #endif        // Cotton Calling places the call itself, to the card's person.        if let placesCall {            let person = DeckPlacesCall.person(of: record)            let refused: @MainActor (String) -> Void = { [toasts] words in toasts?.show(words, tone: .error) }            return DeckFaceParts.taps(                open: opens ? open : nil,                call: { placesCall(person, false, refused) },                videoCall: { placesCall(person, true, refused) },                next: next, previous: previous, play: play)        }        return DeckFaceParts.taps(            open: opens ? open : nil,            call: call.map { call in { ContactHandoff.open(call, with: openURL, failed: {}) } },            message: message.map { message in { ContactHandoff.open(message, with: openURL, failed: {}) } },            email: email.map { email in { ContactHandoff.open(email, with: openURL, failed: {}) } },            next: next, previous: previous, play: play)    }}