// 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. import CottonPaper import CottonValues import SwiftUI enum MediaPartsKnobs { /// The most stars a rating draws. static let starsOf = 5 static let star: CGFloat = 17 static let starGap: CGFloat = 4 static let starStroke: CGFloat = 1.3 static let ruleHeight: CGFloat = 3 static let ruleTrack: CGFloat = 0.8 static let placeSize: CGFloat = 12.5 } /// Where a reader or a watcher is in a book, a film or a series. struct MediaProgress: 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 } extension DeckCard { /// 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 = number case .text(let words)?: let stars = words.filter { $0 == "\u{2605}" }.count let digits = words.filter { $0.isNumber || $0 == "." } value = stars > 0 ? Double(stars) : Double(digits) default: value = nil } guard let value, value > 0 else { return nil } 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. public var dated: String? { guard let date = text("date"), date != ShellLines.line("cards", "date.add") else { return nil } return date } /// Where the reader is: `used` of `limit`, or `used` of a hundred. var progress: MediaProgress? { guard let share = gaugeShare, let used = number("used") else { return nil } if let limit = number("limit"), limit > 0 { return MediaProgress(share: share, said: ShellLines.line("cards", "deck.position", ["at": Self.whole(used), "of": Self.whole(limit)])) } return MediaProgress(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) } private static func 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. struct MediaFoot: 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. public struct StarRow: View { let rating: Double var size: CGFloat = MediaPartsKnobs.star @Environment(\.theme) private var theme public init(rating: Double) { self.rating = rating } init(rating: Double, size: CGFloat) { self.rating = rating self.size = size } public var body: some View { HStack(spacing: MediaPartsKnobs.starGap) { ForEach(0.. 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. struct ReadingRule: View { let progress: MediaProgress @Environment(\.theme) private var theme var body: some View { 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. public enum CoverInk { public static func 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. public struct InkedPaper: View { let ink: Color let seed: String @Environment(\.theme) private var theme public init(ink: Color, seed: String) { self.ink = ink self.seed = seed } public var body: some View { ZStack { PaperSurface(dye: .cream, edge: .cut, seed: seed) Rectangle().fill(ink).blendMode(theme.printBlend) } } } extension EnvironmentValues { /// The card has been opened on its own ground. @Entry public var 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 public var faceTurnedOver: Bool = false @Entry public var 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 public var facePlaysSong: ((String) -> Void)? = nil }