cake_wallet/lib/src/screens/auth/auth_page.dart

146 lines
4.8 KiB
Dart
Raw Normal View History

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/entities/biometric_auth.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,
{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>();
final _backArrowImageDarkTheme =
2020-06-20 07:10:00 +00:00
Image.asset('assets/images/back_arrow_dark_theme.png');
ReactionDisposer _reaction;
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-01-04 19:31:52 +00:00
WidgetsBinding.instance.addPostFrameCallback((_) {
if (widget.onAuthenticationFinished != null) {
widget.onAuthenticationFinished(true, this);
} else {
_key.currentState.showSnackBar(
SnackBar(
content: Text(S.of(context).authenticated),
backgroundColor: Colors.green,
),
);
}
});
}
2020-09-21 11:50:26 +00:00
if (state is IsExecutingState) {
2020-01-04 19:31:52 +00:00
WidgetsBinding.instance.addPostFrameCallback((_) {
_key.currentState.showSnackBar(
SnackBar(
content: Text(S.of(context).authentication),
backgroundColor: Colors.green,
),
);
});
}
2020-09-21 11:50:26 +00:00
if (state is FailureState) {
2020-01-08 12:26:34 +00:00
WidgetsBinding.instance.addPostFrameCallback((_) {
_pinCodeKey.currentState.clear();
_key.currentState.hideCurrentSnackBar();
_key.currentState.showSnackBar(
SnackBar(
content: Text(S.of(context).failed_authentication(state.error)),
backgroundColor: Colors.red,
),
);
if (widget.onAuthenticationFinished != null) {
widget.onAuthenticationFinished(false, this);
}
});
}
if (state is AuthenticationBanned) {
2020-01-04 19:31:52 +00:00
WidgetsBinding.instance.addPostFrameCallback((_) {
_pinCodeKey.currentState.clear();
_key.currentState.hideCurrentSnackBar();
_key.currentState.showSnackBar(
SnackBar(
content: Text(S.of(context).failed_authentication(state.error)),
backgroundColor: Colors.red,
),
);
if (widget.onAuthenticationFinished != null) {
widget.onAuthenticationFinished(false, this);
}
});
}
});
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() {
_reaction.reaction.dispose();
super.dispose();
}
void changeProcessText(String text) => _key.currentState.showSnackBar(
SnackBar(content: Text(text), backgroundColor: Colors.green));
void close() => Navigator.of(_key.currentContext).pop();
@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
? SizedBox(
height: 37,
width: 20,
child: ButtonTheme(
minWidth: double.minPositive,
child: FlatButton(
highlightColor: Colors.transparent,
splashColor: Colors.transparent,
padding: EdgeInsets.all(0),
onPressed: () => Navigator.of(context).pop(),
child: _backArrowImageDarkTheme),
),
)
: Container(),
backgroundColor: Theme.of(context).backgroundColor,
border: null),
2020-01-04 19:31:52 +00:00
resizeToAvoidBottomPadding: 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
}
}