From 91dd677e287b2d2000f1ed4aef6dfe006b21228f Mon Sep 17 00:00:00 2001 From: julian Date: Mon, 5 Jun 2023 15:04:11 -0600 Subject: [PATCH] fix: banano network pinging, clean up imports, move file to separate folder --- .../coins/{nano => banano}/banano_wallet.dart | 75 ++++++++++++++++--- lib/services/coins/coin_service.dart | 8 +- 2 files changed, 69 insertions(+), 14 deletions(-) rename lib/services/coins/{nano => banano}/banano_wallet.dart (92%) diff --git a/lib/services/coins/nano/banano_wallet.dart b/lib/services/coins/banano/banano_wallet.dart similarity index 92% rename from lib/services/coins/nano/banano_wallet.dart rename to lib/services/coins/banano/banano_wallet.dart index 4ab0e7b1c..23c219b00 100644 --- a/lib/services/coins/nano/banano_wallet.dart +++ b/lib/services/coins/banano/banano_wallet.dart @@ -4,28 +4,29 @@ import 'dart:convert'; import 'package:http/http.dart' as http; import 'package:isar/isar.dart'; import 'package:nanodart/nanodart.dart'; +import 'package:stackwallet/db/isar/main_db.dart'; import 'package:stackwallet/models/balance.dart'; import 'package:stackwallet/models/isar/models/isar_models.dart'; +import 'package:stackwallet/models/node_model.dart'; import 'package:stackwallet/models/paymint/fee_object_model.dart'; import 'package:stackwallet/services/coins/coin_service.dart'; +import 'package:stackwallet/services/event_bus/events/global/node_connection_status_changed_event.dart'; import 'package:stackwallet/services/event_bus/events/global/wallet_sync_status_changed_event.dart'; import 'package:stackwallet/services/event_bus/global_event_bus.dart'; import 'package:stackwallet/services/mixins/coin_control_interface.dart'; import 'package:stackwallet/services/mixins/wallet_cache.dart'; import 'package:stackwallet/services/mixins/wallet_db.dart'; +import 'package:stackwallet/services/node_service.dart'; +import 'package:stackwallet/services/transaction_notification_tracker.dart'; import 'package:stackwallet/utilities/amount/amount.dart'; +import 'package:stackwallet/utilities/constants.dart'; +import 'package:stackwallet/utilities/default_nodes.dart'; import 'package:stackwallet/utilities/enums/coin_enum.dart'; +import 'package:stackwallet/utilities/flutter_secure_storage_interface.dart'; import 'package:stackwallet/utilities/logger.dart'; +import 'package:stackwallet/utilities/prefs.dart'; import 'package:tuple/tuple.dart'; -import '../../../db/isar/main_db.dart'; -import '../../../models/node_model.dart'; -import '../../../utilities/default_nodes.dart'; -import '../../../utilities/flutter_secure_storage_interface.dart'; -import '../../../utilities/prefs.dart'; -import '../../node_service.dart'; -import '../../transaction_notification_tracker.dart'; - const int MINIMUM_CONFIRMATIONS = 1; const String DEFAULT_REPRESENTATIVE = "ban_1ka1ium4pfue3uxtntqsrib8mumxgazsjf58gidh1xeo5te3whsq8z476goo"; @@ -118,13 +119,26 @@ class BananoWallet extends CoinServiceAPI late final TransactionNotificationTracker txTracker; final _prefs = Prefs.instance; + Timer? timer; bool _shouldAutoSync = false; @override bool get shouldAutoSync => _shouldAutoSync; @override - set shouldAutoSync(bool shouldAutoSync) => _shouldAutoSync = shouldAutoSync; + set shouldAutoSync(bool shouldAutoSync) { + if (_shouldAutoSync != shouldAutoSync) { + _shouldAutoSync = shouldAutoSync; + if (!shouldAutoSync) { + timer?.cancel(); + timer = null; + stopNetworkAlivePinging(); + } else { + startNetworkAlivePinging(); + refresh(); + } + } + } @override Balance get balance => _balance ??= getCachedBalance(); @@ -753,6 +767,49 @@ class BananoWallet extends CoinServiceAPI return Future.value(false); } + Timer? _networkAliveTimer; + + void startNetworkAlivePinging() { + // call once on start right away + _periodicPingCheck(); + + // then periodically check + _networkAliveTimer = Timer.periodic( + Constants.networkAliveTimerDuration, + (_) async { + _periodicPingCheck(); + }, + ); + } + + void _periodicPingCheck() async { + bool hasNetwork = await testNetworkConnection(); + + if (_isConnected != hasNetwork) { + NodeConnectionStatus status = hasNetwork + ? NodeConnectionStatus.connected + : NodeConnectionStatus.disconnected; + + GlobalEventBus.instance.fire( + NodeConnectionStatusChangedEvent( + status, + walletId, + coin, + ), + ); + + _isConnected = hasNetwork; + if (hasNetwork) { + unawaited(refresh()); + } + } + } + + void stopNetworkAlivePinging() { + _networkAliveTimer?.cancel(); + _networkAliveTimer = null; + } + @override Future> get transactions => db.getTransactions(walletId).findAll(); diff --git a/lib/services/coins/coin_service.dart b/lib/services/coins/coin_service.dart index 01affd58b..00a52eba5 100644 --- a/lib/services/coins/coin_service.dart +++ b/lib/services/coins/coin_service.dart @@ -14,6 +14,7 @@ import 'package:stackwallet/models/balance.dart'; import 'package:stackwallet/models/isar/models/isar_models.dart' as isar_models; import 'package:stackwallet/models/node_model.dart'; import 'package:stackwallet/models/paymint/fee_object_model.dart'; +import 'package:stackwallet/services/coins/banano/banano_wallet.dart'; import 'package:stackwallet/services/coins/bitcoin/bitcoin_wallet.dart'; import 'package:stackwallet/services/coins/bitcoincash/bitcoincash_wallet.dart'; import 'package:stackwallet/services/coins/dogecoin/dogecoin_wallet.dart'; @@ -24,7 +25,6 @@ import 'package:stackwallet/services/coins/firo/firo_wallet.dart'; import 'package:stackwallet/services/coins/litecoin/litecoin_wallet.dart'; import 'package:stackwallet/services/coins/monero/monero_wallet.dart'; import 'package:stackwallet/services/coins/namecoin/namecoin_wallet.dart'; -import 'package:stackwallet/services/coins/nano/banano_wallet.dart'; import 'package:stackwallet/services/coins/nano/nano_wallet.dart'; import 'package:stackwallet/services/coins/particl/particl_wallet.dart'; import 'package:stackwallet/services/coins/wownero/wownero_wallet.dart'; @@ -244,8 +244,7 @@ abstract class CoinServiceAPI { walletName: walletName, coin: coin, tracker: tracker, - secureStore: secureStorageInterface - ); + secureStore: secureStorageInterface); case Coin.banano: return BananoWallet( @@ -253,8 +252,7 @@ abstract class CoinServiceAPI { walletName: walletName, coin: coin, tracker: tracker, - secureStore: secureStorageInterface - ); + secureStore: secureStorageInterface); case Coin.dogecoinTestNet: return DogecoinWallet(