28 lines
1.0 KiB
Swift
28 lines
1.0 KiB
Swift
import Foundation
|
|
|
|
// Encrypted account cache backed by UserDefaults (AES-256-GCM via CacheEncryption).
|
|
// Used by HomeViewModel to display cached data instantly on launch while a fresh
|
|
// network fetch runs in the background.
|
|
struct AccountCache {
|
|
static let shared = AccountCache()
|
|
private let udKey = "thijooree_accounts_v1"
|
|
private init() {}
|
|
|
|
func save(_ accounts: [BankAccount]) {
|
|
guard let json = try? JSONEncoder().encode(accounts),
|
|
let encrypted = try? CacheEncryption.shared.encryptData(json) else { return }
|
|
UserDefaults.standard.set(encrypted, forKey: udKey)
|
|
}
|
|
|
|
func load() -> [BankAccount] {
|
|
guard let encrypted = UserDefaults.standard.string(forKey: udKey),
|
|
let json = try? CacheEncryption.shared.decryptData(encrypted),
|
|
let accounts = try? JSONDecoder().decode([BankAccount].self, from: json) else { return [] }
|
|
return accounts
|
|
}
|
|
|
|
func clear() {
|
|
UserDefaults.standard.removeObject(forKey: udKey)
|
|
}
|
|
}
|