Update link launcher

This commit is contained in:
shilangyu 2022-04-30 16:23:36 +02:00
parent 816b7d1346
commit 5068eb900f
9 changed files with 59 additions and 53 deletions

View File

@ -1,9 +1,9 @@
import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:lemmy_api_client/v3.dart';
import 'package:url_launcher/url_launcher.dart' as ul;
import '../../hooks/logged_in_action.dart';
import '../../url_launcher.dart';
import '../../util/extensions/api.dart';
import '../../util/mobx_provider.dart';
import '../../util/observer_consumers.dart';
@ -28,10 +28,10 @@ class CommunityMoreMenu extends HookWidget {
ListTile(
leading: const Icon(Icons.open_in_browser),
title: const Text('Open in browser'),
onTap: () async => await ul.canLaunch(communityView.community.actorId)
? ul.launch(communityView.community.actorId)
: ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text("can't open in browser"))),
onTap: () => launchLink(
link: communityView.community.actorId,
context: context,
),
),
ObserverBuilder<CommunityStore>(builder: (context, store) {
return ListTile(

View File

@ -1,9 +1,9 @@
import 'package:flutter/material.dart';
import 'package:lemmy_api_client/v3.dart';
import 'package:url_launcher/url_launcher.dart' as ul;
import '../../l10n/l10n.dart';
import '../../stores/accounts_store.dart';
import '../../url_launcher.dart';
import '../../util/observer_consumers.dart';
import '../../widgets/bottom_modal.dart';
import '../../widgets/info_table_popup.dart';
@ -37,17 +37,7 @@ class InstanceMoreMenu extends StatelessWidget {
ListTile(
leading: const Icon(Icons.open_in_browser),
title: Text(L10n.of(context).open_in_browser),
onTap: () async {
if (await ul.canLaunch(instanceUrl)) {
await ul.launch(instanceUrl);
} else {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(L10n.of(context).cannot_open_in_browser),
),
);
}
},
onTap: () => launchLink(link: instanceUrl, context: context),
),
ListTile(
leading: const Icon(Icons.info_outline),

View File

@ -3,12 +3,12 @@ import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:image_picker/image_picker.dart';
import 'package:lemmy_api_client/pictrs.dart';
import 'package:lemmy_api_client/v3.dart';
import 'package:url_launcher/url_launcher.dart' as ul;
import '../hooks/delayed_loading.dart';
import '../hooks/image_picker.dart';
import '../hooks/stores.dart';
import '../l10n/l10n.dart';
import '../url_launcher.dart';
import '../util/icons.dart';
import '../util/pictrs.dart';
import '../widgets/bottom_modal.dart';
@ -48,13 +48,12 @@ class ManageAccountPage extends HookWidget {
final userProfileUrl =
await userFuture.then((e) => e.person.actorId);
if (await ul.canLaunch(userProfileUrl)) {
await ul.launch(userProfileUrl);
final didLaunch = await launchLink(
link: userProfileUrl,
context: context,
);
if (didLaunch) {
Navigator.of(context).pop();
} else {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text("can't open in browser")),
);
}
},
),

View File

@ -2,13 +2,13 @@ import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:lemmy_api_client/v3.dart';
import 'package:provider/provider.dart';
import 'package:url_launcher/url_launcher.dart' as ul;
import '../../hooks/delayed_loading.dart';
import '../../hooks/stores.dart';
import '../../l10n/l10n.dart';
import '../../stores/accounts_store.dart';
import '../../stores/config_store.dart';
import '../../url_launcher.dart';
import '../../widgets/cached_network_image.dart';
import '../../widgets/fullscreenable_image.dart';
import '../../widgets/radio_picker.dart';
@ -173,8 +173,11 @@ class AddAccountPage extends HookWidget {
),
TextButton(
onPressed: () {
// TODO: extract to LemmyUrls or something
ul.launch('https://${selectedInstance.value}/login');
launchLink(
// TODO: extract to LemmyUrls or something
link: 'https://${selectedInstance.value}/login',
context: context,
);
},
child: const Text('Register'),
),

View File

@ -1 +1,3 @@
const lemmurRepositoryLink = 'https://github.com/LemmurOrg/lemmur';
const lemmurRepositoryUrl = 'https://github.com/LemmurOrg/lemmur';
const patreonUrl = 'https://patreon.com/lemmur';
const buyMeACoffeeUrl = 'https://buymeacoff.ee/lemmur';

View File

@ -1,7 +1,9 @@
import 'package:flutter/material.dart';
import 'package:logging/logging.dart';
import 'package:provider/provider.dart';
import 'package:url_launcher/url_launcher.dart' as ul;
import 'l10n/l10n.dart';
import 'pages/community/community.dart';
import 'pages/instance/instance.dart';
import 'pages/media_view.dart';
@ -23,7 +25,10 @@ Future<void> linkLauncher({
final instances = context.read<AccountsStore>().instances;
final chonks = url.split('/');
if (chonks.length == 1) return openInBrowser(url);
if (chonks.length == 1) {
await launchLink(link: url, context: context);
return;
}
// CHECK IF LINK TO USER
if (url.startsWith('/u/')) {
@ -97,14 +102,25 @@ Future<void> linkLauncher({
// FALLBACK TO REGULAR LINK STUFF
return openInBrowser(url);
await launchLink(link: url, context: context);
}
Future<void> openInBrowser(String url) async {
if (await ul.canLaunch(url)) {
await ul.launch(url);
final _logger = Logger('launchLink');
/// Returns whether launching was successful.
Future<bool> launchLink({
required String link,
required BuildContext context,
}) async {
final uri = Uri.tryParse(link);
if (uri != null && await ul.canLaunchUrl(uri)) {
await ul.launchUrl(uri);
return true;
} else {
throw Exception();
// TODO: handle opening links to stuff in app
_logger.warning('Failed to launch a link: $link');
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text(L10n.of(context).cannot_open_in_browser)),
);
return false;
}
}

View File

@ -43,7 +43,8 @@ class AboutTile extends HookWidget {
TextButton.icon(
icon: const Icon(Icons.code),
label: const Text('source code'),
onPressed: () => openInBrowser(lemmurRepositoryLink),
onPressed: () =>
launchLink(link: lemmurRepositoryUrl, context: context),
),
TextButton.icon(
icon: const Icon(Icons.monetization_on),
@ -56,13 +57,17 @@ class AboutTile extends HookWidget {
mainAxisSize: MainAxisSize.min,
children: [
TextButton(
onPressed: () =>
openInBrowser('https://patreon.com/lemmur'),
onPressed: () => launchLink(
link: patreonUrl,
context: context,
),
child: const Text('Patreon'),
),
TextButton(
onPressed: () =>
openInBrowser('https://buymeacoff.ee/lemmur'),
onPressed: () => launchLink(
link: buyMeACoffeeUrl,
context: context,
),
child: const Text('Buy Me a Coffee'),
),
],

View File

@ -1,10 +1,10 @@
import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:lemmy_api_client/v3.dart';
import 'package:url_launcher/url_launcher.dart' as ul;
import '../../hooks/logged_in_action.dart';
import '../../l10n/l10n.dart';
import '../../url_launcher.dart';
import '../../util/extensions/api.dart';
import '../../util/icons.dart';
import '../../util/observer_consumers.dart';
@ -83,13 +83,7 @@ class _CommentMoreMenuPopup extends HookWidget {
leading: const Icon(Icons.open_in_browser),
title: const Text('Open in browser'),
onTap: () async {
if (await ul.canLaunch(comment.link)) {
await ul.launch(comment.link);
} else {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text("can't open in browser")),
);
}
await launchLink(link: comment.link, context: context);
Navigator.of(context).pop();
},

View File

@ -1,11 +1,11 @@
import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:url_launcher/url_launcher.dart' as ul;
import '../../hooks/logged_in_action.dart';
import '../../pages/create_post.dart';
import '../../pages/full_post/full_post_store.dart';
import '../../stores/accounts_store.dart';
import '../../url_launcher.dart';
import '../../util/icons.dart';
import '../../util/observer_consumers.dart';
import '../bottom_modal.dart';
@ -72,10 +72,7 @@ class PostMoreMenu extends HookWidget {
ListTile(
leading: const Icon(Icons.open_in_browser),
title: const Text('Open in browser'),
onTap: () async => await ul.canLaunch(post.post.apId)
? ul.launch(post.post.apId)
: ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text("can't open in browser"))),
onTap: () => launchLink(link: post.post.apId, context: context),
),
if (isMine)
ListTile(