lemmur-app-android/lib/url_launcher.dart

118 lines
3.1 KiB
Dart
Raw Normal View History

import 'package:flutter/cupertino.dart';
2020-09-11 23:54:28 +02:00
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:url_launcher/url_launcher.dart' as ul;
import 'pages/community.dart';
import 'pages/full_post.dart';
import 'pages/instance.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-12 17:05:52 +02:00
Future<void> linkLauncher({
@required BuildContext context,
@required String url,
@required String instanceUrl,
}) async {
push(Widget Function() builder) {
goTo(context, (c) => builder());
}
2020-09-11 23:54:28 +02:00
final instances = context.read<AccountsStore>().users.keys.toList();
2020-09-11 23:54:28 +02:00
final chonks = url.split('/');
2020-09-11 23:54:28 +02:00
// CHECK IF LINK TO USER
if (chonks[1] == 'u') {
return push(
() => UserPage.fromName(instanceUrl: instanceUrl, username: chonks[2]));
}
2020-09-11 23:54:28 +02:00
// CHECK IF LINK TO COMMUNITY
if (chonks[1] == 'c') {
return push(() => CommunityPage.fromName(
communityName: chonks[2], instanceUrl: instanceUrl));
}
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.isEmpty || rest == '/') {
return push(() => InstancePage(instanceUrl: matchedInstance));
}
final split = rest.split('/');
switch (split[1]) {
2020-09-11 23:54:28 +02:00
case 'c':
return push(() => CommunityPage.fromName(
communityName: split[2], instanceUrl: matchedInstance));
2020-09-11 23:54:28 +02:00
case 'u':
return push(() => UserPage.fromName(
instanceUrl: matchedInstance, username: split[2]));
2020-09-11 23:54:28 +02:00
case 'post':
if (split.length == 3) {
return push(() => FullPostPage(
id: int.parse(split[2]), instanceUrl: matchedInstance));
} else if (split.length == 5) {
// TODO: post with focus on comment thread
2020-09-12 09:44:23 +02:00
print('comment in post');
return;
}
break;
2020-09-11 23:54:28 +02:00
case 'pictrs':
// TODO: put here push to media view
2020-09-12 09:44:23 +02:00
print('pictrs');
2020-09-11 23:54:28 +02:00
return;
2020-09-11 23:54:28 +02:00
case 'communities':
// 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-11 23:54:28 +02:00
case 'modlog':
// TODO: put here push to modlog
2020-09-11 23:54:28 +02:00
print('modlog');
return;
case 'inbox':
// TODO: put here push to inbox
2020-09-11 23:54:28 +02:00
print('inbox');
return;
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':
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
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 {
if (await ul.canLaunch(url)) {
2020-09-12 17:14:08 +02:00
return await ul.launch(url);
} else {
throw Exception();
2020-08-31 19:04:23 +02:00
// TODO: handle opening links to stuff in app
}
}