Skip to content

Commit

Permalink
More Ledger Monero Fixes (#1888)
Browse files Browse the repository at this point in the history
* More Ledger Monero Fixes

* Minor fixes
  • Loading branch information
konstantinullrich authored Dec 17, 2024
1 parent b1751f1 commit 77c4eaa
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 32 deletions.
20 changes: 14 additions & 6 deletions lib/src/screens/connect_device/connect_device_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ class ConnectDevicePageBodyState extends State<ConnectDevicePageBody> {
late StreamSubscription<LedgerDevice>? _bleRefresh = null;

bool longWait = false;
Timer? _longWaitTimer;

@override
void initState() {
Expand All @@ -108,7 +109,7 @@ class ConnectDevicePageBodyState extends State<ConnectDevicePageBody> {
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);
});
Expand All @@ -121,6 +122,7 @@ class ConnectDevicePageBodyState extends State<ConnectDevicePageBody> {
_bleStateTimer?.cancel();
_usbRefreshTimer?.cancel();
_bleRefresh?.cancel();
_longWaitTimer?.cancel();

widget.ledgerVM.stopScanning();
super.dispose();
Expand Down Expand Up @@ -206,7 +208,8 @@ class ConnectDevicePageBodyState extends State<ConnectDevicePageBody> {
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,
Expand Down Expand Up @@ -235,7 +238,6 @@ class ConnectDevicePageBodyState extends State<ConnectDevicePageBody> {
),
),
),

if (bleDevices.length > 0) ...[
Padding(
padding: EdgeInsets.only(left: 20, right: 20, bottom: 20),
Expand Down Expand Up @@ -277,7 +279,9 @@ class ConnectDevicePageBodyState extends State<ConnectDevicePageBody> {
style: TextStyle(
fontSize: 14,
fontWeight: FontWeight.w400,
color: Theme.of(context).extension<CakeTextTheme>()!.titleColor,
color: Theme.of(context)
.extension<CakeTextTheme>()!
.titleColor,
),
),
),
Expand All @@ -299,8 +303,12 @@ class ConnectDevicePageBodyState extends State<ConnectDevicePageBody> {
if (widget.allowChangeWallet) ...[
PrimaryButton(
text: S.of(context).wallets,
color: Theme.of(context).extension<WalletListTheme>()!.createNewWalletButtonBackgroundColor,
textColor: Theme.of(context).extension<WalletListTheme>()!.restoreWalletButtonTextColor,
color: Theme.of(context)
.extension<WalletListTheme>()!
.createNewWalletButtonBackgroundColor,
textColor: Theme.of(context)
.extension<WalletListTheme>()!
.restoreWalletButtonTextColor,
onPressed: _onChangeWallet,
)
],
Expand Down
59 changes: 33 additions & 26 deletions lib/view_model/hardware_wallet/ledger_view_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -99,47 +99,54 @@ abstract class LedgerViewModelBase with Store {
}

Future<void> 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<sdk.BleConnectionState>? _connectionChangeListener;
StreamSubscription<sdk.BleConnectionState>? _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);

Expand Down

0 comments on commit 77c4eaa

Please sign in to comment.