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';
|
2024-11-12 04:26:09 +01:00
|
|
|
import 'package:cake_wallet/src/widgets/primary_button.dart';
|
2024-05-04 20:44:50 -05:00
|
|
|
import 'package:cake_wallet/themes/extensions/cake_text_theme.dart';
|
2024-11-12 04:26:09 +01:00
|
|
|
import 'package:cake_wallet/themes/extensions/wallet_list_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';
|
|
|
|
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-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,
|
|
|
|
});
|
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-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,
|
|
|
|
allowChangeWallet = params.allowChangeWallet;
|
2024-05-04 20:44:50 -05:00
|
|
|
|
|
|
|
@override
|
|
|
|
String get title => S.current.restore_title_from_hardware_wallet;
|
|
|
|
|
|
|
|
@override
|
2024-11-12 04:26:09 +01:00
|
|
|
Widget body(BuildContext context) => ConnectDevicePageBody(
|
|
|
|
walletType, onConnectDevice, allowChangeWallet, ledgerVM);
|
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;
|
|
|
|
|
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,
|
|
|
|
);
|
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;
|
|
|
|
|
|
|
|
@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-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();
|
|
|
|
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) {
|
|
|
|
_bleRefresh = widget.ledgerVM
|
|
|
|
.scanForBleDevices()
|
|
|
|
.listen((device) => setState(() => bleDevices.add(device)))
|
|
|
|
..onError((e) {
|
|
|
|
throw e.toString();
|
|
|
|
});
|
|
|
|
_bleRefreshTimer?.cancel();
|
|
|
|
_bleRefreshTimer = null;
|
|
|
|
}
|
2024-05-17 08:15:19 -05:00
|
|
|
} catch (e) {
|
2024-10-23 17:38:31 +02:00
|
|
|
print(e);
|
2024-05-04 20:44:50 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> _connectToDevice(LedgerDevice device) async {
|
2024-10-23 17:38:31 +02:00
|
|
|
await widget.ledgerVM.connectLedger(device, widget.walletType);
|
2024-05-04 20:44:50 -05:00
|
|
|
widget.onConnectDevice(context, widget.ledgerVM);
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
return Center(
|
|
|
|
child: Container(
|
|
|
|
width: ResponsiveLayoutUtilBase.kDesktopMaxWidthConstraint,
|
|
|
|
height: double.infinity,
|
|
|
|
padding: EdgeInsets.symmetric(vertical: 24, horizontal: 24),
|
|
|
|
child: SingleChildScrollView(
|
|
|
|
child: Column(
|
|
|
|
children: [
|
|
|
|
Padding(
|
|
|
|
padding: EdgeInsets.only(left: 20, right: 20, bottom: 20),
|
|
|
|
child: Text(
|
|
|
|
Platform.isIOS
|
|
|
|
? S.of(context).connect_your_hardware_wallet_ios
|
|
|
|
: S.of(context).connect_your_hardware_wallet,
|
|
|
|
style: TextStyle(
|
|
|
|
fontSize: 16,
|
|
|
|
fontWeight: FontWeight.w500,
|
2024-10-23 17:38:31 +02:00
|
|
|
color: Theme.of(context)
|
|
|
|
.extension<CakeTextTheme>()!
|
|
|
|
.titleColor),
|
2024-05-04 20:44:50 -05:00
|
|
|
textAlign: TextAlign.center,
|
|
|
|
),
|
|
|
|
),
|
2024-05-17 08:15:19 -05:00
|
|
|
// DeviceTile(
|
|
|
|
// onPressed: () => Navigator.of(context).push(
|
|
|
|
// MaterialPageRoute<void>(
|
|
|
|
// builder: (BuildContext context) => DebugDevicePage(),
|
|
|
|
// ),
|
|
|
|
// ),
|
|
|
|
// title: "Debug Ledger",
|
|
|
|
// leading: imageLedger,
|
|
|
|
// ),
|
2024-10-23 17:38:31 +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: TextStyle(
|
|
|
|
fontSize: 16,
|
|
|
|
fontWeight: FontWeight.w500,
|
|
|
|
color: Theme.of(context)
|
|
|
|
.extension<CakeTextTheme>()!
|
|
|
|
.titleColor),
|
|
|
|
textAlign: TextAlign.center,
|
|
|
|
),
|
2024-05-04 20:44:50 -05:00
|
|
|
),
|
|
|
|
),
|
2024-10-23 17:38:31 +02:00
|
|
|
),
|
|
|
|
|
2024-05-04 20:44:50 -05: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: TextStyle(
|
|
|
|
fontSize: 14,
|
|
|
|
fontWeight: FontWeight.w400,
|
2024-10-23 17:38:31 +02:00
|
|
|
color: Theme.of(context)
|
|
|
|
.extension<CakeTextTheme>()!
|
|
|
|
.titleColor,
|
2024-05-04 20:44:50 -05:00
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
...bleDevices
|
|
|
|
.map(
|
|
|
|
(device) => Padding(
|
|
|
|
padding: EdgeInsets.only(bottom: 20),
|
|
|
|
child: DeviceTile(
|
|
|
|
onPressed: () => _connectToDevice(device),
|
|
|
|
title: device.name,
|
2024-10-23 17:38:31 +02:00
|
|
|
leading: _getDeviceTileLeading(device.deviceInfo),
|
2024-05-04 20:44:50 -05:00
|
|
|
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: TextStyle(
|
|
|
|
fontSize: 14,
|
|
|
|
fontWeight: FontWeight.w400,
|
2024-11-12 04:26:09 +01:00
|
|
|
color: Theme.of(context).extension<CakeTextTheme>()!.titleColor,
|
2024-05-04 20:44:50 -05:00
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
...usbDevices
|
|
|
|
.map(
|
|
|
|
(device) => Padding(
|
|
|
|
padding: EdgeInsets.only(bottom: 20),
|
|
|
|
child: DeviceTile(
|
|
|
|
onPressed: () => _connectToDevice(device),
|
|
|
|
title: device.name,
|
2024-10-23 17:38:31 +02:00
|
|
|
leading: _getDeviceTileLeading(device.deviceInfo),
|
2024-05-04 20:44:50 -05:00
|
|
|
connectionType: device.connectionType,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
.toList(),
|
2024-11-12 04:26:09 +01:00
|
|
|
],
|
|
|
|
if (widget.allowChangeWallet) ...[
|
|
|
|
PrimaryButton(
|
|
|
|
text: S.of(context).wallets,
|
|
|
|
color: Theme.of(context).extension<WalletListTheme>()!.createNewWalletButtonBackgroundColor,
|
|
|
|
textColor: Theme.of(context).extension<WalletListTheme>()!.restoreWalletButtonTextColor,
|
|
|
|
onPressed: _onChangeWallet,
|
|
|
|
)
|
|
|
|
],
|
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),
|
|
|
|
);
|
|
|
|
}
|
2024-05-04 20:44:50 -05:00
|
|
|
}
|