import Foundation // Locally persisted contacts saved from transfer receipts. // Encrypted with AES-256-GCM via CacheEncryption, stored in UserDefaults. struct SavedContactsStore { static let shared = SavedContactsStore() private let udKey = "thijooree_saved_contacts_v1" private init() {} func save(_ contact: BankContact) { var contacts = load() // Replace existing entry for the same account+source, then prepend (most-recent-first) contacts.removeAll { $0.benefAccount == contact.benefAccount && $0.source == contact.source } contacts.insert(contact, at: 0) if contacts.count > 100 { contacts = Array(contacts.prefix(100)) } persist(contacts) } func load() -> [BankContact] { guard let encrypted = UserDefaults.standard.string(forKey: udKey), let json = try? CacheEncryption.shared.decryptData(encrypted), let contacts = try? JSONDecoder().decode([BankContact].self, from: json) else { return [] } return contacts } func delete(id: String) { var contacts = load() contacts.removeAll { $0.id == id } persist(contacts) } func clear() { UserDefaults.standard.removeObject(forKey: udKey) } private func persist(_ contacts: [BankContact]) { guard let json = try? JSONEncoder().encode(contacts), let encrypted = try? CacheEncryption.shared.encryptData(json) else { return } UserDefaults.standard.set(encrypted, forKey: udKey) } }