lemmur-app-android/lib/url_launcher.dart

127 lines
3.5 KiB
Dart
Raw Normal View History

2020-09-11 23:54:28 +02:00
import 'package:flutter/material.dart';
2022-04-30 16:23:36 +02:00
import 'package:logging/logging.dart';
2020-09-11 23:54:28 +02:00
import 'package:provider/provider.dart';
import 'package:url_launcher/url_launcher.dart' as ul;
2022-04-30 16:23:36 +02:00
import 'l10n/l10n.dart';
import 'pages/community/community.dart';
import 'pages/instance/instance.dart';
import 'pages/media_view.dart';
import 'pages/user.dart';
2020-09-11 23:54:28 +02:00
import 'stores/accounts_store.dart';
import 'util/goto.dart';
2020-09-11 23:54:28 +02:00
2020-09-30 19:05:00 +02:00
/// Decides where does a link link to. Either somewhere in-app:
/// opens the correct page, or outside of the app: opens in a browser
2020-09-12 17:05:52 +02:00
Future<void> linkLauncher({
required BuildContext context,
required String url,
required String instanceHost,
}) async {
void push(Widget Function() builder) {
goTo(context, (c) => builder());
}
2020-09-11 23:54:28 +02:00
final instances = context.read<AccountsStore>().instances;
2020-09-11 23:54:28 +02:00
final chonks = url.split('/');
2022-04-30 16:23:36 +02:00
if (chonks.length == 1) {
await launchLink(link: url, context: context);
return;
}
2020-09-11 23:54:28 +02:00
// CHECK IF LINK TO USER
2020-09-28 19:58:09 +02:00
if (url.startsWith('/u/')) {
return push(() =>
UserPage.fromName(instanceHost: instanceHost, username: chonks[2]));
}
2020-09-11 23:54:28 +02:00
// CHECK IF LINK TO COMMUNITY
2020-09-28 19:58:09 +02:00
if (url.startsWith('/c/')) {
await Navigator.of(context)
.push(CommunityPage.fromNameRoute(instanceHost, chonks[2]));
return;
}
2020-09-11 23:54:28 +02:00
// CHECK IF REDIRECTS TO A PAGE ON ONE OF ADDED INSTANCES
final instanceRegex = RegExp(r'^(?:https?:\/\/)?([\w\.-]+)(.*)$');
2020-09-11 23:54:28 +02:00
final match = instanceRegex.firstMatch(url);
final matchedInstance = match?.group(1);
final rest = match?.group(2);
2020-09-11 23:54:28 +02:00
if (matchedInstance != null && instances.any((e) => e == match?.group(1))) {
if (rest == null || rest.isEmpty || rest == '/') {
return Navigator.of(context).push<void>(InstancePage.route(instanceHost));
}
final split = rest.split('/');
switch (split[1]) {
2020-09-11 23:54:28 +02:00
case 'c':
return goToCommunity.byName(context, matchedInstance, split[2]);
2020-09-11 23:54:28 +02:00
case 'u':
return goToUser.byName(context, matchedInstance, split[2]);
2020-09-11 23:54:28 +02:00
case 'post':
if (split.length == 3) {
return goToPost(context, matchedInstance, int.parse(split[2]));
} else if (split.length == 5) {
// TODO: post with focus on comment thread
return goToPost(context, matchedInstance, int.parse(split[2]));
}
break;
2020-09-11 23:54:28 +02:00
case 'pictrs':
return push(() => MediaViewPage(url));
2020-09-11 23:54:28 +02:00
case 'communities':
// TODO: put here push to communities page
2020-09-11 23:54:28 +02:00
return;
2020-09-11 23:54:28 +02:00
case 'modlog':
// TODO: put here push to modlog
2020-09-11 23:54:28 +02:00
return;
case 'inbox':
// TODO: put here push to inbox
2020-09-11 23:54:28 +02:00
return;
case 'search':
// TODO: *maybe* put here push to search. we'll see
// how much web version differs form the app
return;
2020-09-11 23:54:28 +02:00
case 'create_post':
case 'create_community':
case 'sponsors':
case 'instances':
case 'docs':
break;
2020-09-11 23:54:28 +02:00
default:
break;
2020-09-11 23:54:28 +02:00
}
}
// FALLBACK TO REGULAR LINK STUFF
2020-09-11 23:54:28 +02:00
2022-04-30 16:23:36 +02:00
await launchLink(link: url, context: context);
2020-09-11 23:54:28 +02:00
}
2022-04-30 16:23:36 +02:00
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 {
2022-04-30 16:23:36 +02:00
_logger.warning('Failed to launch a link: $link');
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text(L10n.of(context).cannot_open_in_browser)),
);
return false;
}
}