// DELETE ACCOUNT (assets/design/shared/DELETE_ACCOUNT.md; owner, 22 Nov 2026:// the App Store asks that a person can delete their account from inside the// app, 5.1.1(v)). As Android's `DeleteAccountPart.kt`: Settings › Account,// last, one row in the app's danger ink, only while signed in; a tap opens a// page over the app in its own look: what goes, what stays, that it is every// Cotton app and cannot be undone. Send code by text asks a fresh code to the// account's number (`account.deleteCode`); the code typed back and Delete my// account deletes it (`account.delete`). The core has then forgotten the copy;// `PersonalObject.accountDeleted` forgets the sign-in and the data key and the// app opens afresh. Every word is the core's (`account.deletePage`).#if os(iOS)importCottonKitimportSwiftUI/// `account.deletePage`'s words.structDeleteAccountWords: Decodable, Equatable, Sendable {let row: Stringlet heading: Stringlet whatGoes: Stringlet whatStays: String/// When it is deleted (30 days on, DELETE_ACCOUNT.md "(30 days)": `undo` is now `when`); a core before that says `undo`.var when: String?var undo: String?let otherApps: Stringlet send: Stringlet codeLabel: Stringlet confirm: Stringlet cancel: Stringlet working: Stringvar offline: String?/// The warning in the danger ink: when it is deleted, or (an older core) that it cannot be undone.var warning: String { when ?? undo ?? "" }}/// `account.deleteCode`'s answer.structDeleteAccountCode: Decodable, Equatable, Sendable {let challengeId: Stringvar sent: String?}/// `account.delete`'s answer.structDeleteAccountDone: Decodable, Equatable, Sendable {var heading: String?var line: String?var databaseId: String?}/// The calls the page makes, so a test stands an invented core in.protocolAccountDeleting: Sendable {func page(app: String) asyncthrows -> DeleteAccountWordsfunc code() asyncthrows -> DeleteAccountCodefunc delete(challengeId: String, code: String, app: String) asyncthrows -> DeleteAccountDone}structCoreAccountDeleting: AccountDeleting {let core: any CottonCallingprivatestructApp: Encodable { let app: String }privatestructConfirm: Encodable { let challengeId: String; let code: String; let app: String }func page(app: String) asyncthrows -> DeleteAccountWords { tryawait core.call("account.deletePage", App(app: app)) }func code() asyncthrows -> DeleteAccountCode { tryawait core.call("account.deleteCode") }func delete(challengeId: String, code: String, app: String) asyncthrows -> DeleteAccountDone {tryawait core.call("account.delete", Confirm(challengeId: challengeId, code: code, app: app)) }}/// Where the page stands: its words, the code asked, what was typed, what went wrong.@MainActor @ObservablefinalclassDeleteAccountFlow {let app: String @ObservationIgnored privatelet deleting: any AccountDeletingprivate(set) var words: DeleteAccountWords?varopen = falseprivate(set) var challenge: String?private(set) var sent: String?var code = "" { didSet {let digits = String(code.filter(\.isNumber).prefix(8))if digits != code { code = digits } } }private(set) var problem: String?private(set) var busy = falseinit(app: String, deleting: any AccountDeleting) {self.app = appself.deleting = deleting }func load() async {if words == nil { words = try? await deleting.page(app: app) } }/// Send code by text: a fresh code to the account's own number.func send() async {guard !busy else { return } busy = true problem = nildefer { busy = false }do {let asked = tryawait deleting.code() challenge = asked.challengeId sent = asked.sent } catch { problem = said(error) } }/// Delete my account, with the code typed back: the answer on success, nil when it was refused (said under the field).func confirm() async -> DeleteAccountDone? {guard !busy, let challenge, !code.isEmpty else { returnnil } busy = true problem = nildefer { busy = false }do {let done = tryawait deleting.delete(challengeId: challenge, code: code, app: app)open = falsereturn done } catch { problem = said(error)returnnil } }/// Go back: asks nothing, and forgets what was typed.func close() {guard !busy else { return }open = false challenge = nil sent = nil code = "" problem = nil }/// The core's own words for a refusal (a wrong or old code, offline); its offline line otherwise.privatefunc said(_ error: Error) -> String? {iflet refused = error as? CottonCallError, !refused.message.isEmpty { return refused.message }return words?.offline }}/// The row, last under Settings › Account while signed in, and the page it opens.structDeleteAccountPart: View {let look: SettingsLook/// After the account is deleted: the app goes to its first screen (Days' today, Story's first page, Words' notebook)./// The core's done line is said once by the shell's passing line (`PersonalObject.say`), which stands over the/// app while its root is made again on the fresh database; a line the old root said went with it.let onDeleted: () -> Void @Environment(PersonalObject.self) privatevar personal: PersonalObject? @State privatevar flow: DeleteAccountFlowinit(core: (any CottonCalling)?, look: SettingsLook, onDeleted: @escaping () -> Void) {self.look = lookself.onDeleted = onDeletedlet app = FlavourManifest.current?.name ?? "Cotton" _flow = State(initialValue: DeleteAccountFlow(app: app, deleting: core.map { CoreAccountDeleting(core: $0) } ?? NoAccountDeleting())) }var body: someView {iflet personal, personal.stand != .local {// A stack that stands while the words are still unread: a `.task` on an empty Group never runs. VStack(alignment: .leading, spacing: 0) {iflet words = flow.words {Button { flow.open = true } label: {Text(verbatim: words.row).font(look.body).foregroundStyle(look.danger ?? look.ink) .frame(maxWidth: .infinity, minHeight: 44, alignment: .leading) .contentShape(Rectangle()) } .buttonStyle(.plain) .padding(.top, 14) .accessibilityIdentifier("account-delete") } } .frame(maxWidth: .infinity, alignment: .leading) .task { await flow.load() } .fullScreenCover(isPresented: Binding(get: { flow.open }, set: { if !$0 { flow.close() } })) {DeleteAccountPage(flow: flow, look: look) { done inTask { onDeleted()await personal.accountDeleted(databaseId: done.databaseId, heading: done.heading, line: done.line) } } } } }}/// The page over the app, in the app's own look.structDeleteAccountPage: View { @Bindable var flow: DeleteAccountFlowlet look: SettingsLooklet onDeleted: (DeleteAccountDone) -> Void @FocusState privatevar typing: Boolprivatestaticlet foot = "account-delete-foot"var body: someView {ScrollViewReader { proxy inScrollView {iflet words = flow.words { VStack(alignment: .leading, spacing: 18) {Text(verbatim: words.heading).font(look.title ?? look.body).foregroundStyle(look.ink) .fixedSize(horizontal: false, vertical: true) .accessibilityAddTraits(.isHeader)ForEach([words.whatGoes, words.whatStays, words.otherApps], id: \.self) { line inText(verbatim: line).font(look.body).foregroundStyle(look.ink).fixedSize(horizontal: false, vertical: true) }Text(verbatim: words.warning).font(look.body).foregroundStyle(look.danger ?? look.ink) .fixedSize(horizontal: false, vertical: true)if flow.challenge == nil { look.act(words.send, "account-delete-send", !flow.busy) { Task { await flow.send() } } } else {iflet sent = flow.sent {Text(verbatim: sent).font(look.body).foregroundStyle(look.ink).fixedSize(horizontal: false, vertical: true) }TextField(text: $flow.code) { Text(verbatim: words.codeLabel) } .font(look.title ?? look.body) .foregroundStyle(look.ink) .keyboardType(.numberPad) .textContentType(.oneTimeCode) .focused($typing) .padding(14) .overlay(RoundedRectangle(cornerRadius: 12).strokeBorder(look.ink, lineWidth: 1.5)) .accessibilityLabel(Text(verbatim: words.codeLabel)) .accessibilityIdentifier("account-delete-code") .onAppear { typing = true } look.act(flow.busy ? words.working : words.confirm, "account-delete-confirm", !flow.busy && !flow.code.isEmpty) {Task { iflet done = await flow.confirm() { onDeleted(done) } } } }Color.clear.frame(height: 1).id(Self.foot)iflet problem = flow.problem {Text(verbatim: problem).font(look.small).foregroundStyle(look.danger ?? look.ink) .fixedSize(horizontal: false, vertical: true) .accessibilityIdentifier("account-delete-problem") } look.act(words.cancel, "account-delete-cancel", !flow.busy) { flow.close() } } .padding(.horizontal, 24) .padding(.vertical, 28) .frame(maxWidth: 520, alignment: .leading) .frame(maxWidth: .infinity) } }// The code asked: Delete my account and Go back stand above the keyboard, not under it (launch-ios walk).// Once the keyboard has risen (it rises after the field takes focus), and again as the code is typed. .onChange(of: flow.challenge) { _, asked inguard asked != nilelse { return }Task {try? awaitTask.sleep(for: .milliseconds(900)) withAnimation(.easeOut(duration: 0.2)) { proxy.scrollTo(Self.foot, anchor: .bottom) } } } .onChange(of: flow.code.count) { before, now inif before == 0, now > 0 { withAnimation(.easeOut(duration: 0.2)) { proxy.scrollTo(Self.foot, anchor: .bottom) } } }// Words scrolled up stand under the status bar on the page's ground, never over the clock. .overlay(alignment: .top) {Color.clear.frame(height: 0).background(look.ground, ignoresSafeAreaEdges: .top) } } .background(look.ground.ignoresSafeArea()) .accessibilityIdentifier("account-delete-page") .interactiveDismissDisabled(flow.busy) }}/// No core (a look, a preview): nothing can be deleted, and the row does not draw.privatestructNoAccountDeleting: AccountDeleting {func page(app: String) asyncthrows -> DeleteAccountWords { throwCancellationError() }func code() asyncthrows -> DeleteAccountCode { throwCancellationError() }func delete(challengeId: String, code: String, app: String) asyncthrows -> DeleteAccountDone { throwCancellationError() }}/// After Delete account, the core's heading and line, standing over the app (fresh on its new database) until Close.structAccountDeletedCard: View {let said: String @Environment(\.closeCard) privatevar closevar body: someView { VStack(alignment: .leading, spacing: 18) {Text(verbatim: said).paperBody() .fixedSize(horizontal: false, vertical: true) .accessibilityIdentifier("account-deleted-said")PaperTypedButton(ShellLines.line("words", "close")) { close?() } .accessibilityIdentifier("account-deleted-close") } .padding(24) }}#endif