| 1 | import 'dart:async'; |
| 2 | |
| 3 | import 'package:another_flushbar/flushbar.dart'; |
| 4 | import 'package:cake_wallet/core/wallet_name_validator.dart'; |
| 5 | import 'package:cake_wallet/entities/wallet_edit_page_arguments.dart'; |
| 6 | import 'package:cake_wallet/src/widgets/alert_with_one_action.dart'; |
| 7 | import 'package:cake_wallet/src/widgets/alert_with_two_actions.dart'; |
| 8 | import 'package:cake_wallet/routes.dart'; |
| 9 | import 'package:cake_wallet/src/screens/auth/auth_page.dart'; |
| 10 | import 'package:cake_wallet/src/screens/wallet_unlock/wallet_unlock_arguments.dart'; |
| 11 | import 'package:cake_wallet/store/settings_store.dart'; |
| 12 | import 'package:cake_wallet/utils/show_bar.dart'; |
| 13 | import 'package:cake_wallet/utils/show_pop_up.dart'; |
| 14 | import 'package:cake_wallet/view_model/wallet_list/wallet_edit_view_model.dart'; |
| 15 | import 'package:cw_core/utils/print_verbose.dart'; |
| 16 | import 'package:flutter/material.dart'; |
| 17 | import 'package:cake_wallet/generated/i18n.dart'; |
| 18 | import 'package:cake_wallet/src/widgets/primary_button.dart'; |
| 19 | import 'package:cake_wallet/src/widgets/base_text_form_field.dart'; |
| 20 | import 'package:cake_wallet/src/screens/base_page.dart'; |
| 21 | import 'package:flutter_mobx/flutter_mobx.dart'; |
| 22 | |
| 23 | class WalletEditPage extends BasePage { |
| 24 | WalletEditPage({ |
| 25 | required this.pageArguments, |
| 26 | }) : _formKey = GlobalKey<FormState>(), |
| 27 | _labelController = TextEditingController(), |
| 28 | walletEditViewModel = pageArguments.walletEditViewModel!, |
| 29 | super() { |
| 30 | _labelController.text = |
| 31 | pageArguments.isWalletGroup ? pageArguments.groupName : pageArguments.editingWallet.name; |
| 32 | _labelController.addListener(() => walletEditViewModel.newName = _labelController.text); |
| 33 | } |
| 34 | |
| 35 | final GlobalKey<FormState> _formKey; |
| 36 | final TextEditingController _labelController; |
| 37 | |
| 38 | final WalletEditPageArguments pageArguments; |
| 39 | final WalletEditViewModel walletEditViewModel; |
| 40 | |
| 41 | @override |
| 42 | String get title => pageArguments.isWalletGroup |
| 43 | ? S.current.wallet_list_edit_group_name |
| 44 | : S.current.wallet_list_edit_wallet; |
| 45 | |
| 46 | Flushbar<void>? _progressBar; |
| 47 | |
| 48 | @override |
| 49 | Widget body(BuildContext context) { |
| 50 | return Form( |
| 51 | key: _formKey, |
| 52 | child: Container( |
| 53 | padding: EdgeInsets.all(24.0), |
| 54 | child: Column( |
| 55 | children: <Widget>[ |
| 56 | Expanded( |
| 57 | child: Center( |
| 58 | child: BaseTextFormField( |
| 59 | controller: _labelController, |
| 60 | hintText: S.of(context).wallet_list_wallet_name, |
| 61 | validator: WalletNameValidator(), |
| 62 | ), |
| 63 | ), |
| 64 | ), |
| 65 | Observer( |
| 66 | builder: (_) { |
| 67 | final isLoading = walletEditViewModel.state is WalletEditRenamePending || |
| 68 | walletEditViewModel.state is WalletEditDeletePending; |
| 69 | |
| 70 | return Row( |
| 71 | children: <Widget>[ |
| 72 | if (!pageArguments.isWalletGroup) |
| 73 | Flexible( |
| 74 | child: Container( |
| 75 | padding: EdgeInsets.only(right: 8.0), |
| 76 | child: LoadingPrimaryButton( |
| 77 | isDisabled: isLoading, |
| 78 | onPressed: () => _removeWallet(context), |
| 79 | text: S.of(context).delete, |
| 80 | color: Theme.of(context).colorScheme.errorContainer, |
| 81 | textColor: Theme.of(context).colorScheme.onErrorContainer, |
| 82 | ), |
| 83 | ), |
| 84 | ), |
| 85 | Flexible( |
| 86 | child: Container( |
| 87 | padding: EdgeInsets.only(left: 8.0), |
| 88 | child: LoadingPrimaryButton( |
| 89 | onPressed: () async { |
| 90 | if (_formKey.currentState?.validate() ?? false) { |
| 91 | if (!pageArguments.isWalletGroup && |
| 92 | await pageArguments.walletNewVM! |
| 93 | .nameExists(walletEditViewModel.newName)) { |
| 94 | showPopUp<void>( |
| 95 | context: context, |
| 96 | builder: (_) { |
| 97 | return AlertWithOneAction( |
| 98 | alertTitle: '', |
| 99 | alertContent: S.of(context).wallet_name_exists, |
| 100 | buttonText: S.of(context).ok, |
| 101 | buttonAction: () => Navigator.of(context).pop(), |
| 102 | ); |
| 103 | }, |
| 104 | ); |
| 105 | } else { |
| 106 | try { |
| 107 | bool confirmed = false; |
| 108 | |
| 109 | if (SettingsStoreBase.walletPasswordDirectInput) { |
| 110 | await Navigator.of(context).pushNamed( |
| 111 | Routes.walletUnlockLoadable, |
| 112 | arguments: WalletUnlockArguments( |
| 113 | authPasswordHandler: (String password) async { |
| 114 | await walletEditViewModel.changeName( |
| 115 | pageArguments.editingWallet, |
| 116 | password: password, |
| 117 | isWalletGroup: pageArguments.isWalletGroup, |
| 118 | walletGroupKey: pageArguments.walletGroupKey, |
| 119 | ); |
| 120 | }, |
| 121 | callback: (bool isAuthenticatedSuccessfully, |
| 122 | AuthPageState auth) async { |
| 123 | if (isAuthenticatedSuccessfully) { |
| 124 | auth.close(); |
| 125 | confirmed = true; |
| 126 | } |
| 127 | }, |
| 128 | walletName: pageArguments.editingWallet.name, |
| 129 | walletType: pageArguments.editingWallet.type)); |
| 130 | } else { |
| 131 | await walletEditViewModel.changeName( |
| 132 | pageArguments.editingWallet, |
| 133 | isWalletGroup: pageArguments.isWalletGroup, |
| 134 | walletGroupKey: pageArguments.walletGroupKey, |
| 135 | ); |
| 136 | confirmed = true; |
| 137 | } |
| 138 | |
| 139 | if (confirmed) { |
| 140 | Navigator.of(context).pop(); |
| 141 | walletEditViewModel.resetState(); |
| 142 | } |
| 143 | } catch (e) {} |
| 144 | } |
| 145 | } |
| 146 | }, |
| 147 | text: S.of(context).save, |
| 148 | color: Theme.of(context).colorScheme.primary, |
| 149 | textColor: Theme.of(context).colorScheme.onPrimary, |
| 150 | isDisabled: walletEditViewModel.newName.isEmpty || isLoading, |
| 151 | ), |
| 152 | ), |
| 153 | ) |
| 154 | ], |
| 155 | ); |
| 156 | }, |
| 157 | ) |
| 158 | ], |
| 159 | ), |
| 160 | ), |
| 161 | ); |
| 162 | } |
| 163 | |
| 164 | Future<void> _removeWallet(BuildContext context) async { |
| 165 | pageArguments.authService!.authenticateAction( |
| 166 | context, |
| 167 | onAuthSuccess: (isAuthenticatedSuccessfully) async { |
| 168 | if (!isAuthenticatedSuccessfully) { |
| 169 | return; |
| 170 | } |
| 171 | |
| 172 | _onSuccessfulAuth(context); |
| 173 | }, |
| 174 | conditionToDetermineIfToUse2FA: false, |
| 175 | ); |
| 176 | } |
| 177 | |
| 178 | void _onSuccessfulAuth(BuildContext context) async { |
| 179 | bool confirmed = false; |
| 180 | |
| 181 | await showPopUp<void>( |
| 182 | context: context, |
| 183 | builder: (BuildContext dialogContext) { |
| 184 | return AlertWithTwoActions( |
| 185 | alertTitle: S.of(context).delete_wallet, |
| 186 | alertContent: |
| 187 | S.of(context).delete_wallet_confirm_message(pageArguments.editingWallet.name), |
| 188 | leftButtonText: S.of(context).cancel, |
| 189 | rightButtonText: S.of(context).delete, |
| 190 | actionLeftButton: () => Navigator.of(dialogContext).pop(), |
| 191 | actionRightButton: () { |
| 192 | confirmed = true; |
| 193 | Navigator.of(dialogContext).pop(); |
| 194 | }, |
| 195 | ); |
| 196 | }, |
| 197 | ); |
| 198 | |
| 199 | if (confirmed) { |
| 200 | Navigator.of(context).pop(); |
| 201 | |
| 202 | try { |
| 203 | changeProcessText( |
| 204 | context, S.of(context).wallet_list_removing_wallet(pageArguments.editingWallet.name)); |
| 205 | await walletEditViewModel.remove(pageArguments.editingWallet); |
| 206 | hideProgressText(); |
| 207 | } catch (e) { |
| 208 | changeProcessText( |
| 209 | context, |
| 210 | S |
| 211 | .of(context) |
| 212 | .wallet_list_failed_to_remove(pageArguments.editingWallet.name, e.toString()), |
| 213 | ); |
| 214 | } |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | void changeProcessText(BuildContext context, String text) { |
| 219 | _progressBar = createBar<void>(text, context, duration: null)..show(context); |
| 220 | } |
| 221 | |
| 222 | Future<void> hideProgressText() async { |
| 223 | try { |
| 224 | await Future.delayed(Duration(milliseconds: 250)); |
| 225 | await _progressBar?.dismiss(); |
| 226 | } catch (e) { |
| 227 | printV(e); |
| 228 | } |
| 229 | _progressBar = null; |
| 230 | } |
| 231 | } |