2024-05-04 20:44:50 -05:00
|
|
|
import 'dart:async';
|
|
|
|
import 'dart:io';
|
|
|
|
|
|
|
|
import 'package:cake_wallet/generated/i18n.dart';
|
2024-11-12 04:26:09 +01:00
|
|
|
import 'package:cake_wallet/routes.dart';
|
2024-05-04 20:44:50 -05:00
|
|
|
import 'package:cake_wallet/src/screens/base_page.dart';
|
|
|
|
import 'package:cake_wallet/src/screens/connect_device/widgets/device_tile.dart';
|
2025-05-26 17:38:51 +02:00
|
|
|
import 'package:cake_wallet/src/widgets/bottom_sheet/info_steps_bottom_sheet_widget.dart';
|
2024-11-12 04:26:09 +01:00
|
|
|
import 'package:cake_wallet/src/widgets/primary_button.dart';
|
2025-05-26 17:38:51 +02:00
|
|
|
import 'package:cake_wallet/themes/core/material_base_theme.dart';
|
2024-05-04 20:44:50 -05:00
|
|
|
import 'package:cake_wallet/utils/responsive_layout_util.dart';
|
|
|
|
import 'package:cake_wallet/view_model/hardware_wallet/ledger_view_model.dart';
|
2024-12-09 12:23:59 -06:00
|
|
|
import 'package:cw_core/utils/print_verbose.dart';
|
2024-05-04 20:44:50 -05:00
|
|
|
import 'package:cw_core/wallet_type.dart';
|
|
|
|
import 'package:flutter/material.dart';
|
2024-10-23 17:38:31 +02:00
|
|
|
import 'package:flutter_mobx/flutter_mobx.dart';
|
|
|
|
import 'package:ledger_flutter_plus/ledger_flutter_plus.dart';
|
2024-05-04 20:44:50 -05:00
|
|
|
|
|
|
|
typedef OnConnectDevice = void Function(BuildContext, LedgerViewModel);
|
|
|
|
|
|
|
|
class ConnectDevicePageParams {
|
|
|
|
final WalletType walletType;
|
|
|
|
final OnConnectDevice onConnectDevice;
|
2024-11-12 04:26:09 +01:00
|
|
|
final bool allowChangeWallet;
|
2024-12-14 00:32:36 +01:00
|
|
|
final bool isReconnect;
|
2024-05-04 20:44:50 -05:00
|
|
|
|
2024-11-12 04:26:09 +01:00
|
|
|
ConnectDevicePageParams({
|
|
|
|
required this.walletType,
|
|
|
|
required this.onConnectDevice,
|
|
|
|
this.allowChangeWallet = false,
|
2025-04-03 00:20:13 +02:00
|
|
|
this.isReconnect = true,
|
2024-11-12 04:26:09 +01:00
|
|
|
});
|
2024-05-04 20:44:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
class ConnectDevicePage extends BasePage {
|
|
|
|
final WalletType walletType;
|
|
|
|
final OnConnectDevice onConnectDevice;
|
2024-11-12 04:26:09 +01:00
|
|
|
final bool allowChangeWallet;
|
2024-12-14 00:32:36 +01:00
|
|
|
final bool isReconnect;
|
2024-05-04 20:44:50 -05:00
|
|
|
final LedgerViewModel ledgerVM;
|
|
|
|
|
|
|
|
ConnectDevicePage(ConnectDevicePageParams params, this.ledgerVM)
|
|
|
|
: walletType = params.walletType,
|
2024-11-12 04:26:09 +01:00
|
|
|
onConnectDevice = params.onConnectDevice,
|
2024-12-14 00:32:36 +01:00
|
|
|
allowChangeWallet = params.allowChangeWallet,
|
|
|
|
isReconnect = params.isReconnect;
|
2024-05-04 20:44:50 -05:00
|
|
|
|
|
|
|
@override
|
2024-12-14 00:32:36 +01:00
|
|
|
String get title => isReconnect
|
|
|
|
? S.current.reconnect_your_hardware_wallet
|
|
|
|
: S.current.restore_title_from_hardware_wallet;
|
2024-05-04 20:44:50 -05:00
|
|
|
|
|
|
|
@override
|
2024-12-14 00:32:36 +01:00
|
|
|
Widget? leading(BuildContext context) =>
|
|
|
|
!isReconnect ? super.leading(context) : null;
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget body(BuildContext context) => PopScope(
|
|
|
|
canPop: !isReconnect,
|
|
|
|
child: ConnectDevicePageBody(
|
|
|
|
walletType,
|
|
|
|
onConnectDevice,
|
|
|
|
allowChangeWallet,
|
|
|
|
ledgerVM,
|
2025-05-26 17:38:51 +02:00
|
|
|
currentTheme,
|
2024-12-14 00:32:36 +01:00
|
|
|
));
|
2024-05-04 20:44:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
class ConnectDevicePageBody extends StatefulWidget {
|
|
|
|
final WalletType walletType;
|
|
|
|
final OnConnectDevice onConnectDevice;
|
2024-11-12 04:26:09 +01:00
|
|
|
final bool allowChangeWallet;
|
2024-05-04 20:44:50 -05:00
|
|
|
final LedgerViewModel ledgerVM;
|
2025-05-26 17:38:51 +02:00
|
|
|
final MaterialThemeBase currentTheme;
|
2024-05-04 20:44:50 -05:00
|
|
|
|
2024-10-23 17:38:31 +02:00
|
|
|
const ConnectDevicePageBody(
|
2024-11-12 04:26:09 +01:00
|
|
|
this.walletType,
|
|
|
|
this.onConnectDevice,
|
|
|
|
this.allowChangeWallet,
|
|
|
|
this.ledgerVM,
|
2025-05-26 17:38:51 +02:00
|
|
|
this.currentTheme,
|
2024-11-12 04:26:09 +01:00
|
|
|
);
|
2024-05-04 20:44:50 -05:00
|
|
|
|
|
|
|
@override
|
|
|
|
ConnectDevicePageBodyState createState() => ConnectDevicePageBodyState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class ConnectDevicePageBodyState extends State<ConnectDevicePageBody> {
|
|
|
|
var bleDevices = <LedgerDevice>[];
|
|
|
|
var usbDevices = <LedgerDevice>[];
|
|
|
|
|
|
|
|
late Timer? _usbRefreshTimer = null;
|
|
|
|
late Timer? _bleRefreshTimer = null;
|
2024-10-23 17:38:31 +02:00
|
|
|
late Timer? _bleStateTimer = null;
|
2024-05-04 20:44:50 -05:00
|
|
|
late StreamSubscription<LedgerDevice>? _bleRefresh = null;
|
|
|
|
|
2024-12-14 00:32:36 +01:00
|
|
|
bool longWait = false;
|
2024-12-17 19:57:57 +01:00
|
|
|
Timer? _longWaitTimer;
|
2024-12-14 00:32:36 +01:00
|
|
|
|
2024-05-04 20:44:50 -05:00
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
2024-05-17 08:15:19 -05:00
|
|
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
2024-10-23 17:38:31 +02:00
|
|
|
_bleStateTimer = Timer.periodic(
|
|
|
|
Duration(seconds: 1), (_) => widget.ledgerVM.updateBleState());
|
|
|
|
|
|
|
|
_bleRefreshTimer =
|
|
|
|
Timer.periodic(Duration(seconds: 1), (_) => _refreshBleDevices());
|
2024-05-17 08:15:19 -05:00
|
|
|
|
|
|
|
if (Platform.isAndroid) {
|
2024-10-23 17:38:31 +02:00
|
|
|
_usbRefreshTimer =
|
|
|
|
Timer.periodic(Duration(seconds: 1), (_) => _refreshUsbDevices());
|
2024-05-17 08:15:19 -05:00
|
|
|
}
|
2024-12-14 00:32:36 +01:00
|
|
|
|
2024-12-17 19:57:57 +01:00
|
|
|
_longWaitTimer = Timer(Duration(seconds: 10), () {
|
2024-12-14 00:32:36 +01:00
|
|
|
if (widget.ledgerVM.bleIsEnabled && bleDevices.isEmpty)
|
|
|
|
setState(() => longWait = true);
|
|
|
|
});
|
2024-05-17 08:15:19 -05:00
|
|
|
});
|
2024-05-04 20:44:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void dispose() {
|
|
|
|
_bleRefreshTimer?.cancel();
|
2024-10-23 17:38:31 +02:00
|
|
|
_bleStateTimer?.cancel();
|
2024-05-04 20:44:50 -05:00
|
|
|
_usbRefreshTimer?.cancel();
|
|
|
|
_bleRefresh?.cancel();
|
2024-12-17 19:57:57 +01:00
|
|
|
_longWaitTimer?.cancel();
|
2024-12-14 00:32:36 +01:00
|
|
|
|
|
|
|
widget.ledgerVM.stopScanning();
|
2024-05-04 20:44:50 -05:00
|
|
|
super.dispose();
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> _refreshUsbDevices() async {
|
2024-10-23 17:38:31 +02:00
|
|
|
final dev = await widget.ledgerVM.ledgerPlusUSB.devices;
|
2024-05-04 20:44:50 -05:00
|
|
|
if (usbDevices.length != dev.length) setState(() => usbDevices = dev);
|
2024-10-23 17:38:31 +02:00
|
|
|
// _usbRefresh = widget.ledgerVM
|
|
|
|
// .scanForUsbDevices()
|
|
|
|
// .listen((device) => setState(() => usbDevices.add(device)))
|
|
|
|
// ..onError((e) {
|
|
|
|
// throw e.toString();
|
|
|
|
// });
|
|
|
|
// Keep polling until the lfp lib gets updated
|
|
|
|
// _usbRefreshTimer?.cancel();
|
|
|
|
// _usbRefreshTimer = null;
|
2024-05-04 20:44:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> _refreshBleDevices() async {
|
2024-05-17 08:15:19 -05:00
|
|
|
try {
|
2024-11-12 04:26:09 +01:00
|
|
|
if (widget.ledgerVM.bleIsEnabled) {
|
2024-12-14 00:32:36 +01:00
|
|
|
_bleRefresh =
|
|
|
|
widget.ledgerVM.scanForBleDevices().listen((device) => setState(() {
|
|
|
|
bleDevices.add(device);
|
|
|
|
if (longWait) longWait = false;
|
|
|
|
}))
|
|
|
|
..onError((e) {
|
|
|
|
throw e.toString();
|
|
|
|
});
|
2024-11-12 04:26:09 +01:00
|
|
|
_bleRefreshTimer?.cancel();
|
|
|
|
_bleRefreshTimer = null;
|
|
|
|
}
|
2024-05-17 08:15:19 -05:00
|
|
|
} catch (e) {
|
2024-12-09 12:23:59 -06:00
|
|
|
printV(e);
|
2024-05-04 20:44:50 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> _connectToDevice(LedgerDevice device) async {
|
2025-06-19 19:00:16 +02:00
|
|
|
final isConnected =
|
|
|
|
await widget.ledgerVM.connectLedger(device, widget.walletType);
|
|
|
|
if (isConnected) widget.onConnectDevice(context, widget.ledgerVM);
|
2024-05-04 20:44:50 -05:00
|
|
|
}
|
|
|
|
|
2024-10-23 17:38:31 +02:00
|
|
|
String _getDeviceTileLeading(LedgerDeviceType deviceInfo) {
|
|
|
|
switch (deviceInfo) {
|
|
|
|
case LedgerDeviceType.nanoX:
|
|
|
|
return 'assets/images/hardware_wallet/ledger_nano_x.png';
|
|
|
|
case LedgerDeviceType.stax:
|
|
|
|
return 'assets/images/hardware_wallet/ledger_stax.png';
|
|
|
|
case LedgerDeviceType.flex:
|
|
|
|
return 'assets/images/hardware_wallet/ledger_flex.png';
|
|
|
|
default:
|
|
|
|
return 'assets/images/hardware_wallet/ledger_nano_x.png';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-04 20:44:50 -05:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2025-05-26 17:38:51 +02:00
|
|
|
return Container(
|
|
|
|
width: ResponsiveLayoutUtilBase.kDesktopMaxWidthConstraint,
|
|
|
|
padding: EdgeInsets.symmetric(vertical: 24, horizontal: 24),
|
|
|
|
child: Column(
|
|
|
|
children: [
|
|
|
|
Expanded(
|
|
|
|
child: SingleChildScrollView(
|
|
|
|
child: Column(
|
|
|
|
children: [
|
|
|
|
Padding(
|
2024-10-23 17:38:31 +02:00
|
|
|
padding: EdgeInsets.only(left: 20, right: 20, bottom: 20),
|
|
|
|
child: Text(
|
2025-05-26 17:38:51 +02:00
|
|
|
Platform.isIOS
|
|
|
|
? S.of(context).connect_your_hardware_wallet_ios
|
|
|
|
: S.of(context).connect_your_hardware_wallet,
|
|
|
|
style: Theme.of(context)
|
|
|
|
.textTheme.titleMedium,
|
2024-10-23 17:38:31 +02:00
|
|
|
textAlign: TextAlign.center,
|
|
|
|
),
|
2024-05-04 20:44:50 -05:00
|
|
|
),
|
2025-05-26 17:38:51 +02:00
|
|
|
Offstage(
|
|
|
|
offstage: !longWait,
|
|
|
|
child: Padding(
|
|
|
|
padding: EdgeInsets.only(left: 20, right: 20, bottom: 20),
|
|
|
|
child: Text(
|
|
|
|
S.of(context).if_you_dont_see_your_device,
|
|
|
|
style: Theme.of(context)
|
|
|
|
.textTheme.titleMedium,
|
|
|
|
textAlign: TextAlign.center,
|
|
|
|
),
|
2024-05-04 20:44:50 -05:00
|
|
|
),
|
|
|
|
),
|
2025-05-26 17:38:51 +02:00
|
|
|
Observer(
|
|
|
|
builder: (_) => Offstage(
|
|
|
|
offstage: widget.ledgerVM.bleIsEnabled,
|
|
|
|
child: Padding(
|
|
|
|
padding:
|
|
|
|
EdgeInsets.only(left: 20, right: 20, bottom: 20),
|
|
|
|
child: Text(
|
|
|
|
S.of(context).ledger_please_enable_bluetooth,
|
|
|
|
style: Theme.of(context)
|
|
|
|
.textTheme.titleMedium,
|
|
|
|
textAlign: TextAlign.center,
|
2024-05-04 20:44:50 -05:00
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
2025-05-26 17:38:51 +02:00
|
|
|
if (bleDevices.length > 0) ...[
|
|
|
|
Padding(
|
|
|
|
padding: EdgeInsets.only(left: 20, right: 20, bottom: 20),
|
|
|
|
child: Container(
|
|
|
|
width: double.infinity,
|
|
|
|
child: Text(
|
|
|
|
S.of(context).bluetooth,
|
|
|
|
style: Theme.of(context)
|
|
|
|
.textTheme.bodyMedium,
|
2024-05-04 20:44:50 -05:00
|
|
|
),
|
|
|
|
),
|
2025-05-26 17:38:51 +02:00
|
|
|
),
|
|
|
|
...bleDevices
|
|
|
|
.map(
|
|
|
|
(device) => Padding(
|
|
|
|
padding: EdgeInsets.only(bottom: 20),
|
|
|
|
child: DeviceTile(
|
|
|
|
onPressed: () => _connectToDevice(device),
|
|
|
|
title: device.name,
|
|
|
|
leading: _getDeviceTileLeading(device.deviceInfo),
|
|
|
|
connectionType: device.connectionType,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
.toList()
|
|
|
|
],
|
|
|
|
if (usbDevices.length > 0) ...[
|
|
|
|
Padding(
|
|
|
|
padding: EdgeInsets.only(left: 20, right: 20, bottom: 20),
|
|
|
|
child: Container(
|
|
|
|
width: double.infinity,
|
|
|
|
child: Text(
|
|
|
|
S.of(context).usb,
|
|
|
|
style: Theme.of(context)
|
|
|
|
.textTheme.bodyMedium,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
...usbDevices
|
|
|
|
.map(
|
|
|
|
(device) => Padding(
|
|
|
|
padding: EdgeInsets.only(bottom: 20),
|
|
|
|
child: DeviceTile(
|
|
|
|
onPressed: () => _connectToDevice(device),
|
|
|
|
title: device.name,
|
|
|
|
leading: _getDeviceTileLeading(device.deviceInfo),
|
|
|
|
connectionType: device.connectionType,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
.toList(),
|
|
|
|
],
|
|
|
|
if (widget.allowChangeWallet) ...[
|
|
|
|
PrimaryButton(
|
|
|
|
text: S.of(context).wallets,
|
|
|
|
color: Theme.of(context)
|
|
|
|
.colorScheme.primary,
|
|
|
|
textColor: Theme.of(context)
|
|
|
|
.colorScheme.onPrimary,
|
|
|
|
onPressed: _onChangeWallet,
|
2024-05-04 20:44:50 -05:00
|
|
|
)
|
2025-05-26 17:38:51 +02:00
|
|
|
],
|
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
2024-05-04 20:44:50 -05:00
|
|
|
),
|
2025-05-26 17:38:51 +02:00
|
|
|
PrimaryButton(
|
|
|
|
text: S.of(context).how_to_connect,
|
|
|
|
color: Theme.of(context).colorScheme.surfaceContainer,
|
|
|
|
textColor: Theme.of(context).colorScheme.onSecondaryContainer,
|
|
|
|
onPressed: () => _onHowToConnect(context),
|
|
|
|
)
|
|
|
|
],
|
2024-05-04 20:44:50 -05:00
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
2024-11-12 04:26:09 +01:00
|
|
|
|
|
|
|
void _onChangeWallet() {
|
|
|
|
Navigator.of(context).pushNamed(
|
|
|
|
Routes.walletList,
|
|
|
|
arguments: (BuildContext context) => Navigator.of(context)
|
|
|
|
.pushNamedAndRemoveUntil(Routes.dashboard, (route) => false),
|
|
|
|
);
|
|
|
|
}
|
2025-05-26 17:38:51 +02:00
|
|
|
|
|
|
|
void _onHowToConnect(BuildContext context) {
|
|
|
|
showModalBottomSheet(
|
|
|
|
context: context,
|
|
|
|
isScrollControlled: true,
|
|
|
|
builder: (BuildContext bottomSheetContext) => InfoStepsBottomSheet(
|
|
|
|
titleText: S.of(context).how_to_connect,
|
|
|
|
currentTheme: widget.currentTheme,
|
|
|
|
steps: [
|
|
|
|
InfoStep('${S.of(context).step} 1', S.of(context).connect_hw_info_step_1),
|
|
|
|
InfoStep('${S.of(context).step} 2', S.of(context).connect_hw_info_step_2),
|
|
|
|
InfoStep('${S.of(context).step} 3', S.of(context).connect_hw_info_step_3),
|
|
|
|
InfoStep('${S.of(context).step} 4', S.of(context).connect_hw_info_step_4),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
2024-05-04 20:44:50 -05:00
|
|
|
}
|