Update link launcher
This commit is contained in:
parent
816b7d1346
commit
5068eb900f
|
@ -1,9 +1,9 @@
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter_hooks/flutter_hooks.dart';
|
import 'package:flutter_hooks/flutter_hooks.dart';
|
||||||
import 'package:lemmy_api_client/v3.dart';
|
import 'package:lemmy_api_client/v3.dart';
|
||||||
import 'package:url_launcher/url_launcher.dart' as ul;
|
|
||||||
|
|
||||||
import '../../hooks/logged_in_action.dart';
|
import '../../hooks/logged_in_action.dart';
|
||||||
|
import '../../url_launcher.dart';
|
||||||
import '../../util/extensions/api.dart';
|
import '../../util/extensions/api.dart';
|
||||||
import '../../util/mobx_provider.dart';
|
import '../../util/mobx_provider.dart';
|
||||||
import '../../util/observer_consumers.dart';
|
import '../../util/observer_consumers.dart';
|
||||||
|
@ -28,10 +28,10 @@ class CommunityMoreMenu extends HookWidget {
|
||||||
ListTile(
|
ListTile(
|
||||||
leading: const Icon(Icons.open_in_browser),
|
leading: const Icon(Icons.open_in_browser),
|
||||||
title: const Text('Open in browser'),
|
title: const Text('Open in browser'),
|
||||||
onTap: () async => await ul.canLaunch(communityView.community.actorId)
|
onTap: () => launchLink(
|
||||||
? ul.launch(communityView.community.actorId)
|
link: communityView.community.actorId,
|
||||||
: ScaffoldMessenger.of(context).showSnackBar(
|
context: context,
|
||||||
const SnackBar(content: Text("can't open in browser"))),
|
),
|
||||||
),
|
),
|
||||||
ObserverBuilder<CommunityStore>(builder: (context, store) {
|
ObserverBuilder<CommunityStore>(builder: (context, store) {
|
||||||
return ListTile(
|
return ListTile(
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:lemmy_api_client/v3.dart';
|
import 'package:lemmy_api_client/v3.dart';
|
||||||
import 'package:url_launcher/url_launcher.dart' as ul;
|
|
||||||
|
|
||||||
import '../../l10n/l10n.dart';
|
import '../../l10n/l10n.dart';
|
||||||
import '../../stores/accounts_store.dart';
|
import '../../stores/accounts_store.dart';
|
||||||
|
import '../../url_launcher.dart';
|
||||||
import '../../util/observer_consumers.dart';
|
import '../../util/observer_consumers.dart';
|
||||||
import '../../widgets/bottom_modal.dart';
|
import '../../widgets/bottom_modal.dart';
|
||||||
import '../../widgets/info_table_popup.dart';
|
import '../../widgets/info_table_popup.dart';
|
||||||
|
@ -37,17 +37,7 @@ class InstanceMoreMenu extends StatelessWidget {
|
||||||
ListTile(
|
ListTile(
|
||||||
leading: const Icon(Icons.open_in_browser),
|
leading: const Icon(Icons.open_in_browser),
|
||||||
title: Text(L10n.of(context).open_in_browser),
|
title: Text(L10n.of(context).open_in_browser),
|
||||||
onTap: () async {
|
onTap: () => launchLink(link: instanceUrl, context: context),
|
||||||
if (await ul.canLaunch(instanceUrl)) {
|
|
||||||
await ul.launch(instanceUrl);
|
|
||||||
} else {
|
|
||||||
ScaffoldMessenger.of(context).showSnackBar(
|
|
||||||
SnackBar(
|
|
||||||
content: Text(L10n.of(context).cannot_open_in_browser),
|
|
||||||
),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
),
|
),
|
||||||
ListTile(
|
ListTile(
|
||||||
leading: const Icon(Icons.info_outline),
|
leading: const Icon(Icons.info_outline),
|
||||||
|
|
|
@ -3,12 +3,12 @@ import 'package:flutter_hooks/flutter_hooks.dart';
|
||||||
import 'package:image_picker/image_picker.dart';
|
import 'package:image_picker/image_picker.dart';
|
||||||
import 'package:lemmy_api_client/pictrs.dart';
|
import 'package:lemmy_api_client/pictrs.dart';
|
||||||
import 'package:lemmy_api_client/v3.dart';
|
import 'package:lemmy_api_client/v3.dart';
|
||||||
import 'package:url_launcher/url_launcher.dart' as ul;
|
|
||||||
|
|
||||||
import '../hooks/delayed_loading.dart';
|
import '../hooks/delayed_loading.dart';
|
||||||
import '../hooks/image_picker.dart';
|
import '../hooks/image_picker.dart';
|
||||||
import '../hooks/stores.dart';
|
import '../hooks/stores.dart';
|
||||||
import '../l10n/l10n.dart';
|
import '../l10n/l10n.dart';
|
||||||
|
import '../url_launcher.dart';
|
||||||
import '../util/icons.dart';
|
import '../util/icons.dart';
|
||||||
import '../util/pictrs.dart';
|
import '../util/pictrs.dart';
|
||||||
import '../widgets/bottom_modal.dart';
|
import '../widgets/bottom_modal.dart';
|
||||||
|
@ -48,13 +48,12 @@ class ManageAccountPage extends HookWidget {
|
||||||
final userProfileUrl =
|
final userProfileUrl =
|
||||||
await userFuture.then((e) => e.person.actorId);
|
await userFuture.then((e) => e.person.actorId);
|
||||||
|
|
||||||
if (await ul.canLaunch(userProfileUrl)) {
|
final didLaunch = await launchLink(
|
||||||
await ul.launch(userProfileUrl);
|
link: userProfileUrl,
|
||||||
|
context: context,
|
||||||
|
);
|
||||||
|
if (didLaunch) {
|
||||||
Navigator.of(context).pop();
|
Navigator.of(context).pop();
|
||||||
} else {
|
|
||||||
ScaffoldMessenger.of(context).showSnackBar(
|
|
||||||
const SnackBar(content: Text("can't open in browser")),
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
|
|
|
@ -2,13 +2,13 @@ import 'package:flutter/material.dart';
|
||||||
import 'package:flutter_hooks/flutter_hooks.dart';
|
import 'package:flutter_hooks/flutter_hooks.dart';
|
||||||
import 'package:lemmy_api_client/v3.dart';
|
import 'package:lemmy_api_client/v3.dart';
|
||||||
import 'package:provider/provider.dart';
|
import 'package:provider/provider.dart';
|
||||||
import 'package:url_launcher/url_launcher.dart' as ul;
|
|
||||||
|
|
||||||
import '../../hooks/delayed_loading.dart';
|
import '../../hooks/delayed_loading.dart';
|
||||||
import '../../hooks/stores.dart';
|
import '../../hooks/stores.dart';
|
||||||
import '../../l10n/l10n.dart';
|
import '../../l10n/l10n.dart';
|
||||||
import '../../stores/accounts_store.dart';
|
import '../../stores/accounts_store.dart';
|
||||||
import '../../stores/config_store.dart';
|
import '../../stores/config_store.dart';
|
||||||
|
import '../../url_launcher.dart';
|
||||||
import '../../widgets/cached_network_image.dart';
|
import '../../widgets/cached_network_image.dart';
|
||||||
import '../../widgets/fullscreenable_image.dart';
|
import '../../widgets/fullscreenable_image.dart';
|
||||||
import '../../widgets/radio_picker.dart';
|
import '../../widgets/radio_picker.dart';
|
||||||
|
@ -173,8 +173,11 @@ class AddAccountPage extends HookWidget {
|
||||||
),
|
),
|
||||||
TextButton(
|
TextButton(
|
||||||
onPressed: () {
|
onPressed: () {
|
||||||
// TODO: extract to LemmyUrls or something
|
launchLink(
|
||||||
ul.launch('https://${selectedInstance.value}/login');
|
// TODO: extract to LemmyUrls or something
|
||||||
|
link: 'https://${selectedInstance.value}/login',
|
||||||
|
context: context,
|
||||||
|
);
|
||||||
},
|
},
|
||||||
child: const Text('Register'),
|
child: const Text('Register'),
|
||||||
),
|
),
|
||||||
|
|
|
@ -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';
|
||||||
|
|
|
@ -1,7 +1,9 @@
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:logging/logging.dart';
|
||||||
import 'package:provider/provider.dart';
|
import 'package:provider/provider.dart';
|
||||||
import 'package:url_launcher/url_launcher.dart' as ul;
|
import 'package:url_launcher/url_launcher.dart' as ul;
|
||||||
|
|
||||||
|
import 'l10n/l10n.dart';
|
||||||
import 'pages/community/community.dart';
|
import 'pages/community/community.dart';
|
||||||
import 'pages/instance/instance.dart';
|
import 'pages/instance/instance.dart';
|
||||||
import 'pages/media_view.dart';
|
import 'pages/media_view.dart';
|
||||||
|
@ -23,7 +25,10 @@ Future<void> linkLauncher({
|
||||||
final instances = context.read<AccountsStore>().instances;
|
final instances = context.read<AccountsStore>().instances;
|
||||||
|
|
||||||
final chonks = url.split('/');
|
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
|
// CHECK IF LINK TO USER
|
||||||
if (url.startsWith('/u/')) {
|
if (url.startsWith('/u/')) {
|
||||||
|
@ -97,14 +102,25 @@ Future<void> linkLauncher({
|
||||||
|
|
||||||
// FALLBACK TO REGULAR LINK STUFF
|
// FALLBACK TO REGULAR LINK STUFF
|
||||||
|
|
||||||
return openInBrowser(url);
|
await launchLink(link: url, context: context);
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> openInBrowser(String url) async {
|
final _logger = Logger('launchLink');
|
||||||
if (await ul.canLaunch(url)) {
|
|
||||||
await ul.launch(url);
|
/// 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 {
|
} else {
|
||||||
throw Exception();
|
_logger.warning('Failed to launch a link: $link');
|
||||||
// TODO: handle opening links to stuff in app
|
ScaffoldMessenger.of(context).showSnackBar(
|
||||||
|
SnackBar(content: Text(L10n.of(context).cannot_open_in_browser)),
|
||||||
|
);
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -43,7 +43,8 @@ class AboutTile extends HookWidget {
|
||||||
TextButton.icon(
|
TextButton.icon(
|
||||||
icon: const Icon(Icons.code),
|
icon: const Icon(Icons.code),
|
||||||
label: const Text('source code'),
|
label: const Text('source code'),
|
||||||
onPressed: () => openInBrowser(lemmurRepositoryLink),
|
onPressed: () =>
|
||||||
|
launchLink(link: lemmurRepositoryUrl, context: context),
|
||||||
),
|
),
|
||||||
TextButton.icon(
|
TextButton.icon(
|
||||||
icon: const Icon(Icons.monetization_on),
|
icon: const Icon(Icons.monetization_on),
|
||||||
|
@ -56,13 +57,17 @@ class AboutTile extends HookWidget {
|
||||||
mainAxisSize: MainAxisSize.min,
|
mainAxisSize: MainAxisSize.min,
|
||||||
children: [
|
children: [
|
||||||
TextButton(
|
TextButton(
|
||||||
onPressed: () =>
|
onPressed: () => launchLink(
|
||||||
openInBrowser('https://patreon.com/lemmur'),
|
link: patreonUrl,
|
||||||
|
context: context,
|
||||||
|
),
|
||||||
child: const Text('Patreon'),
|
child: const Text('Patreon'),
|
||||||
),
|
),
|
||||||
TextButton(
|
TextButton(
|
||||||
onPressed: () =>
|
onPressed: () => launchLink(
|
||||||
openInBrowser('https://buymeacoff.ee/lemmur'),
|
link: buyMeACoffeeUrl,
|
||||||
|
context: context,
|
||||||
|
),
|
||||||
child: const Text('Buy Me a Coffee'),
|
child: const Text('Buy Me a Coffee'),
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
|
|
|
@ -1,10 +1,10 @@
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter_hooks/flutter_hooks.dart';
|
import 'package:flutter_hooks/flutter_hooks.dart';
|
||||||
import 'package:lemmy_api_client/v3.dart';
|
import 'package:lemmy_api_client/v3.dart';
|
||||||
import 'package:url_launcher/url_launcher.dart' as ul;
|
|
||||||
|
|
||||||
import '../../hooks/logged_in_action.dart';
|
import '../../hooks/logged_in_action.dart';
|
||||||
import '../../l10n/l10n.dart';
|
import '../../l10n/l10n.dart';
|
||||||
|
import '../../url_launcher.dart';
|
||||||
import '../../util/extensions/api.dart';
|
import '../../util/extensions/api.dart';
|
||||||
import '../../util/icons.dart';
|
import '../../util/icons.dart';
|
||||||
import '../../util/observer_consumers.dart';
|
import '../../util/observer_consumers.dart';
|
||||||
|
@ -83,13 +83,7 @@ class _CommentMoreMenuPopup extends HookWidget {
|
||||||
leading: const Icon(Icons.open_in_browser),
|
leading: const Icon(Icons.open_in_browser),
|
||||||
title: const Text('Open in browser'),
|
title: const Text('Open in browser'),
|
||||||
onTap: () async {
|
onTap: () async {
|
||||||
if (await ul.canLaunch(comment.link)) {
|
await launchLink(link: comment.link, context: context);
|
||||||
await ul.launch(comment.link);
|
|
||||||
} else {
|
|
||||||
ScaffoldMessenger.of(context).showSnackBar(
|
|
||||||
const SnackBar(content: Text("can't open in browser")),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
Navigator.of(context).pop();
|
Navigator.of(context).pop();
|
||||||
},
|
},
|
||||||
|
|
|
@ -1,11 +1,11 @@
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter_hooks/flutter_hooks.dart';
|
import 'package:flutter_hooks/flutter_hooks.dart';
|
||||||
import 'package:url_launcher/url_launcher.dart' as ul;
|
|
||||||
|
|
||||||
import '../../hooks/logged_in_action.dart';
|
import '../../hooks/logged_in_action.dart';
|
||||||
import '../../pages/create_post.dart';
|
import '../../pages/create_post.dart';
|
||||||
import '../../pages/full_post/full_post_store.dart';
|
import '../../pages/full_post/full_post_store.dart';
|
||||||
import '../../stores/accounts_store.dart';
|
import '../../stores/accounts_store.dart';
|
||||||
|
import '../../url_launcher.dart';
|
||||||
import '../../util/icons.dart';
|
import '../../util/icons.dart';
|
||||||
import '../../util/observer_consumers.dart';
|
import '../../util/observer_consumers.dart';
|
||||||
import '../bottom_modal.dart';
|
import '../bottom_modal.dart';
|
||||||
|
@ -72,10 +72,7 @@ class PostMoreMenu extends HookWidget {
|
||||||
ListTile(
|
ListTile(
|
||||||
leading: const Icon(Icons.open_in_browser),
|
leading: const Icon(Icons.open_in_browser),
|
||||||
title: const Text('Open in browser'),
|
title: const Text('Open in browser'),
|
||||||
onTap: () async => await ul.canLaunch(post.post.apId)
|
onTap: () => launchLink(link: post.post.apId, context: context),
|
||||||
? ul.launch(post.post.apId)
|
|
||||||
: ScaffoldMessenger.of(context).showSnackBar(
|
|
||||||
const SnackBar(content: Text("can't open in browser"))),
|
|
||||||
),
|
),
|
||||||
if (isMine)
|
if (isMine)
|
||||||
ListTile(
|
ListTile(
|
||||||
|
|
Loading…
Reference in New Issue