From da59860241b77cdf309eea1153ca726ef2d6db44 Mon Sep 17 00:00:00 2001 From: Rafael Saes Date: Fri, 24 May 2024 11:15:33 -0300 Subject: [PATCH] fix: connection switching back and forth --- cw_bitcoin/lib/electrum.dart | 10 +++++----- cw_bitcoin/lib/electrum_wallet.dart | 13 +++++++++---- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/cw_bitcoin/lib/electrum.dart b/cw_bitcoin/lib/electrum.dart index bcef89f43..464b56735 100644 --- a/cw_bitcoin/lib/electrum.dart +++ b/cw_bitcoin/lib/electrum.dart @@ -41,7 +41,7 @@ class ElectrumClient { bool get isConnected => _isConnected; Socket? socket; - void Function(bool)? onConnectionStatusChange; + void Function(bool?)? onConnectionStatusChange; int _id; final Map _tasks; Map get tasks => _tasks; @@ -91,7 +91,7 @@ class ElectrumClient { _setIsConnected(false); }, onDone: () { unterminatedString = ''; - _setIsConnected(false); + _setIsConnected(null); }); keepAlive(); } @@ -146,7 +146,7 @@ class ElectrumClient { await callWithTimeout(method: 'server.ping'); _setIsConnected(true); } on RequestFailedTimeoutException catch (_) { - _setIsConnected(false); + _setIsConnected(null); } } @@ -480,12 +480,12 @@ class ElectrumClient { } } - void _setIsConnected(bool isConnected) { + void _setIsConnected(bool? isConnected) { if (_isConnected != isConnected) { onConnectionStatusChange?.call(isConnected); } - _isConnected = isConnected; + _isConnected = isConnected ?? false; } void _handleResponse(Map response) { diff --git a/cw_bitcoin/lib/electrum_wallet.dart b/cw_bitcoin/lib/electrum_wallet.dart index e571738b9..bbe153de1 100644 --- a/cw_bitcoin/lib/electrum_wallet.dart +++ b/cw_bitcoin/lib/electrum_wallet.dart @@ -235,7 +235,10 @@ abstract class ElectrumWalletBase } Future getUpdatedChainTip() async { - _currentChainTip = await electrumClient.getCurrentBlockChainTip(); + final newTip = await electrumClient.getCurrentBlockChainTip(); + if (newTip != null && newTip > (_currentChainTip ?? 0)) { + _currentChainTip = newTip; + } return _currentChainTip ?? 0; } @@ -444,13 +447,15 @@ abstract class ElectrumWalletBase await electrumClient.close(); - electrumClient.onConnectionStatusChange = (bool isConnected) async { + electrumClient.onConnectionStatusChange = (bool? isConnected) async { if (syncStatus is SyncingSyncStatus) return; - if (isConnected && syncStatus is! SyncedSyncStatus) { + if (isConnected == true && syncStatus is! SyncedSyncStatus) { syncStatus = ConnectedSyncStatus(); - } else if (!isConnected) { + } else if (isConnected == false) { syncStatus = LostConnectionSyncStatus(); + } else if (!(isConnected ?? false) && syncStatus is! ConnectingSyncStatus) { + syncStatus = NotConnectedSyncStatus(); } };