Cotton code
297 lines · 10.3 KB · SwiftRaw
// The open book as the core answers it (`shell.reader`), and the answers to// the reader's reports (`reader.moved`, `reader.highlight`,// `reader.settings`). Every decision is the core's (`phone::reader`): where// the book opens, when a place is written, the first open's Reading, the// face worn, each knob's words, the tiers and the highlights. The reader// decodes these and draws them; nothing here decides.//// Nothing here needs Readium, so it is tested on the Mac with the package.import CottonKitimport CottonMailboximport CottonValuesimport Foundation/// The reader's methods (`CottonMethod`'s, named where the reader calls them).enum ReaderCalls {    static let reader = CottonMethod.shellReader    static let moved = CottonMethod.readerMoved    static let highlight = CottonMethod.readerHighlight    static let settings = CottonMethod.readerSettings    static let seek = CottonMethod.readerSeek    static let seekSay = CottonMethod.readerSeekSay    static let seekGo = CottonMethod.readerSeekGo    static let seekBack = CottonMethod.readerSeekBack}/// What the reader opens: a book's record, and a place to open at instead of/// its own (`reader.open`'s `at`, a highlight's place); or a kept page's/// record (`table` `page`, Cotton Pages), which opens in the pages' reader.public struct ReaderOpening: Identifiable, Hashable, Sendable {    public let id: String    public let at: String?    /// The record's table, as `reader.open` names it: `book` or `page`.    public let table: String    public init(id: String, at: String? = nil, table: String = "book") {        self.id = id        self.at = at        self.table = table    }}/// Where the page stands, as the core says it.struct ReaderPlace: Equatable, Sendable, Decodable {    /// Readium's Locator, serialised.    let locator: String    let chapter: String?    let progress: Int?    /// The page number at the foot: a PDF's printed label, else Readium's position.    let pageLabel: String?    enum CodingKeys: String, CodingKey {        case locator, chapter, progress        case pageLabel = "page_label"    }}/// One tier of the reader's fan, as the core deals it.struct ReaderTierAnswer: Equatable, Sendable, Decodable {    struct Card: Equatable, Sendable, Decodable {        let id: String        let label: String        var red: Bool?        /// A knob's: what it is set to, and its choices.        var words: String?        var choices: ReaderTierAnswer?        /// A knob's choice: the value it sets (`reader.settings`).        var value: RecordValue?    }    let cards: [Card]    var raised: String?}/// The reader's fan: the one tier the hand opens (the faces, then the/// knobs), the selection's pencils and a drawn highlight's acts.struct ReaderFanAnswer: Equatable, Sendable, Decodable {    let hand: ReaderTierAnswer    let selection: ReaderTierAnswer    let highlight: ReaderTierAnswer    let noteHint: String    enum CodingKeys: String, CodingKey {        case hand, selection, highlight        case noteHint = "note_hint"    }}/// `shell.reader`'s answer: the book whole, or the line said in its place.struct ReaderAnswer: Sendable, Decodable {    struct Refusals: Sendable, Decodable {        let locked: String        let unopened: String    }    let book: String    /// Said in place of the book (locked, or no file the reader opens); nothing else is answered then.    let refusal: String?    let title: String?    let file: String?    /// `epub` or `pdf`.    let format: String?    let at: ReaderPlace?    let progressWords: String?    let settings: ReaderSettings?    let fan: ReaderFanAnswer?    let highlights: [BookHighlight]?    let refusals: Refusals?    enum CodingKeys: String, CodingKey {        case book, refusal, title, file, format, at, settings, fan, highlights, refusals        case progressWords = "progress_words"    }    var bookFormat: BookFormat? { format.flatMap(BookFormat.init(rawValue:)) }}/// `reader.moved`'s answer: where the page stands and when to report again.struct ReaderMovedAnswer: Sendable, Decodable {    let at: ReaderPlace?    let progressWords: String?    let wrote: Bool    /// Report again (with no place) this long from now: a place waits to be written.    let againMs: Int?    /// The seek line's Back as it now stands.    var back: ReaderBackAnswer?    enum CodingKeys: String, CodingKey {        case at, wrote, back        case progressWords = "progress_words"        case againMs = "again_ms"    }}/// The seek line's one Back, while it stands (`reader.seek`): where it/// returns to on the line, the slip's words, and how long it still stands.struct ReaderBackAnswer: Equatable, Sendable, Decodable {    let at: Double    let words: String    let msLeft: Int    enum CodingKeys: String, CodingKey {        case at, words        case msLeft = "ms_left"    }}/// `reader.seek`'s answer: the line whole. Every place is a share of the/// book, 0 its start and 1 its end.struct ReaderSeekAnswer: Equatable, Sendable, Decodable {    /// One chapter's start; `here` marks the one the page stands in.    struct Tick: Equatable, Sendable, Decodable {        let at: Double        let title: String        let here: Bool    }    struct Words: Equatable, Sendable, Decodable {        /// The slip's words for where the page stands: the line's value.        let here: String        /// The line's name, and the contents' heading.        let line: String        let contents: String    }    /// Where one adjustable step lands, each way; nil at the book's end.    struct Steps: Equatable, Sendable, Decodable {        let previous: Double?        let next: Double?    }    let here: Double    let ticks: [Tick]    let pages: Int?    let back: ReaderBackAnswer?    let words: Words    let steps: Steps}/// `reader.seek.say`'s answer: the slip's words for a dragged place.struct ReaderSeekSaid: Sendable, Decodable {    let words: String}/// `reader.seek.go`'s and `reader.seek.back`'s answer: the Locator the/// navigator goes to (nil where no Back stood) and the Back left.struct ReaderSeekWent: Sendable, Decodable {    let locator: String?    let back: ReaderBackAnswer?}/// What the seek line asks: the book, a place on the line (with the line's/// length in points when it was a tap), or a tick of the contents.struct ReaderSeekAsk: Encodable, Sendable {    var book: String    var at: Double?    var width: Double?    var tick: Int?}/// What the reader measures of the open book for the seek line, from/// Readium's positions: where each resource of the reading order begins as/// a share of the book, and a PDF's page count. What they mean on the line/// is the core's (`reader.moved`'s `contents`).enum ReaderSeekMeasures {    /// `firsts` is each resource's first position's share, in reading    /// order (nil for a resource with no position, which begins where the    /// one before it did). No starts are said unless every resource is    /// accounted for, and they never run backwards.    static func of(firsts: [Double?], resources: Int, positions: Int, pdf: Bool) -> (starts: [Double]?, pages: Int?) {        var starts: [Double]?        if resources > 0, firsts.count == resources {            var last = 0.0            starts = firsts.map { first in                last = max(last, min(1, max(0, first ?? last)))                return last            }        }        return (starts, pdf && positions > 0 ? positions : nil)    }    /// How many chapter starts lie at or before a place: a change of it    /// while the finger drags is a tick crossed.    static func ticksPassed(_ ticks: [Double], at part: Double) -> Int {        ticks.reduce(0) { $0 + ($1 <= part ? 1 : 0) }    }}/// `reader.settings`'s answer: the settings and the fan as they now stand.struct ReaderSettingsAnswer: Sendable, Decodable {    let settings: ReaderSettings    let fan: ReaderFanAnswer}/// What the reader reports: a request's values, as the core reads them.struct ReaderReport: Encodable, Sendable {    var book: String?    var id: String?    var at: String?    var dark: Bool?    var active: Bool?    var closing: Bool?    var pageLabel: String??    var contents: Contents?    var pencil: String?    var words: String?    var note: String?    var remove: Bool?    var face: String?    var knob: String?    var value: RecordValue?    /// The book's contents in reading order, children after their parent.    struct Contents: Encodable, Sendable {        struct Entry: Encodable, Sendable {            let title: String            let href: String        }        let toc: [Entry]        let readingOrder: [String]        /// Where each resource of the reading order begins, as a share of the book.        var starts: [Double]?        /// The book's page count, where it has page numbers (a PDF).        var pages: Int?        enum CodingKeys: String, CodingKey {            case toc, starts, pages            case readingOrder = "reading_order"        }    }    enum CodingKeys: String, CodingKey {        case book, id, at, dark, active, closing, contents, pencil, words, note, remove, face, knob, value        case pageLabel = "page_label"    }    func encode(to encoder: any Encoder) throws {        var container = encoder.container(keyedBy: CodingKeys.self)        try container.encodeIfPresent(book, forKey: .book)        try container.encodeIfPresent(id, forKey: .id)        try container.encodeIfPresent(at, forKey: .at)        try container.encodeIfPresent(dark, forKey: .dark)        try container.encodeIfPresent(active, forKey: .active)        try container.encodeIfPresent(closing, forKey: .closing)        // A page with no printed label says so (null); a report that names no page leaves it out.        if let pageLabel { try container.encode(pageLabel, forKey: .pageLabel) }        try container.encodeIfPresent(contents, forKey: .contents)        try container.encodeIfPresent(pencil, forKey: .pencil)        try container.encodeIfPresent(words, forKey: .words)        try container.encodeIfPresent(note, forKey: .note)        try container.encodeIfPresent(remove, forKey: .remove)        try container.encodeIfPresent(face, forKey: .face)        try container.encodeIfPresent(knob, forKey: .knob)        try container.encodeIfPresent(value, forKey: .value)    }}