| 1 | import 'package:another_flushbar/flushbar.dart'; |
| 2 | import 'package:cake_wallet/new-ui/widgets/modern_button.dart'; |
| 3 | import 'package:cake_wallet/utils/show_bar.dart'; |
| 4 | import 'package:mobx/mobx.dart'; |
| 5 | import 'package:flutter/material.dart'; |
| 6 | import 'package:flutter/cupertino.dart'; |
| 7 | import 'package:cake_wallet/generated/i18n.dart'; |
| 8 | import 'package:cake_wallet/view_model/auth_state.dart'; |
| 9 | import 'package:cake_wallet/view_model/auth_view_model.dart'; |
| 10 | import 'package:cake_wallet/src/screens/pin_code/pin_code.dart'; |
| 11 | import 'package:cake_wallet/src/screens/pin_code/pin_code_widget.dart'; |
| 12 | import 'package:cake_wallet/core/execution_state.dart'; |
| 13 | |
| 14 | typedef OnAuthenticationFinished = void Function(bool, AuthPageState); |
| 15 | |
| 16 | abstract class AuthPageState<T extends StatefulWidget> extends State<T> { |
| 17 | void changeProcessText(String text); |
| 18 | void hideProgressText(); |
| 19 | Future<void> close({String? route, dynamic arguments}); |
| 20 | } |
| 21 | |
| 22 | class AuthPage extends StatefulWidget { |
| 23 | AuthPage(this.authViewModel, {required this.onAuthenticationFinished, this.closable = true}); |
| 24 | |
| 25 | final AuthViewModel authViewModel; |
| 26 | final OnAuthenticationFinished onAuthenticationFinished; |
| 27 | final bool closable; |
| 28 | |
| 29 | @override |
| 30 | AuthPageState createState() => AuthPagePinCodeStateImpl(); |
| 31 | } |
| 32 | |
| 33 | class AuthPagePinCodeStateImpl extends AuthPageState<AuthPage> { |
| 34 | final _key = GlobalKey<ScaffoldState>(); |
| 35 | final _pinCodeKey = GlobalKey<PinCodeState>(); |
| 36 | ReactionDisposer? _reaction; |
| 37 | Flushbar<void>? _authBar; |
| 38 | Flushbar<void>? _progressBar; |
| 39 | |
| 40 | @override |
| 41 | void initState() { |
| 42 | _reaction ??= reaction((_) => widget.authViewModel.state, (ExecutionState state) { |
| 43 | if (state is ExecutedSuccessfullyState) { |
| 44 | WidgetsBinding.instance.addPostFrameCallback((_) { |
| 45 | widget.onAuthenticationFinished(true, this); |
| 46 | }); |
| 47 | setState(() {}); |
| 48 | } |
| 49 | |
| 50 | if (state is IsExecutingState) { |
| 51 | WidgetsBinding.instance.addPostFrameCallback( |
| 52 | (_) { |
| 53 | // null duration to make it indefinite until its disposed |
| 54 | _authBar = createBar<void>(S.of(context).authentication, context, duration: null) |
| 55 | ..show(context); |
| 56 | }, |
| 57 | ); |
| 58 | } |
| 59 | |
| 60 | if (state is FailureState) { |
| 61 | WidgetsBinding.instance.addPostFrameCallback((_) async { |
| 62 | _pinCodeKey.currentState?.clear(); |
| 63 | dismissFlushBar(_authBar); |
| 64 | if (context.mounted) { |
| 65 | showBar<void>(context, S.of(context).failed_authentication(state.error)); |
| 66 | } |
| 67 | widget.onAuthenticationFinished(false, this); |
| 68 | }); |
| 69 | } |
| 70 | |
| 71 | if (state is AuthenticationBanned) { |
| 72 | WidgetsBinding.instance.addPostFrameCallback((_) async { |
| 73 | _pinCodeKey.currentState?.clear(); |
| 74 | dismissFlushBar(_authBar); |
| 75 | showBar<void>(context, S.of(context).failed_authentication(state.error)); |
| 76 | widget.onAuthenticationFinished(false, this); |
| 77 | }); |
| 78 | } |
| 79 | }); |
| 80 | |
| 81 | if (widget.authViewModel.isBiometricalAuthenticationAllowed) { |
| 82 | WidgetsBinding.instance.addPostFrameCallback((_) async { |
| 83 | await Future<void>.delayed(Duration(milliseconds: 100)); |
| 84 | await widget.authViewModel.biometricAuth(); |
| 85 | }); |
| 86 | } |
| 87 | |
| 88 | super.initState(); |
| 89 | } |
| 90 | |
| 91 | @override |
| 92 | void dispose() { |
| 93 | _reaction?.reaction.dispose(); |
| 94 | super.dispose(); |
| 95 | } |
| 96 | |
| 97 | @override |
| 98 | void changeProcessText(String text) { |
| 99 | dismissFlushBar(_authBar); |
| 100 | _progressBar = createBar<void>(text, context, duration: null)..show(_key.currentContext!); |
| 101 | } |
| 102 | |
| 103 | @override |
| 104 | void hideProgressText() { |
| 105 | dismissFlushBar(_progressBar); |
| 106 | _progressBar = null; |
| 107 | } |
| 108 | |
| 109 | @override |
| 110 | Future<void> close({String? route, dynamic arguments}) async { |
| 111 | if (_key.currentContext == null) { |
| 112 | // throw Exception('Key context is null. Should be not happened'); |
| 113 | // flutter can happen, so safe to just return |
| 114 | return; |
| 115 | } |
| 116 | |
| 117 | /// not the best scenario, but WidgetsBinding is not behaving correctly on Android |
| 118 | await Future<void>.delayed(Duration(milliseconds: 50)); |
| 119 | await _authBar?.dismiss(); |
| 120 | await Future<void>.delayed(Duration(milliseconds: 50)); |
| 121 | await _progressBar?.dismiss(); |
| 122 | await Future<void>.delayed(Duration(milliseconds: 50)); |
| 123 | if (route != null) { |
| 124 | Navigator.of(_key.currentContext!).pushReplacementNamed(route, arguments: arguments); |
| 125 | } else { |
| 126 | Navigator.of(_key.currentContext!).pop(); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | @override |
| 131 | Widget build(BuildContext context) { |
| 132 | return Container( |
| 133 | color: Theme.of(context).colorScheme.surface, |
| 134 | child: Padding( |
| 135 | padding: const EdgeInsets.only(top: 16.0), |
| 136 | child: Scaffold( |
| 137 | key: _key, |
| 138 | appBar: CupertinoNavigationBar( |
| 139 | leading: widget.closable |
| 140 | ? ModernButton( |
| 141 | size: 36, |
| 142 | icon: Icon(Icons.close), |
| 143 | onPressed: Navigator.of(context).pop, |
| 144 | semanticLabel: S.of(context).close, |
| 145 | iconColor: Theme.of(context).colorScheme.onSurfaceVariant) |
| 146 | : Container(), |
| 147 | backgroundColor: Theme.of(context).colorScheme.surface, |
| 148 | border: null), |
| 149 | resizeToAvoidBottomInset: false, |
| 150 | body: PinCode((pin, _) => widget.authViewModel.auth(password: pin), (_) => null, |
| 151 | widget.authViewModel.pinLength, false, _pinCodeKey)), |
| 152 | ), |
| 153 | ); |
| 154 | } |
| 155 | |
| 156 | void dismissFlushBar(Flushbar<dynamic>? bar) { |
| 157 | WidgetsBinding.instance.addPostFrameCallback((_) async { |
| 158 | await bar?.dismiss(); |
| 159 | }); |
| 160 | } |
| 161 | } |