From 41f137a1276d997e6513362447e82233c461d285 Mon Sep 17 00:00:00 2001 From: Sander van Grieken Date: Fri, 3 Feb 2023 11:43:37 +0100 Subject: [PATCH] qml: qewallet sync status and progress --- electrum/gui/qml/qewallet.py | 51 ++++++++++++++++++++---------------- 1 file changed, 29 insertions(+), 22 deletions(-) diff --git a/electrum/gui/qml/qewallet.py b/electrum/gui/qml/qewallet.py index 06e874633..563393d15 100644 --- a/electrum/gui/qml/qewallet.py +++ b/electrum/gui/qml/qewallet.py @@ -106,6 +106,11 @@ class QEWallet(AuthMixin, QObject, QtEventListener): self.notification_timer.setInterval(500) # msec self.notification_timer.timeout.connect(self.notify_transactions) + self.sync_progress_timer = QTimer(self) + self.sync_progress_timer.setSingleShot(False) + self.sync_progress_timer.setInterval(2000) + self.sync_progress_timer.timeout.connect(self.update_sync_progress) + # To avoid leaking references to "self" that prevent the # window from being GC-ed when closed, callbacks should be # methods of this class only, and specifically not be @@ -114,6 +119,8 @@ class QEWallet(AuthMixin, QObject, QtEventListener): self.register_callbacks() self.destroyed.connect(lambda: self.on_destroy()) + self.synchronizing = True # start in sync state + @pyqtProperty(bool, notify=isUptodateChanged) def isUptodate(self): return self._isUpToDate @@ -126,21 +133,29 @@ class QEWallet(AuthMixin, QObject, QtEventListener): @synchronizing.setter def synchronizing(self, synchronizing): if self._synchronizing != synchronizing: + self._logger.info(f'SYNC {self._synchronizing} -> {synchronizing}') self._synchronizing = synchronizing self.synchronizingChanged.emit() + if synchronizing: + if not self.sync_progress_timer.isActive(): + self.update_sync_progress() + self.sync_progress_timer.start() + else: + self.sync_progress_timer.stop() synchronizingProgressChanged = pyqtSignal() @pyqtProperty(str, notify=synchronizingProgressChanged) - def synchronizing_progress(self): + def synchronizingProgress(self): return self._synchronizing_progress - @synchronizing_progress.setter - def synchronizing_progress(self, progress): + @synchronizingProgress.setter + def synchronizingProgress(self, progress): if self._synchronizing_progress != progress: self._synchronizing_progress = progress + self._logger.info(progress) self.synchronizingProgressChanged.emit() - @event_listener + @qt_event_listener def on_event_status(self): self._logger.debug('status') uptodate = self.wallet.is_up_to_date() @@ -151,21 +166,6 @@ class QEWallet(AuthMixin, QObject, QtEventListener): if uptodate: self.historyModel.init_model() - if self.wallet.network.is_connected(): - server_height = self.wallet.network.get_server_height() - server_lag = self.wallet.network.get_local_height() - server_height - # Server height can be 0 after switching to a new server - # until we get a headers subscription request response. - # Display the synchronizing message in that case. - if not self._isUpToDate or server_height == 0: - num_sent, num_answered = self.wallet.adb.get_history_sync_state_details() - self.synchronizing_progress = ("{} ({}/{})" - .format(_("Synchronizing..."), num_answered, num_sent)) - self.synchronizing = True - else: - self.synchronizing_progress = '' - self.synchronizing = False - @qt_event_listener def on_event_request_status(self, wallet, key, status): if wallet == self.wallet: @@ -176,7 +176,7 @@ class QEWallet(AuthMixin, QObject, QtEventListener): # TODO: only update if it was paid over lightning, # and even then, we can probably just add the payment instead # of recreating the whole history (expensive) - self.historyModel.init_model() + self.historyModel.init_model(True) @event_listener def on_event_invoice_status(self, wallet, key, status): @@ -192,11 +192,12 @@ class QEWallet(AuthMixin, QObject, QtEventListener): self.addressModel.setDirty() self.historyModel.setDirty() # assuming wallet.is_up_to_date triggers after - @event_listener + @qt_event_listener def on_event_wallet_updated(self, wallet): if wallet == self.wallet: self._logger.debug('wallet %s updated' % str(wallet)) self.balanceChanged.emit() + self.synchronizing = not wallet.is_up_to_date() @event_listener def on_event_channel(self, wallet, channel): @@ -214,7 +215,7 @@ class QEWallet(AuthMixin, QObject, QtEventListener): def on_event_payment_succeeded(self, wallet, key): if wallet == self.wallet: self.paymentSucceeded.emit(key) - self.historyModel.init_model() # TODO: be less dramatic + self.historyModel.init_model(True) # TODO: be less dramatic @event_listener def on_event_payment_failed(self, wallet, key, reason): @@ -269,6 +270,12 @@ class QEWallet(AuthMixin, QObject, QtEventListener): self.userNotify.emit(self.wallet, _("New transaction: {}").format(config.format_amount_and_units(tx_wallet_delta.delta))) + def update_sync_progress(self): + if self.wallet.network.is_connected(): + num_sent, num_answered = self.wallet.adb.get_history_sync_state_details() + self.synchronizingProgress = \ + ("{} ({}/{})".format(_("Synchronizing..."), num_answered, num_sent)) + historyModelChanged = pyqtSignal() @pyqtProperty(QETransactionListModel, notify=historyModelChanged) def historyModel(self):