Cotton code
269 lines · 11.6 KB · SwiftRaw
// The SUBSCRIPTION face (references `subscriptions.png`, the cancelled case// and the house references 27/57, 28/58 and 87), on the core's places// (KINDS, 0a0a099c3): a shop's price tag — cream card, its top corners cut// off, a reinforced hole at the top with a loop of twine through it, a// notched rule inside. Who provides it typed small (`subtitle`), the service// typed heavy, its price large and the period after it ("₹649 · Monthly")// over a hairline, then the facts down the left — the next charge by hand on// its rule under its label, the card's other facts, `body` typed — and the// old television at the right with the service's name on its screen. A// paused or cancelled one is stamped so in red over the set (`state`); the// corner's "why now" is ringed there instead. No tool's name is on the card:// the fan says where it is. The face draws its own sheet (the tag's shape).// Every word is the core's (`DeckCard`).import CottonPaperimport CottonValuesimport SwiftUIenum SubscriptionFaceKnobs {    static let inset = EdgeInsets(top: 56, leading: 30, bottom: 26, trailing: 24)    static let cut: CGFloat = 26    static let hole: CGFloat = 16    static let nameSize: CGFloat = 34    /// The most a long name shrinks before it takes its second line.    static let nameShrink: CGFloat = 0.6    static let nameGap: CGFloat = 6    static let priceSize: CGFloat = 24    static let periodSize: CGFloat = 16    static let priceGap: CGFloat = 6    static let ruleGap: CGFloat = 12    static let factsTop: CGFloat = 16    static let factGap: CGFloat = 16    static let valueSize: CGFloat = 16    static let bodySize: CGFloat = 14    static let dateRuleGap: CGFloat = 4    /// The facts' column, as a share of the width inside the margins; the    /// set takes the rest at the right.    static let factsShare: CGFloat = 0.44    static let artShare: CGFloat = 0.56    static let screenSize: CGFloat = 13    static let stampSize: CGFloat = 19    static let stampTurn: Double = -9    static let cornerTurn: Double = -12    /// How far the stamp lies over the set's top, so they read as one.    static let stampOverlap: CGFloat = -14}public struct SubscriptionFace: View {    let card: DeckCard    public init(card: DeckCard) {        self.card = card    }    public var body: some View {        GeometryReader { sheet in            let inside = sheet.size.width - SubscriptionFaceKnobs.inset.leading - SubscriptionFaceKnobs.inset.trailing            ZStack(alignment: .topLeading) {                TagSheet()                VStack(alignment: .leading, spacing: 0) {                    if let provider = card.text("subtitle") {                        FaceKind(words: provider)                            .facePart(.subtitle)                    }                    if let name = card.text("title") {                        HeavyType(words: name, size: SubscriptionFaceKnobs.nameSize)                            .lineLimit(2)                            .minimumScaleFactor(SubscriptionFaceKnobs.nameShrink)                            .padding(.top, SubscriptionFaceKnobs.nameGap)                            .accessibilityIdentifier("deck-title")                            .facePart(.title)                    }                    if card.text("price") != nil || card.text("period") != nil {                        price                            .padding(.top, SubscriptionFaceKnobs.priceGap)                    }                    FaceHairline()                        .padding(.top, SubscriptionFaceKnobs.ruleGap)                    HStack(alignment: .top, spacing: 0) {                        facts                            .frame(width: inside * SubscriptionFaceKnobs.factsShare, alignment: .leading)                        Spacer(minLength: 0)                        VStack(alignment: .trailing, spacing: 0) {                            mark                                .zIndex(1)                            Television(words: card.text("title"))                                .frame(width: inside * SubscriptionFaceKnobs.artShare)                                .padding(.top, (card.text("state") ?? card.corner) == nil ? 0 : SubscriptionFaceKnobs.stampOverlap)                        }                    }                    .padding(.top, SubscriptionFaceKnobs.factsTop)                    Spacer(minLength: 0)                }                .padding(SubscriptionFaceKnobs.inset)            }        }    }    /// The price large and the period after it, joined as the cards join    /// words: "₹649 · Monthly".    private var price: some View {        HStack(alignment: .firstTextBaseline, spacing: 0) {            if let amount = card.text("price") {                HeavyType(words: amount, size: SubscriptionFaceKnobs.priceSize)            }            if let period = card.text("period") {                Text(card.text("price") == nil ? period : ShellLines.line("cards", "join") + period)                    .font(CottonFonts.type(SubscriptionFaceKnobs.periodSize))                    .faceInk(.ink)            }        }        .lineLimit(1)        .minimumScaleFactor(SubscriptionFaceKnobs.nameShrink)        .accessibilityElement(children: .combine)    }    /// The state stamped in red ("CANCELLED"), else the corner ringed.    @ViewBuilder private var mark: some View {        if let state = card.text("state") {            RubberStamp(words: state.uppercased(), size: SubscriptionFaceKnobs.stampSize, turn: SubscriptionFaceKnobs.stampTurn)                .facePart(.state)        } else if let corner = card.corner {            RingedCorner(words: corner, pressing: card.pressing)                .rotationEffect(.degrees(SubscriptionFaceKnobs.cornerTurn))                .facePart(.when)        }    }    /// The next charge by hand on its rule, the card's other facts, and    /// `body` typed.    private var facts: some View {        VStack(alignment: .leading, spacing: SubscriptionFaceKnobs.factGap) {            if let next = card.text("next_charge") {                VStack(alignment: .leading, spacing: SubscriptionFaceKnobs.dateRuleGap) {                    FaceFact(label: card.label("next_charge"), value: next, hand: true, size: SubscriptionFaceKnobs.valueSize)                    FaceHairline()                }            }            ForEach(Array(StudioReading.facts(card).enumerated()), id: \.offset) { _, fact in                FaceFact(label: fact.label, value: fact.said, size: SubscriptionFaceKnobs.valueSize)            }            if let body = card.text("body") {                FaceFact(value: body, size: SubscriptionFaceKnobs.bodySize, lines: 3)                    .facePart(.body)            }        }    }}/// The tag's paper: cream, top corners cut, a notched rule inside, a/// reinforced hole and its twine.private struct TagSheet: View {    @Environment(\.theme) private var theme    var body: some View {        GeometryReader { box in            ZStack(alignment: .top) {                PaperSurface(dye: .cream, edge: .cut, seed: "subscription-tag")                    .clipShape(PriceTag(cut: SubscriptionFaceKnobs.cut))                    .shadow(color: theme.colors.shadow.color.opacity(0.22), radius: 3, x: 0, y: 2)                PriceTag(cut: SubscriptionFaceKnobs.cut - 4)                    .inset(by: 10)                    .stroke(theme.print(.pencil).opacity(0.7), lineWidth: 0.9)                    .blendMode(theme.printBlend)                ZStack {                    Circle().stroke(theme.print(.pencil).opacity(0.55), lineWidth: 4)                    Circle().fill(theme.colors.shadow.color.opacity(0.35))                }                .frame(width: SubscriptionFaceKnobs.hole, height: SubscriptionFaceKnobs.hole)                .padding(.top, 18)                Twine()                    .stroke(theme.print(.pencil), style: StrokeStyle(lineWidth: 2, lineCap: .round))                    .frame(width: 30, height: 46)                    .offset(y: -20)                    .blendMode(theme.printBlend)            }            .frame(width: box.size.width, height: box.size.height)        }        .accessibilityHidden(true)    }}/// A price tag's outline: a card with its top corners cut off.private struct PriceTag: InsettableShape {    let cut: CGFloat    var inset: CGFloat = 0    func path(in rect: CGRect) -> Path {        let r = rect.insetBy(dx: inset, dy: inset)        var path = Path()        path.move(to: CGPoint(x: r.minX + cut, y: r.minY))        path.addLine(to: CGPoint(x: r.maxX - cut, y: r.minY))        path.addLine(to: CGPoint(x: r.maxX, y: r.minY + cut))        path.addLine(to: CGPoint(x: r.maxX, y: r.maxY))        path.addLine(to: CGPoint(x: r.minX, y: r.maxY))        path.addLine(to: CGPoint(x: r.minX, y: r.minY + cut))        path.closeSubpath()        return path    }    func inset(by amount: CGFloat) -> PriceTag {        PriceTag(cut: cut, inset: inset + amount)    }}/// The twine through the hole, its two ends rising off the card.private struct Twine: Shape {    func path(in rect: CGRect) -> Path {        var path = Path()        path.move(to: CGPoint(x: rect.midX - 3, y: rect.maxY - 6))        path.addCurve(to: CGPoint(x: rect.minX + 6, y: rect.minY), control1: CGPoint(x: rect.minX, y: rect.maxY - 14), control2: CGPoint(x: rect.midX - 8, y: rect.midY))        path.move(to: CGPoint(x: rect.midX + 3, y: rect.maxY - 6))        path.addCurve(to: CGPoint(x: rect.maxX - 4, y: rect.minY), control1: CGPoint(x: rect.maxX, y: rect.maxY - 16), control2: CGPoint(x: rect.midX + 6, y: rect.midY))        return path    }}/// The old television with the service's name glowing on its screen.private struct Television: View {    let words: String?    @Environment(\.theme) private var theme    var body: some View {        FaceArt(name: "face-art-tv")            .overlay {                GeometryReader { box in                    if let words {                        Text(words)                            .font(CottonFonts.type(SubscriptionFaceKnobs.screenSize))                            .lineLimit(2)                            .minimumScaleFactor(0.6)                            .multilineTextAlignment(.center)                            .foregroundStyle(theme.colors.paper.color.opacity(0.85))                            .frame(width: box.size.width * 0.5, height: box.size.height * 0.45)                            .position(x: box.size.width * 0.38, y: box.size.height * 0.4)                    }                }            }    }}/// The subscription face, by the kind's name, and the cards it is reviewed/// in. The deck draws it straight from the registry, in every app; a/// business's membership is the face its kit names (`sharmila.membership`).public enum SubscriptionFaceCatalogue {    public static let stock: FaceStock = .own    public static let ground: FaceGround = .paper    public static let face = Face(id: "subscription", specimens: [        Specimen("streaming", streaming), Specimen("cancelled", cancelled),    ]) { card, _, _ in        SubscriptionFace(card: card)    }    public static let streaming = DeckCard(face: .subscription, fields: [        "subtitle": .text("Example Media"),        "title": .text("Streamflix"),        "price": .text("\u{20B9}649"),        "period": .text("Monthly"),        "next_charge": .text("Mon 2 Nov"),        "body": .text("Example Bank credit card"),    ])    public static let cancelled = DeckCard(face: .subscription, fields: [        "title": .text("Example Plus"),        "price": .text("\u{20B9}299"),        "period": .text("Monthly"),        "state": .text("Cancelled"),    ])}