// What the faces read off a card's facts: the money, the passwords and the // studio's faces each list a card's facts (`facts`), a label and its words. // And the studio's own readings: a class, a clock's figures, a number's // figure, its seats and their share. Which face a studio's card wears is its // kit's (`sharmila.trainer`, `sharmila.studio`, …); a class's time is the // app's (CottonUI's `StudioFaces`). import CottonPaper import CottonValues /// What the studio faces read off a card. public enum StudioReading { /// A card's facts (`facts`): each one's label and words. public static func facts(_ card: DeckCard) -> [(label: String?, said: String)] { card.entries("facts").compactMap { entry in guard let said = entry["said"]?.text, !said.isEmpty else { return nil } return (entry["label"]?.text, said) } } /// An event card with a picture is a class. public static func isClass(_ card: DeckCard) -> Bool { card.text("photo") != nil } /// A time as the editorial figures and the clock's word after them /// ("11:30 am" → "11:30", "am"; "19:30" → "19:30", nil); nil when the /// words are no clock (an event's kind), which the face sets as a line. public static func clock(_ time: String) -> (figures: String, suffix: String?)? { guard time.first?.isNumber == true else { return nil } let figures = String(time.prefix { $0.isNumber || $0 == ":" || $0 == "." }) let suffix = time.dropFirst(figures.count).trimmingCharacters(in: .whitespaces) return (figures, suffix.isEmpty ? nil : suffix) } /// The core's words for a number split into the figure drawn large and /// the words after it ("148 sessions attended" → "148", "sessions /// attended"); nil when the words do not open on a figure. public static func figure(_ words: String?) -> (figure: String, words: String?)? { guard let words, words.first?.isNumber == true else { return nil } let figure = String(words.prefix { $0.isNumber || $0 == "," || $0 == "." }) let rest = words.dropFirst(figure.count).trimmingCharacters(in: .whitespaces) return (figure, rest.isEmpty ? nil : rest) } /// The places taken and the room (`used` of `limit`), when the card /// names both and they make sense. public static func seats(_ card: DeckCard) -> (taken: Int, of: Int)? { guard let used = card.fields["used"]?.number, let limit = card.fields["limit"]?.number, limit > 0, used >= 0 else { return nil } return (min(Int(used), Int(limit)), Int(limit)) } /// How far a ring or a bar is drawn: taken of the whole, never below /// empty or past full. public static func ringShare(taken: Int, of: Int) -> Double { guard of > 0 else { return 0 } return min(1, max(0, Double(taken) / Double(of))) } /// A share as a whole percent, rounded (2 of 12 → 17). public static func percent(taken: Int, of: Int) -> Int { Int((ringShare(taken: taken, of: of) * 100).rounded()) } }