| 1 | import 'package:cake_wallet/core/email_validator.dart'; |
| 2 | import 'package:cake_wallet/generated/i18n.dart'; |
| 3 | import 'package:cake_wallet/cake_pay/src/cake_pay_states.dart'; |
| 4 | import 'package:cake_wallet/routes.dart'; |
| 5 | import 'package:cake_wallet/src/screens/base_page.dart'; |
| 6 | import 'package:cake_wallet/src/widgets/alert_with_one_action.dart'; |
| 7 | import 'package:cake_wallet/src/widgets/base_text_form_field.dart'; |
| 8 | import 'package:cake_wallet/src/widgets/primary_button.dart'; |
| 9 | import 'package:cake_wallet/src/widgets/scrollable_with_bottom_section.dart'; |
| 10 | import 'package:cake_wallet/typography.dart'; |
| 11 | import 'package:cake_wallet/utils/show_pop_up.dart'; |
| 12 | import 'package:cake_wallet/view_model/cake_pay/cake_pay_auth_view_model.dart'; |
| 13 | import 'package:flutter/material.dart'; |
| 14 | import 'package:flutter_mobx/flutter_mobx.dart'; |
| 15 | import 'package:mobx/mobx.dart'; |
| 16 | |
| 17 | class CakePayWelcomePage extends BasePage { |
| 18 | CakePayWelcomePage(this._authViewModel) |
| 19 | : _formKey = GlobalKey<FormState>(), |
| 20 | _emailController = TextEditingController() { |
| 21 | _emailController.text = _authViewModel.email; |
| 22 | _emailController.addListener(() => _authViewModel.email = _emailController.text); |
| 23 | } |
| 24 | |
| 25 | final GlobalKey<FormState> _formKey; |
| 26 | |
| 27 | final CakePayAuthViewModel _authViewModel; |
| 28 | |
| 29 | final TextEditingController _emailController; |
| 30 | |
| 31 | @override |
| 32 | Widget middle(BuildContext context) { |
| 33 | return Text( |
| 34 | S.current.welcome_to_cakepay, |
| 35 | style: textMediumSemiBold( |
| 36 | color: Theme.of(context).colorScheme.onSurface, |
| 37 | ), |
| 38 | ); |
| 39 | } |
| 40 | |
| 41 | @override |
| 42 | Widget body(BuildContext context) { |
| 43 | reaction((_) => _authViewModel.userVerificationState, (CakePayUserVerificationState state) { |
| 44 | if (state is CakePayUserVerificationStateFailure) { |
| 45 | _onLoginUserFailure(context, state.error); |
| 46 | } |
| 47 | if (state is CakePayUserVerificationStateSuccess) { |
| 48 | _onLoginSuccessful(context, _authViewModel); |
| 49 | } |
| 50 | }); |
| 51 | return ScrollableWithBottomSection( |
| 52 | contentPadding: EdgeInsets.all(24), |
| 53 | content: Column( |
| 54 | children: [ |
| 55 | SizedBox(height: 90), |
| 56 | Form( |
| 57 | key: _formKey, |
| 58 | child: BaseTextFormField( |
| 59 | hintText: S.of(context).email_address, |
| 60 | keyboardType: TextInputType.emailAddress, |
| 61 | validator: EmailValidator(), |
| 62 | controller: _emailController, |
| 63 | onSubmit: (text) => _login(), |
| 64 | ), |
| 65 | ), |
| 66 | SizedBox(height: 20), |
| 67 | Text( |
| 68 | S.of(context).about_cake_pay, |
| 69 | style: textLarge( |
| 70 | color: Theme.of(context).colorScheme.onSurface, |
| 71 | ), |
| 72 | ), |
| 73 | SizedBox(height: 20), |
| 74 | Text(S.of(context).cake_pay_account_note, |
| 75 | style: textLarge(color: Theme.of(context).colorScheme.onSurface)), |
| 76 | ], |
| 77 | ), |
| 78 | bottomSectionPadding: EdgeInsets.symmetric(vertical: 36, horizontal: 24), |
| 79 | bottomSection: Column( |
| 80 | children: [ |
| 81 | Column( |
| 82 | mainAxisAlignment: MainAxisAlignment.end, |
| 83 | children: <Widget>[ |
| 84 | Observer( |
| 85 | builder: (_) => LoadingPrimaryButton( |
| 86 | text: S.of(context).login, |
| 87 | onPressed: _login, |
| 88 | isLoading: |
| 89 | _authViewModel.userVerificationState is CakePayUserVerificationStateLoading, |
| 90 | color: Theme.of(context).colorScheme.primary, |
| 91 | textColor: Theme.of(context).colorScheme.onPrimary, |
| 92 | ), |
| 93 | ), |
| 94 | SizedBox( |
| 95 | height: 20, |
| 96 | ), |
| 97 | ], |
| 98 | ), |
| 99 | ], |
| 100 | ), |
| 101 | ); |
| 102 | } |
| 103 | |
| 104 | void _onLoginUserFailure(BuildContext context, String error) { |
| 105 | showPopUp<void>( |
| 106 | context: context, |
| 107 | builder: (BuildContext context) { |
| 108 | return AlertWithOneAction( |
| 109 | alertTitle: S.current.login, |
| 110 | alertContent: error, |
| 111 | buttonText: S.of(context).ok, |
| 112 | buttonAction: () => Navigator.of(context).pop()); |
| 113 | }); |
| 114 | } |
| 115 | |
| 116 | Future<void> _onLoginSuccessful(BuildContext context, CakePayAuthViewModel authViewModel) async { |
| 117 | final verified = await Navigator.pushNamed<bool>(context, Routes.cakePayVerifyOtpPage, |
| 118 | arguments: [authViewModel.email, true]); |
| 119 | |
| 120 | if (verified == true && Navigator.of(context).canPop()) { |
| 121 | Navigator.pop(context, true); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | void _login() async { |
| 126 | if (_formKey.currentState != null && !_formKey.currentState!.validate()) { |
| 127 | return; |
| 128 | } |
| 129 | await _authViewModel.logIn(_emailController.text); |
| 130 | } |
| 131 | } |