| 1 | import 'package:cake_wallet/generated/i18n.dart'; |
| 2 | import 'package:cake_wallet/cake_pay/src/cake_pay_states.dart'; |
| 3 | import 'package:cake_wallet/src/screens/base_page.dart'; |
| 4 | import 'package:cake_wallet/src/widgets/alert_with_one_action.dart'; |
| 5 | import 'package:cake_wallet/src/widgets/base_text_form_field.dart'; |
| 6 | import 'package:cake_wallet/src/widgets/keyboard_done_button.dart'; |
| 7 | import 'package:cake_wallet/src/widgets/primary_button.dart'; |
| 8 | import 'package:cake_wallet/src/widgets/scrollable_with_bottom_section.dart'; |
| 9 | import 'package:cake_wallet/typography.dart'; |
| 10 | import 'package:cake_wallet/utils/show_pop_up.dart'; |
| 11 | import 'package:cake_wallet/view_model/cake_pay/cake_pay_auth_view_model.dart'; |
| 12 | import 'package:flutter/material.dart'; |
| 13 | import 'package:flutter_mobx/flutter_mobx.dart'; |
| 14 | import 'package:keyboard_actions/keyboard_actions.dart'; |
| 15 | import 'package:mobx/mobx.dart'; |
| 16 | |
| 17 | class CakePayVerifyOtpPage extends BasePage { |
| 18 | CakePayVerifyOtpPage(this._authViewModel, this._email, this.isSignIn) |
| 19 | : _codeController = TextEditingController(), |
| 20 | _codeFocus = FocusNode() { |
| 21 | _codeController.addListener(() { |
| 22 | final otp = _codeController.text; |
| 23 | _authViewModel.otp = otp; |
| 24 | if (otp.length > 5) { |
| 25 | _authViewModel.otpState = CakePayOtpSendEnabled(); |
| 26 | } else { |
| 27 | _authViewModel.otpState = CakePayOtpSendDisabled(); |
| 28 | } |
| 29 | }); |
| 30 | } |
| 31 | |
| 32 | final CakePayAuthViewModel _authViewModel; |
| 33 | final bool isSignIn; |
| 34 | |
| 35 | final String _email; |
| 36 | |
| 37 | @override |
| 38 | Widget middle(BuildContext context) { |
| 39 | return Text( |
| 40 | S.current.verification, |
| 41 | style: textMediumSemiBold( |
| 42 | color: Theme.of(context).colorScheme.onSurface, |
| 43 | ), |
| 44 | ); |
| 45 | } |
| 46 | |
| 47 | final TextEditingController _codeController; |
| 48 | final FocusNode _codeFocus; |
| 49 | |
| 50 | @override |
| 51 | Widget body(BuildContext context) { |
| 52 | reaction((_) => _authViewModel.otpState, (CakePayOtpState state) { |
| 53 | if (state is CakePayOtpFailure) { |
| 54 | _onOtpFailure(context, state.error); |
| 55 | } |
| 56 | if (state is CakePayOtpSuccess) { |
| 57 | _onOtpSuccessful(context); |
| 58 | } |
| 59 | }); |
| 60 | return KeyboardActions( |
| 61 | config: KeyboardActionsConfig( |
| 62 | keyboardActionsPlatform: KeyboardActionsPlatform.IOS, |
| 63 | keyboardBarColor: Theme.of(context).colorScheme.surface, |
| 64 | nextFocus: false, |
| 65 | actions: [ |
| 66 | KeyboardActionsItem( |
| 67 | focusNode: _codeFocus, |
| 68 | toolbarButtons: [(_) => KeyboardDoneButton()], |
| 69 | ), |
| 70 | ]), |
| 71 | child: Container( |
| 72 | height: 0, |
| 73 | color: Theme.of(context).colorScheme.surface, |
| 74 | child: ScrollableWithBottomSection( |
| 75 | contentPadding: EdgeInsets.all(24), |
| 76 | content: Column( |
| 77 | children: [ |
| 78 | BaseTextFormField( |
| 79 | hintText: S.of(context).enter_code, |
| 80 | keyboardType: TextInputType.numberWithOptions(signed: false, decimal: true), |
| 81 | focusNode: _codeFocus, |
| 82 | controller: _codeController, |
| 83 | onSubmit: (_) => _verify(), |
| 84 | ), |
| 85 | SizedBox(height: 14), |
| 86 | Text( |
| 87 | S.of(context).fill_code, |
| 88 | style: Theme.of(context).textTheme.bodySmall!.copyWith( |
| 89 | color: Theme.of(context).colorScheme.onSurfaceVariant, |
| 90 | ), |
| 91 | ), |
| 92 | SizedBox(height: 34), |
| 93 | Row( |
| 94 | mainAxisAlignment: MainAxisAlignment.center, |
| 95 | children: [ |
| 96 | Text(S.of(context).didnt_get_code), |
| 97 | SizedBox(width: 20), |
| 98 | InkWell( |
| 99 | onTap: () => _authViewModel.logIn(_email), |
| 100 | child: Text( |
| 101 | S.of(context).resend_code, |
| 102 | style: Theme.of(context).textTheme.bodyMedium!.copyWith( |
| 103 | color: Theme.of(context).colorScheme.primary, |
| 104 | ), |
| 105 | ), |
| 106 | ), |
| 107 | ], |
| 108 | ), |
| 109 | ], |
| 110 | ), |
| 111 | bottomSectionPadding: EdgeInsets.symmetric(vertical: 36, horizontal: 24), |
| 112 | bottomSection: Column( |
| 113 | children: [ |
| 114 | Column( |
| 115 | mainAxisAlignment: MainAxisAlignment.end, |
| 116 | children: <Widget>[ |
| 117 | Observer( |
| 118 | builder: (_) => LoadingPrimaryButton( |
| 119 | text: S.of(context).continue_text, |
| 120 | onPressed: _verify, |
| 121 | isDisabled: _authViewModel.otpState is CakePayOtpSendDisabled, |
| 122 | isLoading: _authViewModel.otpState is CakePayOtpValidating, |
| 123 | color: Theme.of(context).colorScheme.primary, |
| 124 | textColor: Theme.of(context).colorScheme.onPrimary, |
| 125 | ), |
| 126 | ), |
| 127 | SizedBox(height: 20), |
| 128 | ], |
| 129 | ), |
| 130 | ], |
| 131 | ), |
| 132 | ), |
| 133 | ), |
| 134 | ); |
| 135 | } |
| 136 | |
| 137 | void _onOtpFailure(BuildContext context, String error) { |
| 138 | showPopUp<void>( |
| 139 | context: context, |
| 140 | builder: (BuildContext context) { |
| 141 | return AlertWithOneAction( |
| 142 | alertTitle: S.current.verification, |
| 143 | alertContent: error, |
| 144 | buttonText: S.of(context).ok, |
| 145 | buttonAction: () => Navigator.of(context).pop()); |
| 146 | }); |
| 147 | } |
| 148 | |
| 149 | void _onOtpSuccessful(BuildContext context) => Navigator.pop(context, true); |
| 150 | |
| 151 | void _verify() async => await _authViewModel.verifyEmail(_codeController.text); |
| 152 | } |