More Ledger Monero Fixes (#1888)
* More Ledger Monero Fixes * Minor fixes
Konstantin Ullrich committed
Dec 17, 2024 at 19:57 UTC
77c4eaaf4f748abcbfe3821f4c3d0ed6626829f0
2 files changed
+47
-32
lib/src/screens/connect_device/connect_device_page.dart
+14
-6
@@ -92,6 +92,7 @@ class ConnectDevicePageBodyState extends State<ConnectDevicePageBody> {
92
late StreamSubscription<LedgerDevice>? _bleRefresh = null;
93
94
bool longWait = false;
95
+ Timer? _longWaitTimer;
96
97
@override
98
void initState() {
@@ -108,7 +109,7 @@ class ConnectDevicePageBodyState extends State<ConnectDevicePageBody> {
109
Timer.periodic(Duration(seconds: 1), (_) => _refreshUsbDevices());
110
}
111
111
- Future.delayed(Duration(seconds: 10), () {
112
+ _longWaitTimer = Timer(Duration(seconds: 10), () {
113
if (widget.ledgerVM.bleIsEnabled && bleDevices.isEmpty)
114
setState(() => longWait = true);
115
});
@@ -121,6 +122,7 @@ class ConnectDevicePageBodyState extends State<ConnectDevicePageBody> {
122
_bleStateTimer?.cancel();
123
_usbRefreshTimer?.cancel();
124
_bleRefresh?.cancel();
125
+ _longWaitTimer?.cancel();
126
127
widget.ledgerVM.stopScanning();
128
super.dispose();
@@ -206,7 +208,8 @@ class ConnectDevicePageBodyState extends State<ConnectDevicePageBody> {
208
offstage: !longWait,
209
child: Padding(
210
padding: EdgeInsets.only(left: 20, right: 20, bottom: 20),
209
- child: Text(S.of(context).if_you_dont_see_your_device,
211
+ child: Text(
212
+ S.of(context).if_you_dont_see_your_device,
213
style: TextStyle(
214
fontSize: 16,
215
fontWeight: FontWeight.w500,
@@ -235,7 +238,6 @@ class ConnectDevicePageBodyState extends State<ConnectDevicePageBody> {
238
),
239
),
240
),
238
-
241
if (bleDevices.length > 0) ...[
242
Padding(
243
padding: EdgeInsets.only(left: 20, right: 20, bottom: 20),
@@ -277,7 +279,9 @@ class ConnectDevicePageBodyState extends State<ConnectDevicePageBody> {
279
style: TextStyle(
280
fontSize: 14,
281
fontWeight: FontWeight.w400,
280
- color: Theme.of(context).extension<CakeTextTheme>()!.titleColor,
282
+ color: Theme.of(context)
283
+ .extension<CakeTextTheme>()!
284
+ .titleColor,
285
),
286
),
287
),
@@ -299,8 +303,12 @@ class ConnectDevicePageBodyState extends State<ConnectDevicePageBody> {
303
if (widget.allowChangeWallet) ...[
304
PrimaryButton(
305
text: S.of(context).wallets,
302
- color: Theme.of(context).extension<WalletListTheme>()!.createNewWalletButtonBackgroundColor,
303
- textColor: Theme.of(context).extension<WalletListTheme>()!.restoreWalletButtonTextColor,
306
+ color: Theme.of(context)
307
+ .extension<WalletListTheme>()!
308
+ .createNewWalletButtonBackgroundColor,
309
+ textColor: Theme.of(context)
310
+ .extension<WalletListTheme>()!
311
+ .restoreWalletButtonTextColor,
312
onPressed: _onChangeWallet,
313
)
314
],
lib/view_model/hardware_wallet/ledger_view_model.dart
+33
-26
@@ -99,47 +99,54 @@ abstract class LedgerViewModelBase with Store {
99
}
100
101
Future<void> connectLedger(sdk.LedgerDevice device, WalletType type) async {
102
+ _isConnecting = true;
103
+ _connectingWalletType = type;
104
if (isConnected) {
105
try {
104
- await _connectionChangeListener?.cancel();
105
- _connectionChangeListener = null;
106
await _connection!.disconnect().catchError((_) {});
107
} catch (_) {}
108
}
109
+
110
final ledger = device.connectionType == sdk.ConnectionType.ble
111
? ledgerPlusBLE
112
: ledgerPlusUSB;
113
113
-
114
- if (_connectionChangeListener == null) {
115
- _connectionChangeListener = ledger.deviceStateChanges.listen((event) {
116
- printV('Ledger Device State Changed: $event');
117
- if (event == sdk.BleConnectionState.disconnected) {
118
- _connection = null;
119
- if (type == WalletType.monero) {
120
- monero!.resetLedgerConnection();
121
-
122
- Navigator.of( navigatorKey.currentContext!).pushNamed(
123
- Routes.connectDevices,
124
- arguments: ConnectDevicePageParams(
125
- walletType: WalletType.monero,
126
- allowChangeWallet: true,
127
- isReconnect: true,
128
- onConnectDevice: (context, ledgerVM) async {
129
- Navigator.of(context).pop();
130
- },
131
- ),
132
- );
133
- }
134
- }
135
- });
114
+ if (_connectionChangeSubscription == null) {
115
+ _connectionChangeSubscription = ledger.deviceStateChanges
116
+ .listen(_connectionChangeListener);
117
}
118
119
_connection = await ledger.connect(device);
120
+ _isConnecting = false;
121
}
122
141
- StreamSubscription<sdk.BleConnectionState>? _connectionChangeListener;
123
+ StreamSubscription<sdk.BleConnectionState>? _connectionChangeSubscription;
124
sdk.LedgerConnection? _connection;
125
+ bool _isConnecting = true;
126
+ WalletType? _connectingWalletType;
127
+
128
+ void _connectionChangeListener(
129
+ sdk.BleConnectionState event, ) {
130
+ printV('Ledger Device State Changed: $event');
131
+ if (event == sdk.BleConnectionState.disconnected && !_isConnecting) {
132
+ _connection = null;
133
+ if (_connectingWalletType == WalletType.monero) {
134
+ monero!.resetLedgerConnection();
135
+
136
+ Navigator.of(navigatorKey.currentContext!).pushNamed(
137
+ Routes.connectDevices,
138
+ arguments: ConnectDevicePageParams(
139
+ walletType: WalletType.monero,
140
+ allowChangeWallet: true,
141
+ isReconnect: true,
142
+ onConnectDevice: (context, ledgerVM) async {
143
+ Navigator.of(context).pop();
144
+ },
145
+ ),
146
+ );
147
+ }
148
+ }
149
+ }
150
151
bool get isConnected => _connection != null && !(_connection!.isDisconnected);
152