Cotton code
190 lines · 8.2 KB · SwiftRaw
import SwiftUI/// First launch: the number, then a code by text or the approval of a phone/// already signed in (it shows a code here to type there), then the app.struct SignInView: View {    @Bindable var model: AppModel    @FocusState private var focus: Field?    private enum Field { case phone, code }    var body: some View {        VStack(alignment: .leading, spacing: 4) {            Text("Cotton")                .font(Faces.title)                .tracking(-0.17)            Text("This Mac imports into your database. Sign in once.")                .font(Faces.small)                .foregroundStyle(Palette.quiet)        }        .padding(.top, 2)        .padding(.horizontal, 4)        VStack(alignment: .leading, spacing: 10) {            if let note = model.status?.signInNote {                Text(note)                    .font(Faces.small)                    .foregroundStyle(Palette.warnText)                    .fixedSize(horizontal: false, vertical: true)                    .accessibilityIdentifier("signin-note")                    .padding(.bottom, 4)            }            Text("Phone number")                .font(Faces.small)                .foregroundStyle(Palette.quiet)            FieldBox(placeholder: "+91", isEmpty: model.phoneInput.isEmpty) {                TextField("", text: $model.phoneInput)                    .focused($focus, equals: .phone)                    .onSubmit { model.askCode() }            }            if model.challengeId != nil {                Text("Code")                    .font(Faces.small)                    .foregroundStyle(Palette.quiet)                    .padding(.top, 6)                FieldBox(font: Faces.code, tracking: 6, placeholder: "6 digits", isEmpty: model.codeInput.isEmpty) {                    TextField("", text: $model.codeInput)                        .focused($focus, equals: .code)                        .onSubmit { model.signIn() }                }            }            if let error = model.signInError {                Text(error)                    .font(Faces.small)                    .foregroundStyle(Palette.warnText)                    .fixedSize(horizontal: false, vertical: true)            }            if let asking = model.asking {                AskCardView(card: asking, model: model)            } else {                Button(model.challengeId == nil ? "Send code" : "Sign in") {                    if model.challengeId == nil { model.askCode() } else { model.signIn() }                }                .buttonStyle(PrimaryButtonStyle(enabled: !model.signInBusy))                .disabled(model.signInBusy)                .padding(.top, 8)            }        }        .padding(.vertical, 16)        .padding(.horizontal, 14)        .frame(maxWidth: .infinity, alignment: .leading)        .background(            RoundedRectangle(cornerRadius: 12, style: .continuous)                .fill(Palette.card)                .shadow(color: Palette.cardShadow, radius: 0, x: 0, y: 1)        )        .onChange(of: model.challengeId) { _, id in            if id != nil { focus = .code }        }        VStack(alignment: .leading, spacing: 6) {            Text("After sign-in")                .font(Faces.smallStrong)                .foregroundStyle(Palette.ink)            Text("WhatsApp, Mail, Photos and Calls start on their own schedules and push to \(URL(string: AppModel.objectURL)?.host() ?? "your database"). Nothing is kept on this Mac beyond the next run.")                .fixedSize(horizontal: false, vertical: true)        }        .font(Faces.small)        .foregroundStyle(Palette.quiet)        .padding(.vertical, 12)        .padding(.horizontal, 14)        .frame(maxWidth: .infinity, alignment: .leading)        .background(RoundedRectangle(cornerRadius: 12, style: .continuous).fill(Palette.note))        Spacer(minLength: 0)    }}/// The approval this Mac asks for: the core's words, and the code to type on/// the phone once it opened the ask; Cancel, or Ask again once it is over.struct AskCardView: View {    let card: AppModel.AskCard    let model: AppModel    /// The recovery code field, behind No other device? (GETTING-IN.md: one way shown, the last resort one tap away).    @State private var noOther = false    var body: some View {        VStack(alignment: .leading, spacing: 8) {            if let heading = card.heading {                Text(heading).font(Faces.smallStrong).foregroundStyle(Palette.ink)            }            if let line = card.line {                Text(line)                    .font(Faces.small)                    .foregroundStyle(card.done && card.state != "approved" ? Palette.warnText : Palette.quiet)                    .fixedSize(horizontal: false, vertical: true)            }            if let square = card.qr, square.size > 0, !card.done {                SquareCode(square: square)                    .frame(width: 180, height: 180)                    .frame(maxWidth: .infinity)                    .accessibilityIdentifier("signin-ask-square")            }            if let codeLine = card.codeLine, !card.done {                Text(codeLine).font(Faces.small).foregroundStyle(Palette.quiet).fixedSize(horizontal: false, vertical: true)            }            if let code = card.code {                Text(code)                    .font(.system(size: 28, weight: .semibold, design: .monospaced))                    .foregroundStyle(Palette.ink)                    .textSelection(.enabled)                    .accessibilityIdentifier("signin-ask-code")            }            if let left = card.left, !card.done {                Text(left).font(Faces.small).foregroundStyle(Palette.quiet)            }            if let check = card.check, !card.done {                Text(check).font(Faces.small).foregroundStyle(Palette.quiet).fixedSize(horizontal: false, vertical: true)            }            if card.done {                Button(card.again ?? "Ask again") { model.askApprovalHere() }                    .buttonStyle(PrimaryButtonStyle(enabled: !model.signInBusy))                    .disabled(model.signInBusy)                    .accessibilityIdentifier("signin-ask-again")            }            if let noDevice = card.noDevice, !noOther {                Button(noDevice) { noOther = true }                    .buttonStyle(.plain)                    .font(Faces.smallStrong)                    .foregroundStyle(Palette.link)                    .accessibilityIdentifier("signin-ask-no-device")            }            if noOther {                HStack(spacing: 6) {                    @Bindable var model = model                    SecureField("Recovery code", text: $model.recoveryCodeInput)                        .textFieldStyle(.roundedBorder)                        .onSubmit { model.openByRecoveryCode() }                        .accessibilityIdentifier("vault-recovery-code")                    Button("Open") { model.openByRecoveryCode() }                        .disabled(model.recoveryBusy || model.recoveryCodeInput.isEmpty)                        .accessibilityIdentifier("vault-recovery-open")                }                if let error = model.recoveryError {                    Text(error).font(Faces.small).foregroundStyle(Palette.warnText).fixedSize(horizontal: false, vertical: true)                }            }            // Sign out is the panel's footer's, on this screen too: the card's own would say it twice.        }        .padding(.top, 8)    }}/// The core's square code, drawn module by module on white with a quiet border of four.struct SquareCode: View {    let square: AppModel.AskCard.Square    var body: some View {        Canvas { context, size in            let modules = CGFloat(square.size + 8)            let unit = min(size.width, size.height) / modules            context.fill(Path(CGRect(origin: .zero, size: size)), with: .color(.white))            for (y, row) in square.rows.enumerated() {                for (x, bit) in row.enumerated() where bit == "1" {                    let cell = CGRect(x: (CGFloat(x) + 4) * unit, y: (CGFloat(y) + 4) * unit, width: unit, height: unit)                    context.fill(Path(cell), with: .color(.black))                }            }        }    }}