// The reader's fan: the deck's own tier component (`FanTiers`, drawn by// `FanTierArc` on the fan's arc with the hub's close under it), hosted over// the page's foot inside the reader. Its tiers are the core's// (`shell.reader`'s `fan`): their cards, their words, which card rests// raised and what a tap writes; this file deals them onto the arc and// draws a face as a page and a pencil by its line.//// The hand in the bar opens ONE tier, flat, as the core deals it// (`fan.hand`): Paper, Sepia and Night, each a small page in its own ground// with its name in its ink over three ruled lines (`FanSwatch`), then Type// size, Line height, Margins, Typeface, Layout, Follow the phone's light and// dark, each saying what it is set to; every one a tile of the one size on// the one arc. A face's tap wears it, at once; a knob's opens its CHOICES,// and a choice is written and the hand's tier returns, resting on the knob.//// A selection in the book opens the SELECTION tier: the three pencils and// Note. A tap on a drawn highlight opens its own: the pencils (to// recolour), Note, and Remove, last and red. The hub closes whatever// stands; so does a tap on the page.#if os(iOS)importCottonPaperimportCottonValuesimportSwiftUI/// The open tier over the page's foot, its close at the hub.structReaderFan: View {let reading: BookReading @Environment(\.theme) privatevar themevar body: someView {let diameter = Double(theme.scale[.fanRecordSize])let geometry = FanGeometry(scale: theme.scale).withHub(diameter: diameter) ZStack(alignment: .bottom) {ifletopen = reading.tiers.open, reading.tiers.closes {FanTierArc(tier: open, geometry: geometry, tiers: reading.tiers) .id(open.id) .frame(height: geometry.height) .transition(.scale(scale: 0.28, anchor: .bottom).combined(with: .opacity))GeometryReader { proxy inFanClose(size: theme.scale[.fanRecordSize], acts: true) { reading.back() } .position(geometry.hub(width: proxy.size.width, diameter: diameter)) } .frame(height: geometry.height) } } .animation(.easeOut(duration: Double(theme.scale[.fanTurnMsMin]) / 1000), value: reading.tiers.open?.id) .accessibilityElement(children: .contain) .accessibilityIdentifier("reader-fan") }}extensionBookReading {// MARK: Opening tiers/// The hand: its one tier, resting on the face chosen.func openHand() { closeFan() tiers.push(Self.handTier(fan)) { [weakself] card inself?.tappedHand(card) } }/// The hub: a knob's choices give way to the hand's tier; anything else closes.func back() {if tiers.depth > 2, tiers.open?.id.hasPrefix(Self.choicesPrefix) == true { tiers.pop() } else { closeFan() } }/// The pencils for the selection just made.func openSelection() {while tiers.pop() != nil {} tiers.push(Self.selectionTier(fan)) { [weakself] card inself?.tappedSelection(card) } }/// A drawn highlight tapped: its own tier.func openHighlight(_ id: String) {guardlet highlight = highlights.first(where: { $0.id == id }) else { return } closeFan() tiers.push(Self.highlightTier(fan, highlight)) { [weakself] card inself?.tappedHighlight(card, id: id) } }/// The hand's tier standing says the row as it now stands, resting where it rested.func refreshTier() {guardletopen = tiers.open, open.id == Self.handID else { return }var tier = Self.handTier(fan)iflet raised = open.raised, tier.cards.contains(where: { $0.id == raised }) { tier.raised = raised } tiers.replaceOpen(tier) { [weakself] card inself?.tappedHand(card) } }// MARK: Taps/// A face is worn at once; a knob opens its choices.privatefunc tappedHand(_ card: FanTier.Card) {guard card.id.hasPrefix(Self.knobPrefix) else { wear(String(card.id.dropFirst(Self.facePrefix.count))) tiers.raise(card.id)return }let knob = String(card.id.dropFirst(Self.knobPrefix.count))guardlet choices = Self.choicesTier(fan, knob: knob) else { return } tiers.push(choices) { [weakself] card inself?.tappedChoice(card, of: knob) } }privatefunc tappedChoice(_ card: FanTier.Card, of knob: String) {guardlet value = fan.hand.cards.first(where: { $0.id == Self.knobPrefix + knob })? .choices?.cards.first(where: { $0.id == card.id })?.value else { return }Task {await set(knob, to: value) tiers.pop() tiers.raise(Self.knobPrefix + knob) refreshTier() } }privatefunc tappedSelection(_ card: FanTier.Card) {if card.id == Self.noteID { mark(with: nil, noting: true) } elseiflet pencil = Self.pencil(of: card) { mark(with: pencil) } }privatefunc tappedHighlight(_ card: FanTier.Card, id: String) {switch card.id {caseSelf.noteID: startNoting(id)caseSelf.removeID: remove(id)default:guardlet pencil = Self.pencil(of: card) else { return } recolour(id, to: pencil) tiers.raise(card.id) } }// MARK: The tiers, as the core dealt themstaticlet handID = "reader-hand"staticlet facePrefix = "face-"staticlet knobPrefix = "knob-"staticlet choicesPrefix = "reader-choices-"staticlet pencilPrefix = "pencil-"staticlet noteID = "note"staticlet removeID = "remove"/// The hand's one tier: a face as a page in its own ground, a knob by/// its name over what it is set to, in the core's order.staticfunc handTier(_ fan: ReaderFanAnswer) -> FanTier {let cards = fan.hand.cards.map { card inguard card.id.hasPrefix(knobPrefix) else {let face = ReaderFace(rawValue: String(card.id.dropFirst(facePrefix.count))) ?? .paperreturnFanTier.Card(id: card.id, label: card.label, swatch: FanSwatch(kind: .page, ground: face.ground, ink: face.ink, line: face.hairline)) }returnFanTier.Card(id: card.id, label: card.label, item: EditingItem(id: card.id, word: card.label, detail: card.words, does: .choice(column: String(card.id.dropFirst(knobPrefix.count)), value: ""))) }returnFanTier(id: handID, cards: cards, raised: fan.hand.raised) }staticfunc choicesTier(_ fan: ReaderFanAnswer, knob: String) -> FanTier? {guardlet choices = fan.hand.cards.first(where: { $0.id == knobPrefix + knob })?.choices else { returnnil }let cards = choices.cards.map { card inFanTier.Card(id: card.id, label: card.label, item: EditingItem(id: card.id, word: card.label, does: .choice(column: knob, value: ""))) }returnFanTier(id: choicesPrefix + knob, cards: cards, raised: choices.raised) }staticfunc selectionTier(_ fan: ReaderFanAnswer) -> FanTier {FanTier(id: "reader-selection", cards: marks(fan.selection), raised: fan.selection.raised) }staticfunc highlightTier(_ fan: ReaderFanAnswer, _ highlight: BookHighlight) -> FanTier {FanTier(id: "reader-highlight", cards: marks(fan.highlight), raised: pencilPrefix + highlight.pencil.rawValue) }/// A tier of pencils and acts: a pencil drawn by its line on Paper.privatestaticfunc marks(_ tier: ReaderTierAnswer) -> [FanTier.Card] {let paper = ReaderFace.paperreturn tier.cards.map { card inlet pencil = card.id.hasPrefix(pencilPrefix) ? HighlightPencil(rawValue: String(card.id.dropFirst(pencilPrefix.count))) : nilreturnFanTier.Card(id: card.id, label: card.label, red: card.red ?? false, swatch: pencil.map { FanSwatch(kind: .pencil, ground: paper.ground, ink: paper.ink, line: $0.colour) }) } }privatestaticfunc pencil(of card: FanTier.Card) -> HighlightPencil? {guard card.id.hasPrefix(pencilPrefix) else { returnnil }returnHighlightPencil(rawValue: String(card.id.dropFirst(pencilPrefix.count))) }}#endif