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
+26
View File
@@ -41,6 +41,27 @@ final class AppModel: ObservableObject {
var symbol: String { chain.symbol }
// MARK: - Cache
/// Restores the last-known state for the chain on screen instantly; the
/// refresh that follows replaces it with live data.
private func showCached(_ forChain: Chain) {
guard let cached = WalletStore.cachedState(forChain) else { return }
balance = cached.balance
transactions = cached.transactions
if let accounts = cached.accounts { moneroAccounts = accounts }
}
private func persistState(for forChain: Chain) {
guard (forChain == .xrp ? xrpWallet : xmrWallet) != nil else { return }
WalletStore.saveCachedState(
ChainCache(balance: balance,
transactions: transactions,
accounts: forChain == .xmr ? moneroAccounts : nil,
savedAt: Date()),
chain: forChain)
}
// MARK: - Bootstrap
func bootstrap() async {
@@ -57,6 +78,7 @@ final class AppModel: ObservableObject {
if xrpWallet != nil || moneroReady {
chain = xrpWallet != nil ? .xrp : .xmr
}
showCached(chain)
await refresh(force: true)
startAutoRefresh()
}
@@ -101,7 +123,9 @@ final class AppModel: ObservableObject {
statusMessage = nil
balance = Balance(total: "0", unlocked: "", funded: false)
transactions = []
moneroAccounts = []
syncStatus = nil
showCached(newChain)
if newChain == .xmr, let xmrWallet, !moneroOpened {
await openMonero(afterRestore: false, wallet: xmrWallet)
@@ -217,6 +241,7 @@ final class AppModel: ObservableObject {
guard !isStale(token, requestChain) else { return }
balance = fetchedBalance
transactions = fetchedTransactions
persistState(for: requestChain)
} catch {
guard !isStale(token, requestChain) else { return }
errorMessage = error.localizedDescription
@@ -251,6 +276,7 @@ final class AppModel: ObservableObject {
balance = fetchedBalance
transactions = fetchedTransactions
moneroAccounts = fetchedAccounts
persistState(for: requestChain)
}
} catch {
guard !isStale(token, requestChain) else { return }