Cotton code
95 lines · 4.7 KB · SwiftRaw
// THE RECOVERY CODE IN SETTINGS › ACCOUNT (GETTING-IN.md, 22 Nov 2026): on any// device that opens the person's things, in Words, Days, Story and Comms, the// code's line ("Recovery code made 15 November"), Show my recovery code and// Make a new code. Make a new code asks first (`makeNewConfirm`: the old code// then stops working), then the phone's lock, then shows the new code with Save// to Files and Copy, as the first offer. Drawn in the app's own settings look// (`SettingsLook`), beside Delete account. Every word is the core's// (`recovery.status`).#if os(iOS)import CottonKitimport SwiftUIimport UniformTypeIdentifiersprivate struct RecoveryKeysKey: EnvironmentKey {    static let defaultValue: RecoveryKeys? = nil}extension EnvironmentValues {    /// This window's recovery code, the iCloud copy and the trusted person (`SealedKeys.recovery`).    var recoveryKeys: RecoveryKeys? {        get { self[RecoveryKeysKey.self] }        set { self[RecoveryKeysKey.self] = newValue }    }}struct RecoveryAccountPart: View {    let look: SettingsLook    @Environment(\.recoveryKeys) private var recovery    @State private var confirming = false    @State private var exporting = false    @Environment(\.dynamicTypeSize) private var typeSize    /// The acts side by side, or one over another at the accessibility sizes, where two side by side broke a word    /// ("Show my recov / ery code", Data at the largest text, 26 Nov).    private var acts: AnyLayout {        typeSize.isAccessibilitySize ? AnyLayout(VStackLayout(alignment: .leading, spacing: 10)) : AnyLayout(HStackLayout(spacing: 12))    }    var body: some View {        if let recovery, recovery.status?.flag("holdsVault") == true {            VStack(alignment: .leading, spacing: 8) {                // A code shown from here stands here, over the settings, not behind them.                if let shown = recovery.shown {                    Text(verbatim: shown.code)                        .font(.system(size: 19, weight: .semibold, design: .monospaced))                        .foregroundStyle(look.ink)                        .textSelection(.enabled)                        .fixedSize(horizontal: false, vertical: true)                        .accessibilityIdentifier("account-recovery-code")                    acts {                        if let saveFiles = recovery.line("saveFiles"), recovery.file != nil {                            look.act(saveFiles, "account-recovery-save-files", true) { exporting = true }                        }                        if let copy = recovery.line("copy") {                            look.act(copy, "account-recovery-copy", true) { recovery.copy() }                        }                        if let done = recovery.line("done") {                            look.act(done, "account-recovery-done", true) { Task { await recovery.dismiss() } }                        }                    }                }                if let line = recovery.status?["code"]?.text("line") {                    Text(verbatim: line).font(look.body).foregroundStyle(look.ink)                        .fixedSize(horizontal: false, vertical: true)                        .accessibilityIdentifier("account-recovery-line")                } else if let none = recovery.line("none") {                    Text(verbatim: none).font(look.body).foregroundStyle(look.ink)                        .fixedSize(horizontal: false, vertical: true)                }                if recovery.shown == nil { acts {                    let code = recovery.status?["code"]                    if code?.text("made") != nil, code?.maybeFlag("current") != false, let show = recovery.line("show") {                        look.act(show, "account-recovery-show", !recovery.busy) { Task { await recovery.show() } }                    }                    if let make = recovery.line("makeNew") {                        look.act(make, "account-recovery-make", !recovery.busy) { confirming = true }                    }                } }            }            .padding(.top, 14)            .frame(maxWidth: .infinity, alignment: .leading)            .task { await recovery.refresh() }            .fileExporter(isPresented: $exporting, document: recovery.file, contentType: .plainText,                          defaultFilename: recovery.line("fileName") ?? "") { result in                recovery.savedToFiles((try? result.get()) != nil)            }            .confirmDecision(isPresented: $confirming, title: recovery.line("makeNewConfirm") ?? "", message: "",                             confirmLabel: recovery.line("makeNew") ?? "", identifier: "account-recovery-make-confirm") {                Task { await recovery.makeNew() }            }        }    }}#endif