39 lines
1.3 KiB
Swift
39 lines
1.3 KiB
Swift
import Foundation
|
|
|
|
struct CachedBmlForeignLimits: Codable {
|
|
let userName: String
|
|
let limits: [BmlForeignLimit]
|
|
}
|
|
|
|
struct DashboardCachePayload: Codable {
|
|
let foreignLimits: [CachedBmlForeignLimits]
|
|
let mibFinancing: [MibFinanceDeal]
|
|
}
|
|
|
|
struct DashboardCache {
|
|
static let shared = DashboardCache()
|
|
private let udKey = "thijooree_dashboard_v1"
|
|
private init() {}
|
|
|
|
func save(foreignLimits: [(userName: String, limits: [BmlForeignLimit])], mibFinancing: [MibFinanceDeal]) {
|
|
let payload = DashboardCachePayload(
|
|
foreignLimits: foreignLimits.map { CachedBmlForeignLimits(userName: $0.userName, limits: $0.limits) },
|
|
mibFinancing: mibFinancing
|
|
)
|
|
guard let json = try? JSONEncoder().encode(payload),
|
|
let encrypted = try? CacheEncryption.shared.encryptData(json) else { return }
|
|
UserDefaults.standard.set(encrypted, forKey: udKey)
|
|
}
|
|
|
|
func load() -> DashboardCachePayload? {
|
|
guard let encrypted = UserDefaults.standard.string(forKey: udKey),
|
|
let json = try? CacheEncryption.shared.decryptData(encrypted),
|
|
let payload = try? JSONDecoder().decode(DashboardCachePayload.self, from: json) else { return nil }
|
|
return payload
|
|
}
|
|
|
|
func clear() {
|
|
UserDefaults.standard.removeObject(forKey: udKey)
|
|
}
|
|
}
|