// 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.importCottonFacesimportCottonPaperimportCottonValuesimportSwiftUIenumDeckFace {/// 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`).staticfunc draws(_ face: DefinitionCardKind) -> Bool {FaceRegistry.ids.contains(face.rawValue) }/// Does this build draw `card`'s face as its own (`draws(_:)`)?staticfunc 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 staticfunc tagsNoPart(_ definition: TableDefinition) -> Bool { definition.card.flatMap { FaceRegistry.face($0.face.rawValue)?.tagsNoPart } ?? false }/// The face Loom registers for `card`'s id. @MainActor staticfunc 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 staticfunc 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. @MainActorstaticfunc view(for card: DeckCard, record: TableRecord, open: (() -> Void)? = nil, toggle: @escaping () -> Void) -> someView {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.privatestaticlet rolodexPictures: FacePictureDrawer = { key, edge inAnyView(PagePicture(key: key, edge: edge, blankWhileLoading: true)) }staticlet pictures: FacePictureDrawer = { key, edge inAnyView(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.privatestructDeckFaceDrawn: View {let card: DeckCardlet record: TableRecordletopen: (() -> Void)?let toggle: () -> Void @Environment(\.openURL) privatevar openURL @Environment(\.deckEditor) privatevar editor @Environment(\.cottonCore) privatevar core @Environment(ToastCenter.self) privatevar toasts: ToastCenter? @Environment(\.placesCall) privatevar placesCallvar body: someView {DeckFilledCard(card: card, record: record) { filled in drawn(filled) } }privatefunc drawn(_ card: DeckCard) -> someView {let picture = card.text("photo").map { FacePicture(key: $0, edge: DeckFaceKnobs.photoEdge) }returnGroup {iflet 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.privatefunc playsSong() -> ((String) -> Void)? { #if os(iOS)guardlet core else { returnnil }let toasts = toastsreturn { id inMusicPlayback.play(song: id, core: core) { toasts?.show($0, tone: .error) } } #elsereturnnil #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.privatefunc 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.rawValuelet 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)ifListeningCard.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.idlet 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.iflet placesCall {let person = DeckPlacesCall.person(of: record)let refused: @MainActor (String) -> Void = { [toasts] words in toasts?.show(words, tone: .error) }returnDeckFaceParts.taps(open: opens ? open : nil, call: { placesCall(person, false, refused) }, videoCall: { placesCall(person, true, refused) }, next: next, previous: previous, play: play) }returnDeckFaceParts.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) }}