| 1 | import 'package:cake_wallet/bitcoin/bitcoin.dart'; |
| 2 | import 'package:cake_wallet/main.dart'; |
| 3 | import 'package:cake_wallet/new-ui/widgets/keyboard_hide_overlay.dart'; |
| 4 | import 'package:cake_wallet/new-ui/widgets/receive_page/receive_top_bar.dart'; |
| 5 | import 'package:cake_wallet/src/widgets/alert_with_two_actions.dart'; |
| 6 | import 'package:cake_wallet/utils/show_pop_up.dart'; |
| 7 | import 'package:flutter/material.dart'; |
| 8 | import 'package:flutter_mobx/flutter_mobx.dart'; |
| 9 | import 'package:cake_wallet/view_model/rescan_view_model.dart'; |
| 10 | import 'package:cake_wallet/src/widgets/blockchain_height_widget.dart'; |
| 11 | import 'package:cake_wallet/src/widgets/primary_button.dart'; |
| 12 | import 'package:cake_wallet/generated/i18n.dart'; |
| 13 | import 'package:cw_core/wallet_type.dart'; |
| 14 | import 'package:modal_bottom_sheet/modal_bottom_sheet.dart'; |
| 15 | |
| 16 | class RescanPage extends StatefulWidget { |
| 17 | RescanPage(this._rescanViewModel); |
| 18 | |
| 19 | final RescanViewModel _rescanViewModel; |
| 20 | |
| 21 | @override |
| 22 | State<RescanPage> createState() => _RescanPageState(); |
| 23 | } |
| 24 | |
| 25 | class _RescanPageState extends State<RescanPage> { |
| 26 | final TextEditingController _heightController = TextEditingController(); |
| 27 | |
| 28 | @override |
| 29 | Widget build(BuildContext context) { |
| 30 | Widget child; |
| 31 | if (widget._rescanViewModel.wallet.type != WalletType.decred) { |
| 32 | child = Padding( |
| 33 | padding: EdgeInsets.only(left: 24, right: 24, bottom: 24), |
| 34 | child: Column( |
| 35 | mainAxisAlignment: MainAxisAlignment.spaceBetween, |
| 36 | children: [ |
| 37 | Observer( |
| 38 | builder: (_) => SingleChildScrollView( |
| 39 | controller: ModalScrollController.of(context), |
| 40 | child: BlockchainHeightWidget( |
| 41 | // key: _blockchainHeightWidgetKey, |
| 42 | onHeightOrDateEntered: (value) => widget._rescanViewModel.isButtonEnabled = value, |
| 43 | isSilentPaymentsScan: widget._rescanViewModel.isSilentPaymentsScan, |
| 44 | isMwebScan: widget._rescanViewModel.isMwebScan, |
| 45 | doSingleScan: widget._rescanViewModel.doSingleScan, |
| 46 | hasDatePicker: !widget._rescanViewModel.isMwebScan, |
| 47 | // disable date picker for mweb for now |
| 48 | toggleSingleScan: () => |
| 49 | widget._rescanViewModel.doSingleScan = !widget._rescanViewModel.doSingleScan, |
| 50 | walletType: widget._rescanViewModel.wallet.type, |
| 51 | heightController: _heightController, |
| 52 | bitcoinMempoolAPIEnabled: widget._rescanViewModel.isBitcoinMempoolAPIEnabled, |
| 53 | ), |
| 54 | ), |
| 55 | ), |
| 56 | Observer( |
| 57 | builder: (_) => LoadingPrimaryButton( |
| 58 | isLoading: widget._rescanViewModel.state == RescanWalletState.rescaning, |
| 59 | text: S.of(context).rescan, |
| 60 | onPressed: () async { |
| 61 | if (widget._rescanViewModel.isSilentPaymentsScan) { |
| 62 | return _toggleSilentPaymentsScanning(context); |
| 63 | } |
| 64 | |
| 65 | widget._rescanViewModel.rescanCurrentWallet( |
| 66 | restoreHeight: int.tryParse(_heightController.text) ?? 0); |
| 67 | |
| 68 | Navigator.of(context).pop(); |
| 69 | }, |
| 70 | color: Theme.of(context).colorScheme.primary, |
| 71 | textColor: Theme.of(context).colorScheme.onPrimary, |
| 72 | isDisabled: !widget._rescanViewModel.isButtonEnabled, |
| 73 | ), |
| 74 | ) |
| 75 | ], |
| 76 | ), |
| 77 | ); |
| 78 | } else { |
| 79 | child = Center( |
| 80 | child: Padding( |
| 81 | padding: EdgeInsets.only(left: 24, right: 24, bottom: 24), |
| 82 | child: Column( |
| 83 | mainAxisAlignment: MainAxisAlignment.spaceBetween, |
| 84 | children: [ |
| 85 | Spacer(), |
| 86 | Observer( |
| 87 | builder: (_) => LoadingPrimaryButton( |
| 88 | isLoading: widget._rescanViewModel.state == RescanWalletState.rescaning, |
| 89 | text: S.of(context).rescan, |
| 90 | onPressed: () async { |
| 91 | await widget._rescanViewModel.rescanCurrentWallet(restoreHeight: 0); |
| 92 | Navigator.of(context).pop(); |
| 93 | }, |
| 94 | color: Theme.of(context).colorScheme.primary, |
| 95 | textColor: Theme.of(context).colorScheme.onPrimary, |
| 96 | ), |
| 97 | ) |
| 98 | ], |
| 99 | ), |
| 100 | ), |
| 101 | ); |
| 102 | } |
| 103 | return GestureDetector( |
| 104 | behavior: HitTestBehavior.opaque, |
| 105 | // onTap: () => FocusScope.of(context).unfocus(), |
| 106 | child: Material( |
| 107 | color: Theme.of(context).colorScheme.surface, |
| 108 | child: KeyboardHideOverlay( |
| 109 | child: SafeArea( |
| 110 | child: Column( |
| 111 | children: [ |
| 112 | ModalTopBar( |
| 113 | title: widget._rescanViewModel.isSilentPaymentsScan |
| 114 | ? S.current.silent_payments_scanning |
| 115 | : S.current.rescan, |
| 116 | leadingIcon: Icon(Icons.arrow_back_ios_new), |
| 117 | leadingSemanticLabel: S.current.seed_alert_back, |
| 118 | onLeadingPressed: Navigator.of(context).pop), |
| 119 | Expanded( |
| 120 | child: Padding( |
| 121 | padding: EdgeInsets.only(bottom: MediaQuery.of(context).viewInsets.bottom), |
| 122 | child: child, |
| 123 | )), |
| 124 | ], |
| 125 | ), |
| 126 | ), |
| 127 | ), |
| 128 | ), |
| 129 | ); |
| 130 | } |
| 131 | |
| 132 | Future<void> _toggleSilentPaymentsScanning(BuildContext context) async { |
| 133 | final height = int.tryParse(_heightController.text) ?? 0; |
| 134 | |
| 135 | Navigator.of(context).pop(); |
| 136 | |
| 137 | final needsToSwitch = |
| 138 | await bitcoin!.getNodeIsElectrsSPEnabled(widget._rescanViewModel.wallet) == false; |
| 139 | |
| 140 | if (needsToSwitch) { |
| 141 | return showPopUp<void>( |
| 142 | context: navigatorKey.currentState!.context, |
| 143 | builder: (BuildContext _dialogContext) => AlertWithTwoActions( |
| 144 | alertTitle: S.of(_dialogContext).change_current_node_title, |
| 145 | alertContent: S.of(_dialogContext).confirm_silent_payments_switch_node, |
| 146 | rightButtonText: S.of(_dialogContext).confirm, |
| 147 | leftButtonText: S.of(_dialogContext).cancel, |
| 148 | actionRightButton: () async { |
| 149 | Navigator.of(_dialogContext).pop(); |
| 150 | |
| 151 | widget._rescanViewModel.rescanCurrentWallet(restoreHeight: height); |
| 152 | }, |
| 153 | actionLeftButton: () => Navigator.of(_dialogContext).pop(), |
| 154 | ), |
| 155 | ); |
| 156 | } |
| 157 | |
| 158 | widget._rescanViewModel.rescanCurrentWallet(restoreHeight: height); |
| 159 | } |
| 160 | } |