cake_wallet/lib/src/screens/root/root.dart

93 lines
2.3 KiB
Dart
Raw Normal View History

2021-11-02 09:17:24 +00:00
import 'dart:async';
2020-01-04 19:31:52 +00:00
import 'package:flutter/material.dart';
import 'package:cake_wallet/routes.dart';
2020-01-08 12:26:34 +00:00
import 'package:cake_wallet/src/screens/auth/auth_page.dart';
2020-09-21 11:50:26 +00:00
import 'package:cake_wallet/store/app_store.dart';
import 'package:cake_wallet/store/authentication_store.dart';
import 'package:cake_wallet/entities/qr_scanner.dart';
2020-01-04 19:31:52 +00:00
class Root extends StatefulWidget {
2020-09-29 17:56:11 +00:00
Root(
{Key key,
this.authenticationStore,
this.appStore,
this.child,
this.navigatorKey})
2020-09-14 12:08:33 +00:00
: super(key: key);
2020-06-20 07:10:00 +00:00
final AuthenticationStore authenticationStore;
final AppStore appStore;
2020-09-29 17:56:11 +00:00
final GlobalKey<NavigatorState> navigatorKey;
2020-09-14 12:08:33 +00:00
final Widget child;
2020-01-04 19:31:52 +00:00
@override
RootState createState() => RootState();
}
class RootState extends State<Root> with WidgetsBindingObserver {
2021-11-02 09:17:24 +00:00
Stream<bool> get isInactive => _isInactiveController.stream;
StreamController<bool> _isInactiveController;
2020-01-04 19:31:52 +00:00
bool _isInactive;
bool _postFrameCallback;
@override
void initState() {
2021-11-02 09:17:24 +00:00
_isInactiveController = StreamController<bool>();
2020-01-04 19:31:52 +00:00
_isInactive = false;
_postFrameCallback = false;
WidgetsBinding.instance.addObserver(this);
super.initState();
}
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
switch (state) {
case AppLifecycleState.paused:
if (isQrScannerShown) {
return;
}
2020-09-21 11:50:26 +00:00
if (!_isInactive &&
widget.authenticationStore.state == AuthenticationState.allowed) {
2021-11-02 09:17:24 +00:00
setState(() => _setInactive(true));
2020-09-21 11:50:26 +00:00
}
2020-01-04 19:31:52 +00:00
break;
default:
break;
}
}
@override
Widget build(BuildContext context) {
2020-09-21 11:50:26 +00:00
if (_isInactive && !_postFrameCallback) {
_postFrameCallback = true;
WidgetsBinding.instance.addPostFrameCallback((_) {
2020-09-29 17:56:11 +00:00
widget.navigatorKey.currentState.pushNamed(Routes.unlock,
2020-09-21 11:50:26 +00:00
arguments: (bool isAuthenticatedSuccessfully, AuthPageState auth) {
if (!isAuthenticatedSuccessfully) {
return;
}
_reset();
auth.close();
});
});
}
2020-06-20 07:10:00 +00:00
2020-09-28 19:02:30 +00:00
return WillPopScope(onWillPop: () async => false, child: widget.child);
2020-09-21 11:50:26 +00:00
}
2020-01-04 19:31:52 +00:00
2020-09-21 11:50:26 +00:00
void _reset() {
setState(() {
_postFrameCallback = false;
2021-11-02 09:17:24 +00:00
_setInactive(false);
2020-09-21 11:50:26 +00:00
});
2020-01-04 19:31:52 +00:00
}
2021-11-02 09:17:24 +00:00
void _setInactive(bool value) {
_isInactive = value;
_isInactiveController.add(value);
}
2020-01-04 19:31:52 +00:00
}