Move handling deep links to app root

This commit is contained in:
OmarHatem 2022-11-08 18:12:01 +02:00
parent f078457c7b
commit f68798dd19
2 changed files with 38 additions and 45 deletions

View file

@ -5,7 +5,6 @@ import 'package:cake_wallet/buy/order.dart';
import 'package:cake_wallet/ionia/ionia_category.dart';
import 'package:cake_wallet/ionia/ionia_merchant.dart';
import 'package:cake_wallet/store/yat/yat_store.dart';
import 'package:cake_wallet/utils/payment_request.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
@ -39,8 +38,6 @@ import 'package:cw_core/unspent_coins_info.dart';
import 'package:cake_wallet/monero/monero.dart';
import 'package:cake_wallet/wallet_type_utils.dart';
import 'src/screens/auth/auth_page.dart';
final navigatorKey = GlobalKey<NavigatorState>();
final rootKey = GlobalKey<RootState>();
@ -205,47 +202,6 @@ class AppState extends State<App> with SingleTickerProviderStateMixin {
super.initState();
//_handleIncomingLinks();
//_handleInitialUri();
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);
});
final Uri? initialUri = await getInitialUri();
handleDeepLinking(initialUri);
} catch (e) {
print(e);
}
}
void handleDeepLinking(Uri? uri) {
if (uri == null || !mounted) return;
Navigator.of(navigatorKey.currentContext!).pushNamed(Routes.auth,
arguments: (bool isAuthenticatedSuccessfully, AuthPageState auth) {
if (isAuthenticatedSuccessfully) {
auth.close(route: Routes.send, arguments: PaymentRequest.fromUri(uri));
}
});
// Navigator.pushNamed(
// navigatorKey.currentContext!,
// Routes.send,
// arguments: PaymentRequest.fromUri(uri),
// );
}
Future<void> _handleInitialUri() async {

View file

@ -1,10 +1,12 @@
import 'dart:async';
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';
class Root extends StatefulWidget {
Root(
@ -35,6 +37,9 @@ class RootState extends State<Root> with WidgetsBindingObserver {
bool _isInactive;
bool _postFrameCallback;
StreamSubscription<Uri?>? stream;
Uri? launchUri;
@override
void initState() {
_isInactiveController = StreamController<bool>.broadcast();
@ -42,6 +47,34 @@ class RootState extends State<Root> with WidgetsBindingObserver {
_postFrameCallback = false;
WidgetsBinding.instance.addObserver(this);
super.initState();
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
@ -75,7 +108,11 @@ class RootState extends State<Root> with WidgetsBindingObserver {
}
_reset();
auth.close();
auth.close(
route: launchUri != null ? Routes.send : null,
arguments: PaymentRequest.fromUri(launchUri),
);
launchUri = null;
});
});
}