Skip to content

Commit 303aeb4

Browse files
committed
ui: load previous transaction state when app restart
1 parent 2fb7143 commit 303aeb4

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

BDKSwiftExampleWallet/View Model/Activity/ActivityListViewModel.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,19 @@ class ActivityListViewModel {
4646
self.bdkClient = bdkClient
4747
self.transactions = transactions
4848
self.walletSyncState = walletSyncState
49+
50+
// Preload cached data synchronously so UI has content before first render
51+
// transactions + listUnspent items are available from the persisted wallet db
52+
if self.transactions.isEmpty {
53+
if let cached = try? bdkClient.transactions() {
54+
self.transactions = cached
55+
}
56+
}
57+
if self.localOutputs.isEmpty {
58+
if let cachedUtxos = try? bdkClient.listUnspent() {
59+
self.localOutputs = cachedUtxos
60+
}
61+
}
4962
}
5063

5164
func getTransactions() {

BDKSwiftExampleWallet/View/WalletView.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,11 @@ struct WalletView: View {
110110
),
111111
perform: { _ in
112112
Task {
113+
// Show cached state first
114+
viewModel.getBalance()
115+
viewModel.getTransactions()
116+
117+
// Then sync and refresh
113118
await viewModel.syncOrFullScan()
114119
viewModel.getBalance()
115120
viewModel.getTransactions()
@@ -119,13 +124,14 @@ struct WalletView: View {
119124
)
120125
.task {
121126
viewModel.getBalance()
127+
viewModel.getTransactions()
122128
if isFirstAppear || newTransactionSent {
123129
await viewModel.syncOrFullScan()
124130
isFirstAppear = false
125131
newTransactionSent = false
126132
viewModel.getBalance()
133+
viewModel.getTransactions()
127134
}
128-
viewModel.getTransactions()
129135
await viewModel.getPrices()
130136
}
131137
.onAppear {

0 commit comments

Comments
 (0)