Skip to content

Commit

Permalink
Merge pull request #124 from osociety/external-link-warning
Browse files Browse the repository at this point in the history
Warn before opening external links
  • Loading branch information
git-elliot authored Mar 17, 2024
2 parents 16b67a7 + dba683d commit 7f16afb
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 33 deletions.
15 changes: 8 additions & 7 deletions lib/api/update_checker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ Future<void> checkForUpdates(
action = SnackBarAction(
label: 'Update',
onPressed: () {
_navigateToStore();
_navigateToStore(context);
},
);
} else {
Expand All @@ -77,14 +77,15 @@ Future<void> checkForUpdates(
}
}

Future<void> _navigateToStore() async {
Future<void> _navigateToStore(BuildContext context) async {
String url = 'https://github.com/git-elliot/vernet/releases/latest';
final isFdroidInstalled = await LaunchApp.isAppInstalled(
androidPackageName: 'org.fdroid.fdroid',
iosUrlScheme: 'fdroid://',
);

if (Platform.isAndroid) {
final isFdroidInstalled = await LaunchApp.isAppInstalled(
androidPackageName: 'org.fdroid.fdroid',
iosUrlScheme: 'fdroid://',
);

if ((await PackageInfo.fromPlatform()).version.contains('store')) {
//Goto playstore
url =
Expand All @@ -99,5 +100,5 @@ Future<void> _navigateToStore() async {
return;
}
}
launchURL(url);
launchURLWithWarning(context, url);
}
11 changes: 11 additions & 0 deletions lib/helper/utils_helper.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
import 'package:flutter/material.dart';
import 'package:url_launcher/url_launcher_string.dart';
import 'package:vernet/ui/external_link_dialog.dart';

Future<void> launchURL(String url) async => await canLaunchUrlString(url)
? await launchUrlString(url)
: throw 'Could not launch $url';

Future<void> launchURLWithWarning(BuildContext context, String url) {
return showDialog(
context: context,
builder: (context) => ExternalLinkWarningDialog(
link: url,
),
);
}
2 changes: 1 addition & 1 deletion lib/pages/home_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ class _WifiDetailState extends State<HomePage> {
const SizedBox(height: 10),
ElevatedButton.icon(
onPressed: () {
launchURL('https://fast.com');
launchURLWithWarning(context, 'https://fast.com');
},
icon: const Icon(Icons.speed),
label: const Text('Speed Test'),
Expand Down
28 changes: 3 additions & 25 deletions lib/pages/settings_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -162,45 +162,23 @@ class _SettingsPageState extends State<SettingsPage> {
ListTile(
leading: const Icon(Icons.bug_report),
title: const Text('Report Issues'),
// subtitle: Text(_issueUrl),
// trailing: IconButton(
// icon: Icon(Icons.open_in_new),
// onPressed: () {
// _launchURL(_issueUrl);
// },
// ),
onTap: () {
launchURL(_issueUrl);
launchURLWithWarning(context, _issueUrl);
},
),
ListTile(
leading: const Icon(Icons.favorite),
title: const Text('Donate'),
// subtitle: Text(_donateUrl),
// trailing: IconButton(
// icon: Icon(Icons.open_in_new),
// onPressed: () {
// _launchURL(_donateUrl);
// },
// ),
onTap: () {
launchURL(_donateUrl);
launchURLWithWarning(context, _donateUrl);
},
),
ListTile(
leading: const Icon(Icons.code),
title: const Text('Source Code'),
// subtitle: Text(_srcUrl),
onTap: () {
launchURL(_srcUrl);
launchURLWithWarning(context, _srcUrl);
},

// trailing: IconButton(
// icon: Icon(Icons.open_in_new),
// onPressed: () {
// _launchURL(_srcUrl);
// },
// ),
),
const ListTile(
title: Text(
Expand Down
31 changes: 31 additions & 0 deletions lib/ui/external_link_dialog.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import 'package:flutter/material.dart';
import 'package:vernet/helper/utils_helper.dart';

class ExternalLinkWarningDialog extends StatelessWidget {
const ExternalLinkWarningDialog({super.key, required this.link});

final String link;

@override
Widget build(BuildContext context) {
return AlertDialog(
title: const Text("Confirm to open external link"),
content: Text(link),
actions: [
TextButton(
onPressed: () {
Navigator.pop(context);
},
child: const Text('Cancel'),
),
TextButton.icon(
onPressed: () {
launchURL(link);
},
icon: const Icon(Icons.link),
label: const Text('Open Link'),
),
],
);
}
}

0 comments on commit 7f16afb

Please sign in to comment.