diff --git a/lib/src/screens/connect_device/connect_device_page.dart b/lib/src/screens/connect_device/connect_device_page.dart index 5e94c78a4..5e52b887c 100644 --- a/lib/src/screens/connect_device/connect_device_page.dart +++ b/lib/src/screens/connect_device/connect_device_page.dart @@ -92,6 +92,7 @@ class ConnectDevicePageBodyState extends State { late StreamSubscription? _bleRefresh = null; bool longWait = false; + Timer? _longWaitTimer; @override void initState() { @@ -108,7 +109,7 @@ class ConnectDevicePageBodyState extends State { Timer.periodic(Duration(seconds: 1), (_) => _refreshUsbDevices()); } - Future.delayed(Duration(seconds: 10), () { + _longWaitTimer = Timer(Duration(seconds: 10), () { if (widget.ledgerVM.bleIsEnabled && bleDevices.isEmpty) setState(() => longWait = true); }); @@ -121,6 +122,7 @@ class ConnectDevicePageBodyState extends State { _bleStateTimer?.cancel(); _usbRefreshTimer?.cancel(); _bleRefresh?.cancel(); + _longWaitTimer?.cancel(); widget.ledgerVM.stopScanning(); super.dispose(); @@ -206,7 +208,8 @@ class ConnectDevicePageBodyState extends State { offstage: !longWait, child: Padding( padding: EdgeInsets.only(left: 20, right: 20, bottom: 20), - child: Text(S.of(context).if_you_dont_see_your_device, + child: Text( + S.of(context).if_you_dont_see_your_device, style: TextStyle( fontSize: 16, fontWeight: FontWeight.w500, @@ -235,7 +238,6 @@ class ConnectDevicePageBodyState extends State { ), ), ), - if (bleDevices.length > 0) ...[ Padding( padding: EdgeInsets.only(left: 20, right: 20, bottom: 20), @@ -277,7 +279,9 @@ class ConnectDevicePageBodyState extends State { style: TextStyle( fontSize: 14, fontWeight: FontWeight.w400, - color: Theme.of(context).extension()!.titleColor, + color: Theme.of(context) + .extension()! + .titleColor, ), ), ), @@ -299,8 +303,12 @@ class ConnectDevicePageBodyState extends State { if (widget.allowChangeWallet) ...[ PrimaryButton( text: S.of(context).wallets, - color: Theme.of(context).extension()!.createNewWalletButtonBackgroundColor, - textColor: Theme.of(context).extension()!.restoreWalletButtonTextColor, + color: Theme.of(context) + .extension()! + .createNewWalletButtonBackgroundColor, + textColor: Theme.of(context) + .extension()! + .restoreWalletButtonTextColor, onPressed: _onChangeWallet, ) ], diff --git a/lib/view_model/hardware_wallet/ledger_view_model.dart b/lib/view_model/hardware_wallet/ledger_view_model.dart index b48f641a2..4c084c778 100644 --- a/lib/view_model/hardware_wallet/ledger_view_model.dart +++ b/lib/view_model/hardware_wallet/ledger_view_model.dart @@ -99,47 +99,54 @@ abstract class LedgerViewModelBase with Store { } Future connectLedger(sdk.LedgerDevice device, WalletType type) async { + _isConnecting = true; + _connectingWalletType = type; if (isConnected) { try { - await _connectionChangeListener?.cancel(); - _connectionChangeListener = null; await _connection!.disconnect().catchError((_) {}); } catch (_) {} } + final ledger = device.connectionType == sdk.ConnectionType.ble ? ledgerPlusBLE : ledgerPlusUSB; - - if (_connectionChangeListener == null) { - _connectionChangeListener = ledger.deviceStateChanges.listen((event) { - printV('Ledger Device State Changed: $event'); - if (event == sdk.BleConnectionState.disconnected) { - _connection = null; - if (type == WalletType.monero) { - monero!.resetLedgerConnection(); - - Navigator.of( navigatorKey.currentContext!).pushNamed( - Routes.connectDevices, - arguments: ConnectDevicePageParams( - walletType: WalletType.monero, - allowChangeWallet: true, - isReconnect: true, - onConnectDevice: (context, ledgerVM) async { - Navigator.of(context).pop(); - }, - ), - ); - } - } - }); + if (_connectionChangeSubscription == null) { + _connectionChangeSubscription = ledger.deviceStateChanges + .listen(_connectionChangeListener); } _connection = await ledger.connect(device); + _isConnecting = false; } - StreamSubscription? _connectionChangeListener; + StreamSubscription? _connectionChangeSubscription; sdk.LedgerConnection? _connection; + bool _isConnecting = true; + WalletType? _connectingWalletType; + + void _connectionChangeListener( + sdk.BleConnectionState event, ) { + printV('Ledger Device State Changed: $event'); + if (event == sdk.BleConnectionState.disconnected && !_isConnecting) { + _connection = null; + if (_connectingWalletType == WalletType.monero) { + monero!.resetLedgerConnection(); + + Navigator.of(navigatorKey.currentContext!).pushNamed( + Routes.connectDevices, + arguments: ConnectDevicePageParams( + walletType: WalletType.monero, + allowChangeWallet: true, + isReconnect: true, + onConnectDevice: (context, ledgerVM) async { + Navigator.of(context).pop(); + }, + ), + ); + } + } + } bool get isConnected => _connection != null && !(_connection!.isDisconnected);