2020-09-16 22:53:04 +02:00
|
|
|
import 'package:flutter/cupertino.dart';
|
2020-09-11 23:54:28 +02:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:provider/provider.dart';
|
2020-08-30 16:49:59 +02:00
|
|
|
import 'package:url_launcher/url_launcher.dart' as ul;
|
|
|
|
|
2020-09-12 00:46:16 +02:00
|
|
|
import 'pages/community.dart';
|
2020-09-12 10:23:39 +02:00
|
|
|
import 'pages/instance.dart';
|
2020-09-28 20:04:44 +02:00
|
|
|
import 'pages/media_view.dart';
|
2020-09-12 00:46:16 +02:00
|
|
|
import 'pages/user.dart';
|
2020-09-11 23:54:28 +02:00
|
|
|
import 'stores/accounts_store.dart';
|
2020-09-16 22:53:04 +02:00
|
|
|
import 'util/goto.dart';
|
2020-09-11 23:54:28 +02:00
|
|
|
|
2020-09-12 17:05:52 +02:00
|
|
|
Future<void> linkLauncher({
|
2020-09-12 00:46:16 +02:00
|
|
|
@required BuildContext context,
|
|
|
|
@required String url,
|
|
|
|
@required String instanceUrl,
|
|
|
|
}) async {
|
2020-09-12 10:25:22 +02:00
|
|
|
push(Widget Function() builder) {
|
2020-09-16 22:53:04 +02:00
|
|
|
goTo(context, (c) => builder());
|
2020-09-12 00:46:16 +02:00
|
|
|
}
|
2020-09-11 23:54:28 +02:00
|
|
|
|
2020-09-29 23:15:51 +02:00
|
|
|
final instances = context.read<AccountsStore>().instances;
|
2020-09-11 23:54:28 +02:00
|
|
|
|
2020-09-12 00:46:16 +02:00
|
|
|
final chonks = url.split('/');
|
2020-09-28 19:58:09 +02:00
|
|
|
if (chonks.length == 1) return openInBrowser(url);
|
2020-09-11 23:54:28 +02:00
|
|
|
|
2020-09-12 00:46:16 +02:00
|
|
|
// CHECK IF LINK TO USER
|
2020-09-28 19:58:09 +02:00
|
|
|
if (url.startsWith('/u/')) {
|
2020-09-12 10:25:22 +02:00
|
|
|
return push(
|
|
|
|
() => UserPage.fromName(instanceUrl: instanceUrl, username: chonks[2]));
|
2020-09-12 00:46:16 +02:00
|
|
|
}
|
2020-09-11 23:54:28 +02:00
|
|
|
|
2020-09-12 00:46:16 +02:00
|
|
|
// CHECK IF LINK TO COMMUNITY
|
2020-09-28 19:58:09 +02:00
|
|
|
if (url.startsWith('/c/')) {
|
2020-09-12 10:25:22 +02:00
|
|
|
return push(() => CommunityPage.fromName(
|
2020-09-12 00:46:16 +02:00
|
|
|
communityName: chonks[2], instanceUrl: instanceUrl));
|
|
|
|
}
|
2020-09-11 23:54:28 +02:00
|
|
|
|
|
|
|
// CHECK IF REDIRECTS TO A PAGE ON ONE OF ADDED INSTANCES
|
|
|
|
|
2020-09-12 10:23:39 +02:00
|
|
|
final instanceRegex = RegExp(r'^(?:https?:\/\/)?([\w\.-]+)(.*)$');
|
2020-09-11 23:54:28 +02:00
|
|
|
final match = instanceRegex.firstMatch(url);
|
|
|
|
|
2020-09-12 10:23:39 +02:00
|
|
|
final matchedInstance = match?.group(1);
|
|
|
|
final rest = match?.group(2);
|
2020-09-11 23:54:28 +02:00
|
|
|
|
2020-09-12 10:23:39 +02:00
|
|
|
if (matchedInstance != null && instances.any((e) => e == match.group(1))) {
|
|
|
|
if (rest.isEmpty || rest == '/') {
|
2020-09-12 10:25:22 +02:00
|
|
|
return push(() => InstancePage(instanceUrl: matchedInstance));
|
2020-09-12 10:23:39 +02:00
|
|
|
}
|
2020-09-12 00:26:54 +02:00
|
|
|
final split = rest.split('/');
|
|
|
|
switch (split[1]) {
|
2020-09-11 23:54:28 +02:00
|
|
|
case 'c':
|
2020-09-28 20:04:44 +02:00
|
|
|
return goToCommunity.byName(context, matchedInstance, split[2]);
|
2020-09-12 00:26:54 +02:00
|
|
|
|
2020-09-11 23:54:28 +02:00
|
|
|
case 'u':
|
2020-09-28 20:04:44 +02:00
|
|
|
return goToUser.byName(context, matchedInstance, split[2]);
|
2020-09-12 00:26:54 +02:00
|
|
|
|
2020-09-11 23:54:28 +02:00
|
|
|
case 'post':
|
2020-09-12 09:40:47 +02:00
|
|
|
if (split.length == 3) {
|
2020-09-28 20:04:44 +02:00
|
|
|
return goToPost(context, matchedInstance, int.parse(split[2]));
|
2020-09-12 09:40:47 +02:00
|
|
|
} else if (split.length == 5) {
|
|
|
|
// TODO: post with focus on comment thread
|
2020-09-12 09:44:23 +02:00
|
|
|
print('comment in post');
|
2020-09-28 20:04:44 +02:00
|
|
|
return goToPost(context, matchedInstance, int.parse(split[2]));
|
2020-09-12 09:40:47 +02:00
|
|
|
}
|
|
|
|
break;
|
2020-09-12 00:26:54 +02:00
|
|
|
|
2020-09-11 23:54:28 +02:00
|
|
|
case 'pictrs':
|
2020-09-28 20:04:44 +02:00
|
|
|
return push(() => MediaViewPage(url));
|
2020-09-12 00:26:54 +02:00
|
|
|
|
2020-09-11 23:54:28 +02:00
|
|
|
case 'communities':
|
2020-09-12 00:26:54 +02:00
|
|
|
// TODO: put here push to communities page
|
2020-09-12 09:44:23 +02:00
|
|
|
print('communities');
|
2020-09-11 23:54:28 +02:00
|
|
|
return;
|
2020-09-12 00:26:54 +02:00
|
|
|
|
2020-09-11 23:54:28 +02:00
|
|
|
case 'modlog':
|
2020-09-12 00:26:54 +02:00
|
|
|
// TODO: put here push to modlog
|
2020-09-11 23:54:28 +02:00
|
|
|
print('modlog');
|
|
|
|
return;
|
|
|
|
case 'inbox':
|
2020-09-12 00:26:54 +02:00
|
|
|
// TODO: put here push to inbox
|
2020-09-11 23:54:28 +02:00
|
|
|
print('inbox');
|
|
|
|
return;
|
2020-09-12 00:26:54 +02:00
|
|
|
case 'search':
|
|
|
|
// TODO: *maybe* put here push to search. we'll see
|
|
|
|
// how much web version differs form the app
|
|
|
|
print('search');
|
|
|
|
return;
|
2020-09-11 23:54:28 +02:00
|
|
|
case 'create_post':
|
|
|
|
case 'create_community':
|
|
|
|
case 'sponsors':
|
|
|
|
case 'instances':
|
|
|
|
case 'docs':
|
2020-09-12 10:23:39 +02:00
|
|
|
break;
|
2020-09-11 23:54:28 +02:00
|
|
|
default:
|
2020-09-12 10:23:39 +02:00
|
|
|
break;
|
2020-09-11 23:54:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-12 00:46:16 +02:00
|
|
|
// FALLBACK TO REGULAR LINK STUFF
|
2020-09-11 23:54:28 +02:00
|
|
|
|
2020-09-12 17:14:08 +02:00
|
|
|
return openInBrowser(url);
|
2020-09-11 23:54:28 +02:00
|
|
|
}
|
|
|
|
|
2020-09-12 17:14:08 +02:00
|
|
|
Future<void> openInBrowser(String url) async {
|
2020-08-30 16:49:59 +02:00
|
|
|
if (await ul.canLaunch(url)) {
|
2020-09-12 17:14:08 +02:00
|
|
|
return await ul.launch(url);
|
2020-08-30 16:49:59 +02:00
|
|
|
} else {
|
|
|
|
throw Exception();
|
2020-08-31 19:04:23 +02:00
|
|
|
// TODO: handle opening links to stuff in app
|
2020-08-30 16:49:59 +02:00
|
|
|
}
|
|
|
|
}
|