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

114 lines
3.6 KiB
Dart
Raw Normal View History

2020-01-04 19:31:52 +00:00
import 'package:mobx/mobx.dart';
import 'package:provider/provider.dart';
import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'package:cake_wallet/generated/i18n.dart';
import 'package:cake_wallet/src/stores/auth/auth_state.dart';
import 'package:cake_wallet/src/stores/auth/auth_store.dart';
import 'package:cake_wallet/src/screens/pin_code/pin_code.dart';
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 {
AuthPage({this.onAuthenticationFinished, this.closable = true});
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>();
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) {
final authStore = Provider.of<AuthStore>(context);
2020-01-08 12:26:34 +00:00
reaction((_) => authStore.state, (AuthState state) {
2020-01-04 19:31:52 +00:00
if (state is AuthenticatedSuccessfully) {
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,
),
);
}
});
}
if (state is AuthenticationInProgress) {
WidgetsBinding.instance.addPostFrameCallback((_) {
_key.currentState.showSnackBar(
SnackBar(
content: Text(S.of(context).authentication),
backgroundColor: Colors.green,
),
);
});
}
2020-01-08 12:26:34 +00:00
if (state is AuthenticationFailure) {
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);
}
});
}
});
return Scaffold(
key: _key,
appBar: CupertinoNavigationBar(
leading: widget.closable ? CloseButton() : Container(),
backgroundColor: Theme.of(context).backgroundColor,
border: null,
),
resizeToAvoidBottomPadding: false,
body: PinCode(
(pin, _) => authStore.auth(
password: pin.fold('', (ac, val) => ac + '$val')),
false,
_pinCodeKey));
}
}