Cache wallet balance and activity so screens show it instantly

This commit is contained in:
2026-09-25 21:58:34 +08:00
parent 2728bf161e
commit b989b0f791
3 changed files with 55 additions and 5 deletions
+15
View File
@@ -63,9 +63,24 @@ enum WalletStore {
static func removeWallet(_ chain: Chain) {
defaults.removeObject(forKey: walletKey(chain))
defaults.removeObject(forKey: cacheKey(chain))
Keychain.remove(secretKey(chain))
}
// MARK: - Cached state (balance, transactions, accounts)
private static func cacheKey(_ chain: Chain) -> String { "koins.cache.\(chain.rawValue)" }
static func cachedState(_ chain: Chain) -> ChainCache? {
guard let data = defaults.data(forKey: cacheKey(chain)) else { return nil }
return try? JSONDecoder().decode(ChainCache.self, from: data)
}
static func saveCachedState(_ state: ChainCache, chain: Chain) {
guard let data = try? JSONEncoder().encode(state) else { return }
defaults.set(data, forKey: cacheKey(chain))
}
static func secret(_ chain: Chain) -> String? {
Keychain.get(secretKey(chain))
}