More Ledger Monero Fixes (#1888)
Some checks are pending
Cache Dependencies / test (push) Waiting to run

* More Ledger Monero Fixes

* Minor fixes
This commit is contained in:
Konstantin Ullrich 2024-12-17 19:57:57 +01:00 committed by GitHub
parent b1751f1fd6
commit 77c4eaaf4f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 47 additions and 32 deletions

View file

@ -92,6 +92,7 @@ class ConnectDevicePageBodyState extends State<ConnectDevicePageBody> {
late StreamSubscription<LedgerDevice>? _bleRefresh = null; late StreamSubscription<LedgerDevice>? _bleRefresh = null;
bool longWait = false; bool longWait = false;
Timer? _longWaitTimer;
@override @override
void initState() { void initState() {
@ -108,7 +109,7 @@ class ConnectDevicePageBodyState extends State<ConnectDevicePageBody> {
Timer.periodic(Duration(seconds: 1), (_) => _refreshUsbDevices()); Timer.periodic(Duration(seconds: 1), (_) => _refreshUsbDevices());
} }
Future.delayed(Duration(seconds: 10), () { _longWaitTimer = Timer(Duration(seconds: 10), () {
if (widget.ledgerVM.bleIsEnabled && bleDevices.isEmpty) if (widget.ledgerVM.bleIsEnabled && bleDevices.isEmpty)
setState(() => longWait = true); setState(() => longWait = true);
}); });
@ -121,6 +122,7 @@ class ConnectDevicePageBodyState extends State<ConnectDevicePageBody> {
_bleStateTimer?.cancel(); _bleStateTimer?.cancel();
_usbRefreshTimer?.cancel(); _usbRefreshTimer?.cancel();
_bleRefresh?.cancel(); _bleRefresh?.cancel();
_longWaitTimer?.cancel();
widget.ledgerVM.stopScanning(); widget.ledgerVM.stopScanning();
super.dispose(); super.dispose();
@ -206,7 +208,8 @@ class ConnectDevicePageBodyState extends State<ConnectDevicePageBody> {
offstage: !longWait, offstage: !longWait,
child: Padding( child: Padding(
padding: EdgeInsets.only(left: 20, right: 20, bottom: 20), 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( style: TextStyle(
fontSize: 16, fontSize: 16,
fontWeight: FontWeight.w500, fontWeight: FontWeight.w500,
@ -235,7 +238,6 @@ class ConnectDevicePageBodyState extends State<ConnectDevicePageBody> {
), ),
), ),
), ),
if (bleDevices.length > 0) ...[ if (bleDevices.length > 0) ...[
Padding( Padding(
padding: EdgeInsets.only(left: 20, right: 20, bottom: 20), padding: EdgeInsets.only(left: 20, right: 20, bottom: 20),
@ -277,7 +279,9 @@ class ConnectDevicePageBodyState extends State<ConnectDevicePageBody> {
style: TextStyle( style: TextStyle(
fontSize: 14, fontSize: 14,
fontWeight: FontWeight.w400, fontWeight: FontWeight.w400,
color: Theme.of(context).extension<CakeTextTheme>()!.titleColor, color: Theme.of(context)
.extension<CakeTextTheme>()!
.titleColor,
), ),
), ),
), ),
@ -299,8 +303,12 @@ class ConnectDevicePageBodyState extends State<ConnectDevicePageBody> {
if (widget.allowChangeWallet) ...[ if (widget.allowChangeWallet) ...[
PrimaryButton( PrimaryButton(
text: S.of(context).wallets, text: S.of(context).wallets,
color: Theme.of(context).extension<WalletListTheme>()!.createNewWalletButtonBackgroundColor, color: Theme.of(context)
textColor: Theme.of(context).extension<WalletListTheme>()!.restoreWalletButtonTextColor, .extension<WalletListTheme>()!
.createNewWalletButtonBackgroundColor,
textColor: Theme.of(context)
.extension<WalletListTheme>()!
.restoreWalletButtonTextColor,
onPressed: _onChangeWallet, onPressed: _onChangeWallet,
) )
], ],

View file

@ -99,47 +99,54 @@ abstract class LedgerViewModelBase with Store {
} }
Future<void> connectLedger(sdk.LedgerDevice device, WalletType type) async { Future<void> connectLedger(sdk.LedgerDevice device, WalletType type) async {
_isConnecting = true;
_connectingWalletType = type;
if (isConnected) { if (isConnected) {
try { try {
await _connectionChangeListener?.cancel();
_connectionChangeListener = null;
await _connection!.disconnect().catchError((_) {}); await _connection!.disconnect().catchError((_) {});
} catch (_) {} } catch (_) {}
} }
final ledger = device.connectionType == sdk.ConnectionType.ble final ledger = device.connectionType == sdk.ConnectionType.ble
? ledgerPlusBLE ? ledgerPlusBLE
: ledgerPlusUSB; : ledgerPlusUSB;
if (_connectionChangeSubscription == null) {
if (_connectionChangeListener == null) { _connectionChangeSubscription = ledger.deviceStateChanges
_connectionChangeListener = ledger.deviceStateChanges.listen((event) { .listen(_connectionChangeListener);
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();
},
),
);
}
}
});
} }
_connection = await ledger.connect(device); _connection = await ledger.connect(device);
_isConnecting = false;
} }
StreamSubscription<sdk.BleConnectionState>? _connectionChangeListener; StreamSubscription<sdk.BleConnectionState>? _connectionChangeSubscription;
sdk.LedgerConnection? _connection; 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); bool get isConnected => _connection != null && !(_connection!.isDisconnected);