Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Fix external links #5754

Merged
merged 3 commits into from
Oct 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions packages/smooth_app/lib/helpers/launch_url_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> launchURL(String url) async {
static Future<void> 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';
Expand Down
21 changes: 15 additions & 6 deletions packages/smooth_app/lib/pages/navigator/app_navigator.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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;
}
}
}

Expand Down Expand Up @@ -242,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']!),
);
},
),
],
Expand Down Expand Up @@ -468,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)}';
}
16 changes: 14 additions & 2 deletions packages/smooth_app/lib/pages/navigator/external_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ 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;
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!
///
Expand Down Expand Up @@ -62,6 +64,7 @@ class _ExternalPageState extends State<ExternalPage> {

try {
if (Platform.isAndroid) {
/// Custom tabs
WidgetsFlutterBinding.ensureInitialized();
await tabs.launchUrl(
Uri.parse(url),
Expand All @@ -70,13 +73,22 @@ class _ExternalPageState extends State<ExternalPage> {
),
);
} 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);
} finally {
if (mounted) {
AppNavigator.of(context).pop();
final bool success = AppNavigator.of(context).pop();
if (!success) {
/// This page was called with the go() without an history
/// (mainly for an external deep link)
GoRouter.of(context).go('/');
}
}
}
});
Expand Down
Loading