Skip to content

Commit aa0d291

Browse files
authored
Update balance and tx history when new tx is registered. Clean up listener structure between parent and subpage (#733)
* update balance and transaction list when new tx/block is registered * update outdated version on workflow * Fix search and settings (#736) * fix search editor error cannot be entered on PC * (settings) Fix warning text display delay when selecting exchange * display a warning when the exchange settings have been changed automatically because the rate is not getted * update outdated version on workflow
1 parent 758a768 commit aa0d291

7 files changed

Lines changed: 83 additions & 35 deletions

File tree

libwallet/assets_manager.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1060,14 +1060,15 @@ func (mgr *AssetsManager) DeleteDEXData() error {
10601060
return os.Remove(dexDBFile)
10611061
}
10621062

1063-
func (mgr *AssetsManager) WatchBalanceChange(listen func()) {
1063+
// Listen when new tx is registered
1064+
func (mgr *AssetsManager) ListenForTxAndBlockNotification(listen func(int)) {
10641065
// Reload total balance on new tx.
10651066
txAndBlockNotificationListener := &sharedW.TxAndBlockNotificationListener{
1066-
OnTransactionConfirmed: func(_ int, _ string, _ int32) {
1067-
listen()
1067+
OnTransactionConfirmed: func(walletID int, _ string, _ int32) {
1068+
listen(walletID)
10681069
},
1069-
OnTransaction: func(_ int, _ *sharedW.Transaction) {
1070-
listen()
1070+
OnTransaction: func(walletID int, _ *sharedW.Transaction) {
1071+
listen(walletID)
10711072
},
10721073
}
10731074

@@ -1079,8 +1080,10 @@ func (mgr *AssetsManager) WatchBalanceChange(listen func()) {
10791080
}
10801081
}
10811082
}
1083+
}
10821084

1083-
// add rate listener
1085+
// Listen when rate changes
1086+
func (mgr *AssetsManager) ListenForRate(listen func()) {
10841087
rateListener := &ext.RateListener{
10851088
OnRateUpdated: func() {
10861089
listen()

ui/page/components/items_scroll.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,20 +83,27 @@ func (s *Scroll[T]) SetIsHaveKeySearch(isHaveKeySearch bool) {
8383
// FetchScrollData is a mutex protected fetchScrollData function. At the end of
8484
// the function call a window reload is triggered. Returns that latest records.
8585
func (s *Scroll[T]) FetchScrollData(isScrollUp bool, window app.WindowNavigator, isResetList bool) {
86+
s.FetchScrollDataHandler(isScrollUp, window, isResetList, false)
87+
}
88+
89+
// isResetList = false and loadNewItem = true: Reloads the item list but does not alter the current scroll position
90+
func (s *Scroll[T]) FetchScrollDataHandler(isScrollUp bool, window app.WindowNavigator, isResetList, loadNewItem bool) {
8691
s.mu.Lock()
8792
// s.data is not nil when moving from details page to list page.
8893
if s.data != nil {
8994
s.isLoadingItems = false
9095
}
9196

92-
if isResetList {
97+
if isResetList || loadNewItem {
9398
s.loadedAllItems = false
9499
s.isLoadingItems = false
95100
s.offset = 0
96101
s.itemsCount = -1
97102
s.data = nil
98103
s.cacheData = nil
99-
s.list.Position.Offset = 0
104+
if isResetList {
105+
s.list.Position.Offset = 0
106+
}
100107
}
101108
s.mu.Unlock()
102109
if s.data == nil || len(s.data.items) == 0 {

ui/page/info/info_page.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,14 @@ func (pg *WalletInfo) reloadMixerBalances() {
319319
}
320320
}
321321

322+
// Reload tx list when there is new tx. Called from parent page
323+
func (pg *WalletInfo) ListenForNewTx(walletID int) {
324+
if walletID != pg.wallet.GetWalletID() {
325+
return
326+
}
327+
pg.loadTransactions()
328+
}
329+
322330
func (pg *WalletInfo) loadTransactions() {
323331
pg.showMaterialLoader = true
324332
mapInfo, _ := components.TxPageDropDownFields(pg.wallet.GetAssetType(), 0)

ui/page/root/home_page.go

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,12 +212,33 @@ func (hp *HomePage) OnNavigatedTo() {
212212
if hp.isUpdateAPIAllowed() {
213213
go hp.checkForUpdates()
214214
}
215-
216-
hp.AssetsManager.WatchBalanceChange(func() {
215+
// When the new tx has been registered
216+
hp.AssetsManager.ListenForTxAndBlockNotification(func(walletID int) {
217+
go hp.CalculateAssetsUSDBalance()
218+
go hp.UpdateSubpageWhenHasNewTx(walletID)
219+
})
220+
// When rate change
221+
hp.AssetsManager.ListenForRate(func() {
217222
go hp.CalculateAssetsUSDBalance()
218223
})
219224
}
220225

226+
// Call the update function for subpages when there is a new tx
227+
func (hp *HomePage) UpdateSubpageWhenHasNewTx(walletID int) {
228+
switch hp.CurrentPageID() {
229+
// if overview page
230+
case OverviewPageID:
231+
hp.CurrentPage().(*OverviewPage).ListenForNewTx()
232+
return
233+
// if transactions history page
234+
case transaction.TransactionsPageID:
235+
hp.CurrentPage().(*transaction.TransactionsPage).ListenForTxNotification(walletID)
236+
return
237+
default:
238+
return
239+
}
240+
}
241+
221242
// initDEX initializes a new dex client if dex is not ready. If a dex client has
222243
// never been created before, initDEX will return early and do nothing.
223244
func (hp *HomePage) initDEX() {

ui/page/root/overview_page.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1122,6 +1122,13 @@ func (pg *OverviewPage) txAndWallet(mtx *multiWalletTx) (*sharedW.Transaction, s
11221122
return mtx.Transaction, pg.AssetsManager.WalletWithID(mtx.walletID)
11231123
}
11241124

1125+
// Update balance/USD balance and transaction list when there is a new tx
1126+
func (pg *OverviewPage) ListenForNewTx() {
1127+
pg.loadTransactions()
1128+
pg.updateAssetsSliders()
1129+
pg.updateAssetsUSDBalance()
1130+
}
1131+
11251132
func (pg *OverviewPage) updateAssetsUSDBalance() {
11261133
if pg.AssetsManager.ExchangeRateFetchingEnabled() {
11271134
assetsTotalUSDBalance, err := pg.AssetsManager.CalculateAssetsUSDBalance(pg.assetsTotalBalance)

ui/page/transaction/transactions_page.go

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,6 @@ func (pg *TransactionsPage) DisableUniformTab() {
180180
func (pg *TransactionsPage) OnNavigatedTo() {
181181
pg.refreshAvailableTxType()
182182

183-
pg.listenForTxNotifications() // tx ntfn listener is stopped in OnNavigatedFrom().
184183
go pg.scroll.FetchScrollData(false, pg.ParentWindow(), false)
185184
}
186185

@@ -750,28 +749,12 @@ func exportTxs(assets []sharedW.Asset, fileName string) error {
750749
return nil
751750
}
752751

753-
func (pg *TransactionsPage) listenForTxNotifications() {
754-
txAndBlockNotificationListener := &sharedW.TxAndBlockNotificationListener{
755-
OnTransaction: func(walletID int, _ *sharedW.Transaction) {
756-
// Listen for all new txs but ignore ntfns if the wallet sending the
757-
// ntfn is not the currently selected wallet.
758-
if pg.selectedWallet != nil && pg.selectedWallet.GetWalletID() != walletID {
759-
return // ignore tx
760-
}
761-
762-
pg.scroll.FetchScrollData(false, pg.ParentWindow(), false)
763-
},
764-
}
765-
766-
// Listen for ntfns for all wallets.
767-
for _, w := range pg.assetWallets {
768-
w.RemoveTxAndBlockNotificationListener(TransactionsPageID)
769-
err := w.AddTxAndBlockNotificationListener(txAndBlockNotificationListener, TransactionsPageID)
770-
if err != nil {
771-
log.Errorf("Error adding tx and block notification listener: %v", err)
772-
return
773-
}
752+
// Update transaction list when there is new tx or new confirmed status
753+
func (pg *TransactionsPage) ListenForTxNotification(walletID int) {
754+
if pg.selectedWallet != nil && pg.selectedWallet.GetWalletID() != walletID {
755+
return
774756
}
757+
pg.scroll.FetchScrollDataHandler(false, pg.ParentWindow(), false, true)
775758
}
776759

777760
func (pg *TransactionsPage) stopTxNotificationsListener() {

ui/page/wallet/single_wallet_main_page.go

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,9 @@ func (swmp *SingleWalletMasterPage) OnNavigatedTo() {
187187
// set active tab value
188188
swmp.activeTab[swmp.PageNavigationTab.SelectedSegment()] = swmp.CurrentPageID()
189189

190-
swmp.listenForNotifications() // ntfn listeners are stopped in OnNavigatedFrom().
190+
swmp.listenForNotifications(func(walletID int) {
191+
go swmp.ListenNewTxForSubPage(walletID)
192+
}) // ntfn listeners are stopped in OnNavigatedFrom().
191193

192194
if swmp.selectedWallet.GetAssetType() == libutils.DCRWalletAsset {
193195
if swmp.selectedWallet.ReadBoolConfigValueForKey(sharedW.FetchProposalConfigKey, false) && swmp.isGovernanceAPIAllowed() {
@@ -201,6 +203,19 @@ func (swmp *SingleWalletMasterPage) OnNavigatedTo() {
201203
}
202204
}
203205

206+
// Call the subpage component update functions when there is a new tx
207+
func (swmp *SingleWalletMasterPage) ListenNewTxForSubPage(walletID int) {
208+
switch swmp.CurrentPageID() {
209+
case transaction.TransactionsPageID:
210+
swmp.CurrentPage().(*transaction.TransactionsPage).ListenForTxNotification(walletID)
211+
return
212+
case info.InfoID:
213+
swmp.CurrentPage().(*info.WalletInfo).ListenForNewTx(walletID)
214+
default:
215+
return
216+
}
217+
}
218+
204219
// initTabOptions initializes the page navigation tabs
205220
func (swmp *SingleWalletMasterPage) initTabOptions() {
206221
commonTabs := []string{
@@ -764,7 +779,7 @@ func initializeBeepNotification(n string) {
764779

765780
// listenForNotifications starts a goroutine to watch for notifications
766781
// and update the UI accordingly.
767-
func (swmp *SingleWalletMasterPage) listenForNotifications() {
782+
func (swmp *SingleWalletMasterPage) listenForNotifications(listenForSubpage func(int)) {
768783
syncProgressListener := &sharedW.SyncProgressListener{
769784
OnSyncCompleted: func() {
770785
swmp.updateBalance()
@@ -778,7 +793,7 @@ func (swmp *SingleWalletMasterPage) listenForNotifications() {
778793
}
779794

780795
txAndBlockNotificationListener := &sharedW.TxAndBlockNotificationListener{
781-
OnTransaction: func(_ int, transaction *sharedW.Transaction) {
796+
OnTransaction: func(walletID int, transaction *sharedW.Transaction) {
782797
swmp.updateBalance()
783798
if swmp.AssetsManager.IsTransactionNotificationsOn() {
784799
// TODO: SPV wallets only receive mempool tx ntfn for txs that
@@ -788,6 +803,10 @@ func (swmp *SingleWalletMasterPage) listenForNotifications() {
788803
swmp.postTransactionNotification(transaction)
789804
}
790805
swmp.ParentWindow().Reload()
806+
listenForSubpage(walletID)
807+
},
808+
OnTransactionConfirmed: func(walletID int, _ string, _ int32) {
809+
listenForSubpage(walletID)
791810
},
792811
// OnBlockAttached is also called whenever OnTransactionConfirmed is
793812
// called, so use OnBlockAttached. Also, OnTransactionConfirmed may be

0 commit comments

Comments
 (0)