// What the media faces share — an episode's above all (KINDS, 66a451ec1):// the rating as a row of hand-drawn stars, and the reader's place as a slim// rule with "212 / 340" typed beside it; and the ink and paper a cover kept// without a picture is printed on (`TypeSleeve`). The places they read are// the core's (`DeckCard`)://// used, limit where the watcher is: episodes watched of episodes;// `used` alone is of a hundred// date when it was watched; while undated the core says// "Add date", which reads as not watched yet// count the rating, out of five//// The faces compose no words: a number is said in the deck's own line.importCottonPaperimportCottonValuesimportSwiftUIenumMediaPartsKnobs {/// The most stars a rating draws.staticlet starsOf = 5staticlet star: CGFloat = 17staticlet starGap: CGFloat = 4staticlet starStroke: CGFloat = 1.3staticlet ruleHeight: CGFloat = 3staticlet ruleTrack: CGFloat = 0.8staticlet placeSize: CGFloat = 12.5}/// Where a reader or a watcher is in a book, a film or a series.structMediaProgress: Equatable {/// How far, nought to one.let share: Double/// The place said ("212 / 340"), or the share as a percentage when the/// card names no whole.let said: String}extensionDeckCard {/// The rating out of five, in halves, read off the count place (a number,/// or the core's words for it); nil when unrated.var rating: Double? {let value: Double?switch fields["count"] {case .number(let number)?: value = numbercase .text(let words)?:let stars = words.filter { $0 == "\u{2605}" }.countlet digits = words.filter { $0.isNumber || $0 == "." } value = stars > 0 ? Double(stars) : Double(digits)default: value = nil }guardlet value, value > 0else { returnnil }return min(Double(MediaPartsKnobs.starsOf), (value * 2).rounded() / 2) }/// The date place, when it holds a date: the core's "Add date" on an/// undated record says it is not finished yet.publicvar dated: String? {guardlet date = text("date"), date != ShellLines.line("cards", "date.add") else { returnnil }return date }/// Where the reader is: `used` of `limit`, or `used` of a hundred.var progress: MediaProgress? {guardlet share = gaugeShare, let used = number("used") else { returnnil }iflet limit = number("limit"), limit > 0 {returnMediaProgress(share: share, said: ShellLines.line("cards", "deck.position", ["at": Self.whole(used), "of": Self.whole(limit)])) }returnMediaProgress(share: share, said: share.formatted(.percent.precision(.fractionLength(0)))) }/// Finished or watched: a date kept, or the place at its end.var finished: Bool { dated != nil || (progress.map { $0.share >= 1 } ?? false) }privatestaticfunc whole(_ number: Double) -> String {Int(number.rounded()).formatted() }}/// What a cover-led face's foot shows for a card: the day it was finished or/// watched, when one is kept; else where the reader is, if anywhere; and the/// rating either way.structMediaFoot: Equatable {var finished: String?var progress: MediaProgress?var rating: Double?init(_ card: DeckCard) { finished = card.dated progress = finished == nil ? card.progress : nil rating = card.rating }}/// A rating as hand-drawn stars: the given ones inked in red and filled, a/// half one filled to its middle, the rest drawn faintly in pencil.publicstructStarRow: View {let rating: Doublevar size: CGFloat = MediaPartsKnobs.star @Environment(\.theme) privatevar themepublicinit(rating: Double) {self.rating = rating }init(rating: Double, size: CGFloat) {self.rating = ratingself.size = size }publicvar body: someView { HStack(spacing: MediaPartsKnobs.starGap) {ForEach(0..<MediaPartsKnobs.starsOf, id: \.self) { index inlet fill = min(1, max(0, rating - Double(index)))let star = HandStar(seed: "rating-\(index)", theme: theme) ZStack { star.fill(theme.print(.red).opacity(0.85)) .mask(alignment: .leading) {Rectangle().frame(width: size * fill) } star.stroke(theme.print(fill > 0 ? .red : .pencil).opacity(fill > 0 ? 1 : 0.45), style: StrokeStyle(lineWidth: MediaPartsKnobs.starStroke, lineCap: .round, lineJoin: .round)) } .frame(width: size, height: size) } } .blendMode(theme.printBlend) .accessibilityElement(children: .ignore) .accessibilityLabel(ShellLines.line("cards", "deck.position", ["at": rating.formatted(), "of": "\(MediaPartsKnobs.starsOf)"])) .accessibilityIdentifier("face-rating") }}/// The reader's place: a slim rule inked as far as they are, the place typed/// at its end.structReadingRule: View {let progress: MediaProgress @Environment(\.theme) privatevar themevar body: someView { HStack(spacing: 10) {GeometryReader { track in ZStack(alignment: .leading) {Capsule().fill(theme.print(.pencil).opacity(0.25)) .frame(height: MediaPartsKnobs.ruleTrack)Capsule().fill(theme.print(.ink)) .frame(width: track.size.width * progress.share, height: MediaPartsKnobs.ruleHeight) } .frame(maxHeight: .infinity) } .frame(height: MediaPartsKnobs.ruleHeight * 3) .blendMode(theme.printBlend)Text(progress.said) .font(CottonFonts.type(MediaPartsKnobs.placeSize)) .lineLimit(1) .fixedSize() .faceInk(.pencil) } .accessibilityElement(children: .combine) .accessibilityIdentifier("face-progress") }}/// The ink a cover kept without a picture is printed in — a sleeve —/// chosen by its title so the same song is always the same colour: the theme's ornament blue, its green, its red/// or its ink.publicenumCoverInk {publicstaticfunc of(_ title: String?, theme: Theme) -> Color {let inks = [theme.faceStocks.ornament, theme.print(.green), theme.print(.red), theme.print(.ink)]let sum = (title ?? "").unicodeScalars.reduce(0) { $0 &+ Int($1.value) }return inks[abs(sum) % inks.count] }}/// A printed block: the house paper with an ink laid over it, the paper's/// grain showing through — the ground a sleeve is printed on.publicstructInkedPaper: View {let ink: Colorlet seed: String @Environment(\.theme) privatevar themepublicinit(ink: Color, seed: String) {self.ink = inkself.seed = seed }publicvar body: someView { ZStack {PaperSurface(dye: .cream, edge: .cut, seed: seed)Rectangle().fill(ink).blendMode(theme.printBlend) } }}extensionEnvironmentValues {/// The card has been opened on its own ground. @Entry publicvar faceOpened: Bool = false/// The card lies turned over, its back up (a sleeve's track list): the/// side a specimen is reviewed on; the deck lays every card face up. @Entry publicvar faceTurnedOver: Bool = false @Entry publicvar faceRelatedOpening: ((String) -> Void)? = nil/// Plays a song by its record, from a line on the card (a sleeve's back);/// none where the shell plays no songs. @Entry publicvar facePlaysSong: ((String) -> Void)? = nil}