Cotton code
87 lines · 3.4 KB · SwiftRaw
// Find's own private state: the searches that OPENED something.//// DEVICE-LOCAL by design, like the page's visit ledger: what you searched your// own database for is exactly the kind of record the memory product promises// never leaves the phone uninvited. It does not survive an uninstall, and that// is the right cost.//// **The store is a seam, and only because of the cap.** There is no// AsyncStorage here and no `createPersistedAtom`; `UserDefaults` is the whole// mechanism. It is behind a protocol so the cap, the dedupe and the ordering are// tested with no device, no container and no suite to clean up — the same reason// `SecretStore` has an in-memory twin. **Nothing else is persisted here**: the// query being typed, the expanded sections and the hits are all per-visit, and// an expansion is an answer to THIS answer rather than a setting.import Foundation/// How many recent searches the zero state offers back.public let searchRecentsCap = 8/// Where the recents live between launches.public protocol SearchRecentsStore: Sendable {    func load() -> [String]    func save(_ recents: [String])}/// The real one./// `@unchecked Sendable` because `UserDefaults` is thread-safe by contract and/// carries no annotation saying so; the struct holds nothing else.public struct UserDefaultsSearchRecents: SearchRecentsStore, @unchecked Sendable {    /// The previous app's own key, kept. Nothing reads that older store from    /// Swift — the two runtimes share no container for it — so this is a NEW    /// list on the first launch either way; the key carries across so a person    /// reading the defaults finds one name for one thing.    public static let key = "@cotton/find-recents"    private let defaults: UserDefaults    public init(defaults: UserDefaults = .standard) {        self.defaults = defaults    }    public func load() -> [String] {        // `stringArray(forKey:)` answers nil for an absent key AND for a value        // that is not an array of strings, which is the same "no recents" to a        // screen that has never been used.        defaults.stringArray(forKey: UserDefaultsSearchRecents.key) ?? []    }    public func save(_ recents: [String]) {        defaults.set(recents, forKey: UserDefaultsSearchRecents.key)    }}/// The double every test drives.public final class InMemorySearchRecents: SearchRecentsStore, @unchecked Sendable {    private let lock = NSLock()    private var recents: [String]    public init(_ recents: [String] = []) {        self.recents = recents    }    public func load() -> [String] {        lock.lock()        defer { lock.unlock() }        return recents    }    public func save(_ recents: [String]) {        lock.lock()        defer { lock.unlock() }        self.recents = recents    }}/// A search earns a place here only when it OPENED something — recording every/// keystroke would fill the list with the seven prefixes of one thought.////// Case-insensitive dedupe keeps "Ravi" and "ravi" one memory; the newer casing/// wins the slot.public func rememberRecent(_ recents: [String], _ query: String) -> [String] {    let trimmed = query.trimmingCharacters(in: .whitespacesAndNewlines)    if trimmed.isEmpty { return recents }    let kept = recents.filter { $0.lowercased() != trimmed.lowercased() }    return Array(([trimmed] + kept).prefix(searchRecentsCap))}