Cotton code
160 lines · 7.0 KB · SwiftRaw
// One device asking to join, on its own page.//// ON PAPER, and the same key tag the waiting list draws — a straw card with a// punched hole at its head — because it is the same object: the push and the// list are two ways to reach one request, and a person who has seen it in one// place should recognise it in the other.//// What is bigger here is what a person compares: the NAME largest, then the// question with the six digits under it at the size a number held up against// another screen has to be read at. The two verbs are the list's own words —// "Yes, let it in" and "No" — because they answer the question the card asks// rather than naming a mechanism.//// There is no hold-to-approve here, unlike the OAuth consent screen beside it.// That screen's press-and-hold guards an act a person did NOT initiate — a page// somewhere asked, and the stamp is the deliberation. This one is already// deliberate twice over: the person compared two numbers, and the second factor// is that comparison, not the shape of the tap.import CottonKitimport SwiftUIpublic struct JoinRequestScreen: View {    @Bindable private var model: JoinRequestModel    private let onDone: () -> Void    @Environment(\.theme) private var theme    public init(model: JoinRequestModel, onDone: @escaping () -> Void = {}) {        self.model = model        self.onDone = onDone    }    public var body: some View {        PaperScreen(title: ShellLines.line("devices", "a_device_wants_to")) {            VStack(alignment: .leading, spacing: RecordListSurfaceKnobs.paperGap) {                pane            }            .frame(maxWidth: .infinity, alignment: .leading)            // A settings page is a form, and a form has a measure — see            // `Platform/SettingsFormWidth.swift`.            .settingsFormWidth()        }        .task { await model.load() }        .onChange(of: model.answered) { _, answered in            if answered { onDone() }        }        .accessibilityIdentifier("join-request")    }    @ViewBuilder private var pane: some View {        switch model.state {        case .loading:            Text(ShellLines.line("devices", "looking_for_it"))                .paperHand(20)                .accessibilityIdentifier("join-request-loading")        case .settled:            // NOT an error. The request was answered on another device, or it            // ran out its ten minutes — both are the system working, and the            // sentence says which possibilities they are rather than implying            // something went wrong.            Text(ShellLines.line("devices", "this_request_isn_t"))                .paperBody(15)                .fixedSize(horizontal: false, vertical: true)                .accessibilityIdentifier("join-request-settled")        case .refused(let sentence):            Text(sentence)                .paperBody(15)                .fixedSize(horizontal: false, vertical: true)                .accessibilityIdentifier("join-request-refused")        case .waiting(let pairing):            card(pairing)        }    }    private func card(_ pairing: JSONValue) -> some View {        Paper(dye: .straw, edge: .cut, seed: "join-request-\(model.pairingId)", tilt: false, inset: 22) {            VStack(alignment: .leading, spacing: 0) {                Text(ShellLines.line("devices", "wants_to_join", ["name": "\(model.name)"]))                    .paperTitle(26)                    .fixedSize(horizontal: false, vertical: true)                    .accessibilityIdentifier("join-request-name")                if let detail = DeviceWords.detail(                    platform: pairing.text("platform"),                    appVersion: pairing.text("appVersion")                ) {                    Text(detail).paperMeta(13).padding(.top, 4)                }                if let age = pendingPairingAge(pairing.day("createdAt")?.recent) {                    Text(age)                        .paperHand(18)                        .padding(.top, 6)                        .accessibilityIdentifier("join-request-age")                }                if let digits = model.digits {                    Text(ShellLines.line("devices", "does_it_show", ["digits": "\(pairingDigitsGrouped(digits))"]))                        .paperBody(15)                        .padding(.top, 18)                    JoinRequestDigits(digits: digits)                        .padding(.top, 10)                        .accessibilityRepresentation { Text(digits) }                        .accessibilityIdentifier("join-request-digits")                } else {                    Text(DeviceRefusalWords.noDigits)                        .paperBody(15)                        .fixedSize(horizontal: false, vertical: true)                        .padding(.top, 18)                        .accessibilityIdentifier("join-request-no-digits")                }                Text(ShellLines.line("devices", "once_approved_it_can"))                    .paperMeta(13)                    .fixedSize(horizontal: false, vertical: true)                    .padding(.top, 16)                HStack(alignment: .center, spacing: 14) {                    PaperTypedButton("No", destructive: true) {                        Task { await model.decline() }                    }                    .disabled(model.busy)                    .accessibilityIdentifier("join-request-decline")                    Spacer(minLength: 0)                    PaperInkCircle(                        label: model.busy ? ShellLines.line("devices", "approving") : ShellLines.line("devices", "yes_let_it_in"),                        isEnabled: (model.digits != nil && !model.busy) || model.busy,                        isBusy: model.busy                    ) {                        Task { await model.approve() }                    }                    .accessibilityIdentifier("join-request-approve")                }                .padding(.top, 20)            }            .frame(maxWidth: .infinity, alignment: .leading)        }        .accessibilityElement(children: .contain)        .accessibilityIdentifier("join-request-card")    }}/// The six, at the size a number compared against another screen is read at./// A drawing of one number, which is why it is hidden from accessibility and the/// card carries the digits as one string instead.private struct JoinRequestDigits: View {    let digits: String    @Environment(\.theme) private var theme    var body: some View {        HStack(spacing: 8) {            ForEach(Array(pairingDigitSlots(digits).enumerated()), id: \.offset) { _, slot in                Text(slot)                    .paperBody(32)                    .frame(width: 44, height: 58)                    .background(RoundedRectangle(cornerRadius: 3).fill(PaperDye.cream.fill.opacity(0.7)))                    .overlay {                        RoundedRectangle(cornerRadius: 3)                            .stroke(theme.print(.pencil).opacity(0.5), lineWidth: 1)                    }            }        }        .accessibilityHidden(true)    }}