importCottonKitimportSwiftUI/// 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.enumMeOpening {case person(YouRead)case notYetcase refused(String)staticfunc answered(_ read: YouRead) -> MeOpening {iflet id = read.personId, !id.isEmpty { return .person(read) }return read.noOwner ? .notYet : .refused(MeWords.unlinked) }staticfunc refused(by error: Error) -> MeOpening { .refused((error as? CottonCallError)?.message ?? MeWords.unlinked) }/// Ask `read.you`.staticfunc read(_ core: any CottonCalling) async -> MeOpening {do {return answered(tryawait 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.structMeRoute: View { @Environment(\.cottonCore) privatevar core @State privatevar opening: MeOpening?var body: someView {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") }casenil:PaperScreen { EmptyView() } } } .task(id: core != nil) {guardcase .none = opening, let core else { return } opening = awaitMeOpening.read(core) } }}enumMeWords {staticvar title: String { ShellLines.line("record", "me.title") }staticvar unlinked: String { ShellLines.line("record", "me.unlinked") }staticvar 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.structPersonPageRoute: View {let id: Stringlet database: String? @Environment(\.cottonCore) privatevar core @Environment(\.tableDefinitions) privatevar definitions @State privatevar stores = PersonPageStores()var body: someView {iflet people = definitions.playing(.person) {iflet 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.@MainActorfinalclassPersonPageStores {privatevar made: (database: String, core: DatabaseScopedCore, records: CoreRecords)?func store(core: any CottonCalling, database: String, bundle: Bundle? = nil) -> (core: DatabaseScopedCore, records: CoreRecords) {iflet 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) }}