// 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) import CottonKit import SwiftUI /// `account.deletePage`'s words. struct DeleteAccountWords: Decodable, Equatable, Sendable { let row: String let heading: String let whatGoes: String let 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: String let send: String let codeLabel: String let confirm: String let cancel: String let working: String var 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. struct DeleteAccountCode: Decodable, Equatable, Sendable { let challengeId: String var sent: String? } /// `account.delete`'s answer. struct DeleteAccountDone: 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. protocol AccountDeleting: Sendable { func page(app: String) async throws -> DeleteAccountWords func code() async throws -> DeleteAccountCode func delete(challengeId: String, code: String, app: String) async throws -> DeleteAccountDone } struct CoreAccountDeleting: AccountDeleting { let core: any CottonCalling private struct App: Encodable { let app: String } private struct Confirm: Encodable { let challengeId: String; let code: String; let app: String } func page(app: String) async throws -> DeleteAccountWords { try await core.call("account.deletePage", App(app: app)) } func code() async throws -> DeleteAccountCode { try await core.call("account.deleteCode") } func delete(challengeId: String, code: String, app: String) async throws -> DeleteAccountDone { try await 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 @Observable final class DeleteAccountFlow { let app: String @ObservationIgnored private let deleting: any AccountDeleting private(set) var words: DeleteAccountWords? var open = false private(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 = false init(app: String, deleting: any AccountDeleting) { self.app = app self.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 = nil defer { busy = false } do { let asked = try await 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 { return nil } busy = true problem = nil defer { busy = false } do { let done = try await deleting.delete(challengeId: challenge, code: code, app: app) open = false return done } catch { problem = said(error) return nil } } /// 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. private func said(_ error: Error) -> String? { if let 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. struct DeleteAccountPart: 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) private var personal: PersonalObject? @State private var flow: DeleteAccountFlow init(core: (any CottonCalling)?, look: SettingsLook, onDeleted: @escaping () -> Void) { self.look = look self.onDeleted = onDeleted let app = FlavourManifest.current?.name ?? "Cotton" _flow = State(initialValue: DeleteAccountFlow(app: app, deleting: core.map { CoreAccountDeleting(core: $0) } ?? NoAccountDeleting())) } var body: some View { if let 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) { if let 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 in Task { onDeleted() await personal.accountDeleted(databaseId: done.databaseId, heading: done.heading, line: done.line) } } } } } } /// The page over the app, in the app's own look. struct DeleteAccountPage: View { @Bindable var flow: DeleteAccountFlow let look: SettingsLook let onDeleted: (DeleteAccountDone) -> Void @FocusState private var typing: Bool private static let foot = "account-delete-foot" var body: some View { ScrollViewReader { proxy in ScrollView { if let 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 in Text(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 { if let 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 { if let done = await flow.confirm() { onDeleted(done) } } } } Color.clear.frame(height: 1).id(Self.foot) if let 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 in guard asked != nil else { return } Task { try? await Task.sleep(for: .milliseconds(900)) withAnimation(.easeOut(duration: 0.2)) { proxy.scrollTo(Self.foot, anchor: .bottom) } } } .onChange(of: flow.code.count) { before, now in if 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. private struct NoAccountDeleting: AccountDeleting { func page(app: String) async throws -> DeleteAccountWords { throw CancellationError() } func code() async throws -> DeleteAccountCode { throw CancellationError() } func delete(challengeId: String, code: String, app: String) async throws -> DeleteAccountDone { throw CancellationError() } } /// After Delete account, the core's heading and line, standing over the app (fresh on its new database) until Close. struct AccountDeletedCard: View { let said: String @Environment(\.closeCard) private var close var body: some View { 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