2022-10-26 18:13:44 +00:00
|
|
|
import 'package:another_flushbar/flushbar.dart';
|
2020-09-30 18:23:15 +00:00
|
|
|
import 'package:cake_wallet/utils/show_bar.dart';
|
2020-01-04 19:31:52 +00:00
|
|
|
import 'package:mobx/mobx.dart';
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:flutter/cupertino.dart';
|
|
|
|
import 'package:cake_wallet/generated/i18n.dart';
|
2020-06-20 07:10:00 +00:00
|
|
|
import 'package:cake_wallet/view_model/auth_state.dart';
|
|
|
|
import 'package:cake_wallet/view_model/auth_view_model.dart';
|
2020-01-04 19:31:52 +00:00
|
|
|
import 'package:cake_wallet/src/screens/pin_code/pin_code.dart';
|
2020-09-21 11:50:26 +00:00
|
|
|
import 'package:cake_wallet/src/screens/pin_code/pin_code_widget.dart';
|
|
|
|
import 'package:cake_wallet/core/execution_state.dart';
|
2020-01-04 19:31:52 +00:00
|
|
|
|
2020-01-08 12:26:34 +00:00
|
|
|
typedef OnAuthenticationFinished = void Function(bool, AuthPageState);
|
2020-01-04 19:31:52 +00:00
|
|
|
|
|
|
|
class AuthPage extends StatefulWidget {
|
2020-09-21 11:50:26 +00:00
|
|
|
AuthPage(this.authViewModel,
|
2022-10-12 17:09:57 +00:00
|
|
|
{required this.onAuthenticationFinished,
|
|
|
|
this.closable = true});
|
2020-01-04 19:31:52 +00:00
|
|
|
|
2020-06-20 07:10:00 +00:00
|
|
|
final AuthViewModel authViewModel;
|
2020-01-08 12:26:34 +00:00
|
|
|
final OnAuthenticationFinished onAuthenticationFinished;
|
|
|
|
final bool closable;
|
|
|
|
|
2020-01-04 19:31:52 +00:00
|
|
|
@override
|
|
|
|
AuthPageState createState() => AuthPageState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class AuthPageState extends State<AuthPage> {
|
|
|
|
final _key = GlobalKey<ScaffoldState>();
|
|
|
|
final _pinCodeKey = GlobalKey<PinCodeState>();
|
2020-04-27 17:31:39 +00:00
|
|
|
final _backArrowImageDarkTheme =
|
2020-09-25 10:26:08 +00:00
|
|
|
Image.asset('assets/images/close_button.png');
|
2022-10-12 17:09:57 +00:00
|
|
|
ReactionDisposer? _reaction;
|
2022-10-26 18:13:44 +00:00
|
|
|
Flushbar<void>? _authBar;
|
|
|
|
Flushbar<void>? _progressBar;
|
2020-01-04 19:31:52 +00:00
|
|
|
|
|
|
|
@override
|
2020-06-20 07:10:00 +00:00
|
|
|
void initState() {
|
|
|
|
_reaction ??=
|
2020-09-21 11:50:26 +00:00
|
|
|
reaction((_) => widget.authViewModel.state, (ExecutionState state) {
|
|
|
|
if (state is ExecutedSuccessfullyState) {
|
2020-11-17 12:31:09 +00:00
|
|
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
2022-10-12 17:09:57 +00:00
|
|
|
widget.onAuthenticationFinished(true, this);
|
2020-11-17 12:31:09 +00:00
|
|
|
});
|
|
|
|
setState(() {});
|
2020-01-04 19:31:52 +00:00
|
|
|
}
|
|
|
|
|
2020-09-21 11:50:26 +00:00
|
|
|
if (state is IsExecutingState) {
|
2020-01-04 19:31:52 +00:00
|
|
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
2022-10-26 18:13:44 +00:00
|
|
|
// null duration to make it indefinite until its disposed
|
|
|
|
_authBar =
|
|
|
|
createBar<void>(S.of(context).authentication, duration: null)
|
|
|
|
..show(context);
|
2020-01-04 19:31:52 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-09-21 11:50:26 +00:00
|
|
|
if (state is FailureState) {
|
2022-10-12 17:09:57 +00:00
|
|
|
print('X');
|
|
|
|
print(state.error);
|
2022-10-26 18:13:44 +00:00
|
|
|
WidgetsBinding.instance.addPostFrameCallback((_) async {
|
2022-10-12 17:09:57 +00:00
|
|
|
_pinCodeKey.currentState?.clear();
|
2022-10-26 18:13:44 +00:00
|
|
|
dismissFlushBar(_authBar);
|
2020-09-30 18:23:15 +00:00
|
|
|
showBar<void>(
|
|
|
|
context, S.of(context).failed_authentication(state.error));
|
2020-01-08 12:26:34 +00:00
|
|
|
|
2022-10-26 18:13:44 +00:00
|
|
|
widget.onAuthenticationFinished(false, this);
|
2020-01-08 12:26:34 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (state is AuthenticationBanned) {
|
2022-10-26 18:13:44 +00:00
|
|
|
WidgetsBinding.instance.addPostFrameCallback((_) async {
|
2022-10-12 17:09:57 +00:00
|
|
|
_pinCodeKey.currentState?.clear();
|
2022-10-26 18:13:44 +00:00
|
|
|
dismissFlushBar(_authBar);
|
2020-09-30 18:23:15 +00:00
|
|
|
showBar<void>(
|
|
|
|
context, S.of(context).failed_authentication(state.error));
|
2020-01-04 19:31:52 +00:00
|
|
|
|
2022-10-26 18:13:44 +00:00
|
|
|
widget.onAuthenticationFinished(false, this);
|
2020-01-04 19:31:52 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2020-09-10 14:51:59 +00:00
|
|
|
|
2020-09-21 11:50:26 +00:00
|
|
|
if (widget.authViewModel.isBiometricalAuthenticationAllowed) {
|
2020-09-10 14:51:59 +00:00
|
|
|
WidgetsBinding.instance.addPostFrameCallback((_) async {
|
|
|
|
await Future<void>.delayed(Duration(milliseconds: 100));
|
2020-09-21 11:50:26 +00:00
|
|
|
await widget.authViewModel.biometricAuth();
|
2020-09-10 14:51:59 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-06-20 07:10:00 +00:00
|
|
|
super.initState();
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void dispose() {
|
2022-10-12 17:09:57 +00:00
|
|
|
_reaction?.reaction.dispose();
|
2020-06-20 07:10:00 +00:00
|
|
|
super.dispose();
|
|
|
|
}
|
|
|
|
|
2020-09-30 18:23:15 +00:00
|
|
|
void changeProcessText(String text) {
|
2022-10-26 18:13:44 +00:00
|
|
|
dismissFlushBar(_authBar);
|
|
|
|
_progressBar = createBar<void>(text, duration: null)
|
|
|
|
..show(_key.currentContext!);
|
2020-09-30 18:23:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void hideProgressText() {
|
2022-10-26 18:13:44 +00:00
|
|
|
dismissFlushBar(_progressBar);
|
|
|
|
_progressBar = null;
|
2020-09-30 18:23:15 +00:00
|
|
|
}
|
2020-06-20 07:10:00 +00:00
|
|
|
|
2022-10-26 18:13:44 +00:00
|
|
|
Future<void> close({String? route}) async {
|
2022-10-12 17:09:57 +00:00
|
|
|
if (_key.currentContext == null) {
|
|
|
|
throw Exception('Key context is null. Should be not happened');
|
|
|
|
}
|
|
|
|
|
2022-11-02 17:11:53 +00:00
|
|
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
|
|
|
dismissFlushBar(_authBar);
|
|
|
|
dismissFlushBar(_progressBar);
|
|
|
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
|
|
|
if (route != null) {
|
|
|
|
Navigator.of(_key.currentContext!).pushReplacementNamed(route);
|
|
|
|
} else {
|
|
|
|
Navigator.of(_key.currentContext!).pop();
|
|
|
|
}
|
|
|
|
});
|
2022-10-26 18:13:44 +00:00
|
|
|
});
|
2020-09-30 18:23:15 +00:00
|
|
|
}
|
2020-06-20 07:10:00 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2020-01-04 19:31:52 +00:00
|
|
|
return Scaffold(
|
|
|
|
key: _key,
|
|
|
|
appBar: CupertinoNavigationBar(
|
2020-09-21 11:50:26 +00:00
|
|
|
leading: widget.closable
|
2020-09-25 10:26:08 +00:00
|
|
|
? Container(
|
2020-09-30 18:23:15 +00:00
|
|
|
padding: EdgeInsets.only(top: 10),
|
|
|
|
child: SizedBox(
|
|
|
|
height: 37,
|
|
|
|
width: 37,
|
2022-10-21 16:30:36 +00:00
|
|
|
child: InkWell(
|
|
|
|
onTap: () => Navigator.of(context).pop(),
|
|
|
|
child: _backArrowImageDarkTheme,
|
2020-09-30 18:23:15 +00:00
|
|
|
),
|
|
|
|
))
|
2020-09-21 11:50:26 +00:00
|
|
|
: Container(),
|
|
|
|
backgroundColor: Theme.of(context).backgroundColor,
|
|
|
|
border: null),
|
2021-04-02 17:43:06 +00:00
|
|
|
resizeToAvoidBottomInset: false,
|
2020-09-21 11:50:26 +00:00
|
|
|
body: PinCode((pin, _) => widget.authViewModel.auth(password: pin),
|
|
|
|
(_) => null, widget.authViewModel.pinLength, false, _pinCodeKey));
|
2020-01-04 19:31:52 +00:00
|
|
|
}
|
2022-10-26 18:13:44 +00:00
|
|
|
|
|
|
|
void dismissFlushBar(Flushbar<dynamic>? bar) {
|
|
|
|
WidgetsBinding.instance.addPostFrameCallback((_) async {
|
|
|
|
await bar?.dismiss();
|
|
|
|
});
|
|
|
|
}
|
2020-01-04 19:31:52 +00:00
|
|
|
}
|