// What the phone keeps between launches, and the ONE rule about it: **the // phone persists a NO, never a yes.** // // There is no approved flag and there must not be one (pairing PR 7 deleted the // one the bridge read): the SERVER's row says who is in, and a phone that // remembered "yes" would be a second, staler opinion about an approval only the // server can grant. // // **And the NO is not kept here either — it is RUST's.** The transport that was // refused writes the terminal note to `client/refusal.json` and `read.boot` // hands it back as `refusal`, so a relaunch paints the sentence without Swift // remembering anything. A `UserDefaults` copy of the same fact was a second // opinion on the wrong side of the boundary: the two could disagree the moment // a leave cleared one of them, and the one the screen read was the stale one. // `NoStandingTests` asserts the key is ABSENT, so it cannot come back quietly. // // What is left is the NOTIFICATION SWITCHES, which are nobody's claim about // anything, and the suite they live in. (`@cotton:theme` went with the // appearance preference: the app is light-only and there is nothing to store.) // // The server address and the workspace cache are not here either. They live in // Rust's `client/connection.json`, and Swift holds no copy — one writer, one // reader, one file (Cotton note "Devices — how a machine is admitted to your database"). // // Storage is ONE `UserDefaults` suite, the app group's, because the share // extension reads the same container. The older `@cotton:` prefix survives // inside the key names because a defaults key is on-device state and renaming // one silently drops what an install already holds; the suite replaces the // prefix's job of namespacing. // // Guard: `NoStandingTests`, which reads the declared list below and fails on a // key that is not on it. import Foundation /// Every defaults key this app writes, declared once. /// /// A test reads this list. Adding a key means adding a line here AND saying /// which side of the rule it is on — a preference, or a refusal. There is no /// third kind, and **every key here is a preference**: a NO belongs below the /// boundary, where Rust keeps it. public enum StandingKey: String, CaseIterable, Sendable { /// The notification switches — the master, the four categories, and /// whether the one inline prompt has been spent (4j). /// /// A preference, and DEVICE-LOCAL by design: nothing about it is an op, a /// second phone has its own, and the crate takes it as an argument on every /// reminder call rather than looking for a place to keep it. The previous /// build's key is kept verbatim, because a defaults key is on-device state /// and renaming one drops what the install already holds. case notificationPrefs = "@cotton:notifprefs" } /// The key this app used to write and must not write again. /// /// Named rather than merely deleted: `NoStandingTests` asserts it is absent /// from `StandingKey` and from every Swift source, so "Rust owns the refusal /// note" is a fact a future reader trips over rather than a paragraph they have /// to find. public let RETIRED_REFUSAL_KEY = "@cotton:device:refusal" /// Which family of bundles is asking — because the two platforms name their /// app group differently and the difference is the SYSTEM's, not ours. /// /// A parameter rather than a bare `#if` inside the derivation, so both answers /// are reachable from a test running on either platform. `AppGroupTests` drives /// every bundle id in both families, on whichever system runs it. public enum CottonBundleFamily: Sendable, Equatable, CaseIterable { /// The iPhone app and its three extensions. case phone /// CottonDB for Mac. case mac /// What this build is. public static var current: CottonBundleFamily { #if os(macOS) return .mac #else return .phone #endif } } /// The app group's suite — the one place preferences live. public enum CottonDefaults { /// The phone's app id when no bundle says one — a library test. It follows /// the build's flavor (`AppFlavor`), so an unstamped process is Cotton Dev. static var phoneFallbackBundleIdentifier: String { AppFlavor.current.phoneAppID } /// **The Mac's app group, and it is TEAM-PREFIXED on purpose.** /// /// The phone's group is `group.com.cottondb.cotton`, derived below. /// The Mac's is not derived at all — it is this literal — and the team id in /// front of it is DATA (the same `M7V3QVXZQH` `project.yml` signs with), /// written here rather than fetched because a build that guesses its team /// silently writes to a container nothing reads. /// /// Why not `group.`: on macOS 15 and later a `group.`-prefixed container is /// only granted without a prompt to an app carrying an embedded /// provisioning profile that lists it. CottonDB for Mac is hand-assembled by /// `scripts/build-app.sh` and signed ad-hoc by default — that lane cannot /// embed a profile — so every development build would raise "CottonDB wants /// to access data from other apps" before anything could read a byte. A /// `.` group needs no profile. It is macOS-only, which costs /// nothing: the phone's group is untouched and the two files are different /// files on different machines anyway. /// /// `FileManager.containerURL(forSecurityApplicationGroupIdentifier:)` hands /// back a URL on macOS even for a group the process may not use, so nothing /// treats a non-nil URL as proof — the proof is a write that reads back /// (`AppGroupTests`). /// /// Per flavor (`AppFlavor.macAppGroup`): Cotton Dev's is the same literal /// with `.dev`, so the two Mac apps never share a container. public static var macAppGroup: String { AppFlavor.current.macAppGroup } /// The app group for a bundle id. /// /// On the PHONE it is derived from the bundle id exactly as `project.yml` /// derives `COTTON_APP_GROUP` — read rather than written out so a variant /// cannot half-apply. On the MAC every bundle of one flavor answers that /// flavor's literal, so CottonDB and Cotton Dev each have one container. public static func appGroup( bundleIdentifier: String?, family: CottonBundleFamily = .current ) -> String { if family == .mac { // Read off the bundle id when there is one, so the table in // `AppGroupTests` can drive both flavors in one process. // Anything that is neither app's id is this build's own flavor. let named = { (flavor: AppFlavor) in bundleIdentifier == flavor.macBundleID || bundleIdentifier?.hasPrefix(flavor.macBundleID + ".") == true } if named(.dev) { return AppFlavor.dev.macAppGroup } if named(.real) { return AppFlavor.real.macAppGroup } return macAppGroup } let base = bundleIdentifier ?? phoneFallbackBundleIdentifier // An extension's bundle id appends its own suffix (the share // extension's, Cotton Keys' passkey provider's, Messages' notification // extension's); the GROUP is the // app's. Trimming it keeps one derivation for every target. for suffix in [".share-extension", ".provider", ".notifications"] where base.hasSuffix(suffix) { return "group." + String(base.dropLast(suffix.count)) } return "group." + base } /// The suite, or `.standard` when the group is not reachable — which is a /// simulator running a library test with no entitlement, never a shipping /// app. public static func suite(bundleIdentifier: String? = Bundle.main.bundleIdentifier) -> UserDefaults { UserDefaults(suiteName: appGroup(bundleIdentifier: bundleIdentifier)) ?? .standard } }