From fa2b94dac2f241065aefc8da94d83027ef324d4a Mon Sep 17 00:00:00 2001 From: Edouard Marquez Date: Sun, 27 Oct 2024 18:22:14 +0100 Subject: [PATCH 1/3] Fix: Donate link --- .../smooth_app/lib/helpers/launch_url_helper.dart | 14 +++++++++----- .../lib/pages/navigator/app_navigator.dart | 11 +++++++++-- .../lib/pages/navigator/external_page.dart | 8 +++++++- 3 files changed, 25 insertions(+), 8 deletions(-) diff --git a/packages/smooth_app/lib/helpers/launch_url_helper.dart b/packages/smooth_app/lib/helpers/launch_url_helper.dart index 42edfe2d2e3..f4d7dd276a2 100644 --- a/packages/smooth_app/lib/helpers/launch_url_helper.dart +++ b/packages/smooth_app/lib/helpers/launch_url_helper.dart @@ -18,22 +18,26 @@ class LaunchUrlHelper { 'http(s)?://[a-z]*.open(food|beauty|products|petfood)facts.(net|org)', ))) { AnalyticsHelper.trackOutlink(url: url); - GoRouter.of(context).go(url); + GoRouter.of(context).push(url); } else { return launchURL(url); } } /// Launches the url in an external browser. - static Future launchURL(String url) async { + static Future launchURL( + String url, { + LaunchMode? mode, + }) async { AnalyticsHelper.trackOutlink(url: url); try { await launchUrl( Uri.parse(url), - mode: Platform.isAndroid - ? LaunchMode.externalApplication - : LaunchMode.platformDefault, + mode: mode ?? + (Platform.isAndroid + ? LaunchMode.externalApplication + : LaunchMode.platformDefault), ); } catch (e) { throw 'Could not launch $url,Error: $e'; diff --git a/packages/smooth_app/lib/pages/navigator/app_navigator.dart b/packages/smooth_app/lib/pages/navigator/app_navigator.dart index 41696039971..1400ff63e78 100644 --- a/packages/smooth_app/lib/pages/navigator/app_navigator.dart +++ b/packages/smooth_app/lib/pages/navigator/app_navigator.dart @@ -79,8 +79,15 @@ class AppNavigator extends InheritedWidget { } } - void pop([dynamic result]) { - _router.router.pop(result); + /// Returns [true] if the pop was successful + /// Returns [false] if there is nothing to pop (= no history) + bool pop([dynamic result]) { + try { + _router.router.pop(result); + return true; + } on GoError catch (_) { + return false; + } } } diff --git a/packages/smooth_app/lib/pages/navigator/external_page.dart b/packages/smooth_app/lib/pages/navigator/external_page.dart index ed725da3de4..89d36912f6a 100644 --- a/packages/smooth_app/lib/pages/navigator/external_page.dart +++ b/packages/smooth_app/lib/pages/navigator/external_page.dart @@ -9,6 +9,7 @@ import 'package:smooth_app/helpers/launch_url_helper.dart'; import 'package:smooth_app/pages/navigator/app_navigator.dart'; import 'package:smooth_app/query/product_query.dart'; import 'package:smooth_app/services/smooth_services.dart'; +import 'package:url_launcher/url_launcher.dart'; /// This screen is only used for deep links! /// @@ -62,6 +63,7 @@ class _ExternalPageState extends State { try { if (Platform.isAndroid) { + /// Custom tabs WidgetsFlutterBinding.ensureInitialized(); await tabs.launchUrl( Uri.parse(url), @@ -70,7 +72,11 @@ class _ExternalPageState extends State { ), ); } else { - await LaunchUrlHelper.launchURL(url); + /// The default browser + await LaunchUrlHelper.launchURL( + url, + mode: LaunchMode.externalApplication, + ); } } catch (e) { Logs.e('Unable to open an external link', ex: e); From a293146a6e7d0018d73390ee313b25bec3f3b461 Mon Sep 17 00:00:00 2001 From: Edouard Marquez Date: Sun, 27 Oct 2024 18:44:03 +0100 Subject: [PATCH 2/3] Better handle external links --- .../smooth_app/lib/pages/navigator/app_navigator.dart | 10 ++++++---- .../smooth_app/lib/pages/navigator/external_page.dart | 8 +++++++- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/packages/smooth_app/lib/pages/navigator/app_navigator.dart b/packages/smooth_app/lib/pages/navigator/app_navigator.dart index 1400ff63e78..b3c0164bdf2 100644 --- a/packages/smooth_app/lib/pages/navigator/app_navigator.dart +++ b/packages/smooth_app/lib/pages/navigator/app_navigator.dart @@ -1,5 +1,5 @@ import 'package:collection/collection.dart'; -import 'package:flutter/widgets.dart'; +import 'package:flutter/material.dart'; import 'package:go_router/go_router.dart'; import 'package:openfoodfacts/openfoodfacts.dart'; import 'package:provider/provider.dart'; @@ -249,9 +249,11 @@ class _SmoothGoRouter { ], ), GoRoute( - path: '/${_InternalAppRoutes.EXTERNAL_PAGE}/:page', + path: '/${_InternalAppRoutes.EXTERNAL_PAGE}', builder: (BuildContext context, GoRouterState state) { - return ExternalPage(path: state.pathParameters['page']!); + return ExternalPage( + path: Uri.decodeFull(state.uri.queryParameters['path']!), + ); }, ), ], @@ -475,5 +477,5 @@ class AppRoutes { // Open an external link (where path is relative to the OFF website) static String EXTERNAL(String path) => - '/${_InternalAppRoutes.EXTERNAL_PAGE}/$path'; + '/${_InternalAppRoutes.EXTERNAL_PAGE}?path=${Uri.encodeFull(path)}'; } diff --git a/packages/smooth_app/lib/pages/navigator/external_page.dart b/packages/smooth_app/lib/pages/navigator/external_page.dart index 89d36912f6a..c74cfe04ebf 100644 --- a/packages/smooth_app/lib/pages/navigator/external_page.dart +++ b/packages/smooth_app/lib/pages/navigator/external_page.dart @@ -2,6 +2,7 @@ import 'dart:io'; import 'package:flutter/material.dart'; import 'package:flutter_custom_tabs/flutter_custom_tabs.dart' as tabs; +import 'package:go_router/go_router.dart'; import 'package:http/http.dart' as http; import 'package:openfoodfacts/openfoodfacts.dart'; import 'package:path/path.dart' as path; @@ -82,7 +83,12 @@ class _ExternalPageState extends State { Logs.e('Unable to open an external link', ex: e); } finally { if (mounted) { - AppNavigator.of(context).pop(); + final bool success = AppNavigator.of(context).pop(); + if (!success) { + /// This page was called with go() without an history + /// (mainly for an external deep link) + GoRouter.of(context).go('/'); + } } } }); From 8f46a08d91c1b028a134dc5de8dcef1eb5c71160 Mon Sep 17 00:00:00 2001 From: Edouard Marquez Date: Sun, 27 Oct 2024 18:49:48 +0100 Subject: [PATCH 3/3] Typo --- packages/smooth_app/lib/pages/navigator/external_page.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/smooth_app/lib/pages/navigator/external_page.dart b/packages/smooth_app/lib/pages/navigator/external_page.dart index c74cfe04ebf..1cab8d78b93 100644 --- a/packages/smooth_app/lib/pages/navigator/external_page.dart +++ b/packages/smooth_app/lib/pages/navigator/external_page.dart @@ -85,7 +85,7 @@ class _ExternalPageState extends State { if (mounted) { final bool success = AppNavigator.of(context).pop(); if (!success) { - /// This page was called with go() without an history + /// This page was called with the go() without an history /// (mainly for an external deep link) GoRouter.of(context).go('/'); }