CakeWallet/lib/src/screens/root/root.dart
Omar Hatem ce4d375abf
Generic fixes and enhancements (#1083)
* Add exception handler to fiat APIs
Increase send card size for coin control
Fix Monero.com unspent coins hive box issue
minor bug fix

* Remove EIP-1559 parameters from Eth transaction
Enhance error reporting

* Throw error if not enough monero utx outputs are selected

* Fix Search text color

* Fix Ethereum sending EIP-1559 transactions

* Add transaction data to ERC20 transactions

* Add input check in single output transactions as well

* Fix Node deletion issue
Handle user input error in anonpay

* Remove exception handler from fiat conversion since it's not working with isolates

* Require enough utxo for amount and fees; More insightful Error messages

* Add cakewallet to applinks [skip ci]

* Add cakewallet app link for iOS [skip ci]

* Add applink depending on app scheme variable

* Add applink in iOS custom to the app getting built [skip ci]

* Handle normal app links without considering them as Payment URIs

* Minor fix [skip ci]

* Fixate encrypt package version as the recent update they made has some issues [skip ci]

---------

Co-authored-by: Konstantin Ullrich <konstantinullrich12@gmail.com>
2023-09-14 22:14:16 +03:00

188 lines
5.4 KiB
Dart

import 'dart:async';
import 'package:cake_wallet/core/auth_service.dart';
import 'package:cake_wallet/core/totp_request_details.dart';
import 'package:cake_wallet/utils/device_info.dart';
import 'package:cake_wallet/utils/payment_request.dart';
import 'package:flutter/material.dart';
import 'package:cake_wallet/routes.dart';
import 'package:cake_wallet/src/screens/auth/auth_page.dart';
import 'package:cake_wallet/store/app_store.dart';
import 'package:cake_wallet/store/authentication_store.dart';
import 'package:cake_wallet/entities/qr_scanner.dart';
import 'package:uni_links/uni_links.dart';
import '../setup_2fa/setup_2fa_enter_code_page.dart';
class Root extends StatefulWidget {
Root({
required Key key,
required this.authenticationStore,
required this.appStore,
required this.child,
required this.navigatorKey,
required this.authService,
}) : super(key: key);
final AuthenticationStore authenticationStore;
final AppStore appStore;
final GlobalKey<NavigatorState> navigatorKey;
final AuthService authService;
final Widget child;
@override
RootState createState() => RootState();
}
class RootState extends State<Root> with WidgetsBindingObserver {
RootState()
: _isInactiveController = StreamController<bool>.broadcast(),
_isInactive = false,
_requestAuth = true,
_postFrameCallback = false;
Stream<bool> get isInactive => _isInactiveController.stream;
StreamController<bool> _isInactiveController;
bool _isInactive;
bool _postFrameCallback;
bool _requestAuth;
StreamSubscription<Uri?>? stream;
Uri? launchUri;
@override
void initState() {
_requestAuth = widget.authService.requireAuth();
_isInactiveController = StreamController<bool>.broadcast();
_isInactive = false;
_postFrameCallback = false;
WidgetsBinding.instance.addObserver(this);
super.initState();
if (DeviceInfo.instance.isMobile) {
initUniLinks();
}
}
@override
void dispose() {
stream?.cancel();
super.dispose();
}
/// handle app links while the app is already started
/// whether its in the foreground or in the background.
Future<void> initUniLinks() async {
try {
stream = uriLinkStream.listen((Uri? uri) {
handleDeepLinking(uri);
});
handleDeepLinking(await getInitialUri());
} catch (e) {
print(e);
}
}
void handleDeepLinking(Uri? uri) {
if (uri == null || !mounted) return;
launchUri = uri;
}
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
switch (state) {
case AppLifecycleState.paused:
if (isQrScannerShown) {
return;
}
if (!_isInactive &&
widget.authenticationStore.state == AuthenticationState.allowed) {
setState(() => _setInactive(true));
}
break;
case AppLifecycleState.resumed:
setState(() {
_requestAuth = widget.authService.requireAuth();
});
break;
default:
break;
}
}
@override
Widget build(BuildContext context) {
if (_isInactive && !_postFrameCallback && _requestAuth) {
_postFrameCallback = true;
WidgetsBinding.instance.addPostFrameCallback((_) {
widget.navigatorKey.currentState?.pushNamed(
Routes.unlock,
arguments: (bool isAuthenticatedSuccessfully, AuthPageState auth) {
if (!isAuthenticatedSuccessfully) {
return;
} else {
final useTotp = widget.appStore.settingsStore.useTOTP2FA;
final shouldUseTotp2FAToAccessWallets = widget.appStore
.settingsStore.shouldRequireTOTP2FAForAccessingWallet;
if (useTotp && shouldUseTotp2FAToAccessWallets) {
_reset();
auth.close(
route: Routes.totpAuthCodePage,
arguments: TotpAuthArgumentsModel(
onTotpAuthenticationFinished:
(bool isAuthenticatedSuccessfully,
TotpAuthCodePageState totpAuth) {
if (!isAuthenticatedSuccessfully) {
return;
}
_reset();
totpAuth.close(
route: _isValidPaymentUri() ? Routes.send : null,
arguments: PaymentRequest.fromUri(launchUri),
);
launchUri = null;
},
isForSetup: false,
isClosable: false,
),
);
} else {
_reset();
auth.close(
route: _isValidPaymentUri() ? Routes.send : null,
arguments: PaymentRequest.fromUri(launchUri),
);
launchUri = null;
}
}
},
);
});
} else if (_isValidPaymentUri()) {
widget.navigatorKey.currentState?.pushNamed(
Routes.send,
arguments: PaymentRequest.fromUri(launchUri),
);
launchUri = null;
}
return WillPopScope(onWillPop: () async => false, child: widget.child);
}
void _reset() {
setState(() {
_postFrameCallback = false;
_setInactive(false);
});
}
void _setInactive(bool value) {
_isInactive = value;
_isInactiveController.add(value);
}
bool _isValidPaymentUri() => launchUri?.path.isNotEmpty ?? false;
}