Cotton code
121 lines · 4.7 KB · SwiftRaw
import CottonKitimport SwiftUI/// What Me opens, decided from `read.you`. Pure apart from its one call/// (`YouRecordTests`).////// The personal object names the owner — the person whose phone is the/// sign-in number — and this phone's copy takes the link from it/// (`phone/you.rs`); nothing on the phone chooses who you are. `read.you`/// answers `owner: "none_yet"` while the copy does not hold that person yet:/// Me waits for the next pass rather than guess. A refusal is said as it came.enum MeOpening {    case person(YouRead)    case notYet    case refused(String)    static func answered(_ read: YouRead) -> MeOpening {        if let id = read.personId, !id.isEmpty { return .person(read) }        return read.noOwner ? .notYet : .refused(MeWords.unlinked)    }    static func refused(by error: Error) -> MeOpening {        .refused((error as? CottonCallError)?.message ?? MeWords.unlinked)    }    /// Ask `read.you`.    static func read(_ core: any CottonCalling) async -> MeOpening {        do {            return answered(try await core.call(CottonMethod.readYou, EmptyRequest(), as: YouRead.self))        } catch {            return refused(by: error)        }    }}/// `/me` — the Me card's page: the owner's own person, resolved by/// `owner_person_id` through `read.you` (which reads this device's own file,/// so it opens with no server reachable) and drawn as the People row it is./// Until the copy holds the person the object names, Me says so in one line.struct MeRoute: View {    @Environment(\.cottonCore) private var core    @State private var opening: MeOpening?    var body: some View {        Group {            switch opening {            case .person(let you):                PersonPageRoute(id: you.personId ?? "", database: you.databaseId)            case .refused(let refusal):                PaperScreen(title: MeWords.title) {                    Text(refusal)                        .paperHand(17)                        .accessibilityIdentifier("me-refusal")                }            case .notYet:                PaperScreen(title: MeWords.title) {                    Text(MeWords.notYet)                        .paperHand(17)                        .accessibilityIdentifier("me-not-yet")                }            case nil:                PaperScreen { EmptyView() }            }        }        .task(id: core != nil) {            guard case .none = opening, let core else { return }            opening = await MeOpening.read(core)        }    }}enum MeWords {    static var title: String { ShellLines.line("record", "me.title") }    static var unlinked: String { ShellLines.line("record", "me.unlinked") }    static var notYet: String { ShellLines.line("record", "me.not_yet") }}/// A person is their card on the People deck (door row 14), from Home and/// from People alike — there is no person page. A person opened in a named/// database (the Me card's, a device's owner) reads that database whatever/// the window shows; any other opens in the window's.struct PersonPageRoute: View {    let id: String    let database: String?    @Environment(\.cottonCore) private var core    @Environment(\.tableDefinitions) private var definitions    @State private var stores = PersonPageStores()    var body: some View {        if let people = definitions.playing(.person) {            if let database, let core {                let own = stores.store(core: core, database: database)                // Read in their own database.                DeckSurface(definition: people, filter: [:], view: nil, opensAt: id)                    .environment(\.records, own.records)                    .environment(\.cottonCore, own.core)                    .environment(\.listsOnPath, false)                    .environment(\.recordOpener, nil)            } else {                RecordOpening(definition: people, opens: .record(id, nil))            }        } else {            PaperScreen { EmptyView() }        }    }}/// The store a person in a named database is read through, made once per/// page: a new one on every redraw would forget its answers.@MainActorfinal class PersonPageStores {    private var made: (database: String, core: DatabaseScopedCore, records: CoreRecords)?    func store(core: any CottonCalling, database: String, bundle: Bundle? = nil) -> (core: DatabaseScopedCore, records: CoreRecords) {        if let made, made.database == database { return (made.core, made.records) }        let scoped = DatabaseScopedCore(underlying: core, database: database)        let records = CoreRecords(core: scoped, bundle: bundle)        made = (database, scoped, records)        return (scoped, records)    }}