baseline for handling a lot of stuff

This commit is contained in:
krawieck 2020-09-11 23:54:28 +02:00
parent c5e90346fe
commit 83f4f61952
3 changed files with 69 additions and 4 deletions

View File

@ -1,6 +1,72 @@
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:url_launcher/url_launcher.dart' as ul;
Future<void> urlLauncher(String url) async {
import 'stores/accounts_store.dart';
Future<void> urlLauncher(BuildContext c, String url) async {
final instances = c.read<AccountsStore>().users.keys.toList();
print(instances);
// CHECK IF LINK TO USER
// TODO: link to user
// CHECK IF LINK TO COMMUNITY
// TODO; link to community
// CHECK IF REDIRECTS TO A PAGE ON ONE OF ADDED INSTANCES
final instanceRegex = RegExp(r'^(?:https?:\/\/)?([\w\.-]+)(.+)$');
final match = instanceRegex.firstMatch(url);
final matchedDomain = match.group(1);
final rest = match.group(2);
print('matched domain: $matchedDomain, rest: $rest');
print(rest.length);
if (instances.any((e) => e == match.group(1))) {
switch (rest.split('/')[1]) {
case 'c':
print('comunity');
return;
case 'u':
print('user');
return;
case 'post':
print('post');
return;
case 'pictrs':
print('pictures');
return;
case 'communities':
print('communities');
return;
case 'modlog':
print('modlog');
return;
case 'inbox':
print('inbox');
return;
case 'create_post':
case 'create_community':
case 'sponsors':
case 'instances':
case 'docs':
openInBrowser(url);
return;
default:
}
}
// REGULAR LINK STUFF
openInBrowser(url);
}
void openInBrowser(String url) async {
if (await ul.canLaunch(url)) {
await ul.launch(url);
} else {

View File

@ -14,7 +14,7 @@ class MarkdownText extends StatelessWidget {
data: text,
extensionSet: md.ExtensionSet.gitHubWeb,
onTapLink: (href) {
urlLauncher(href)
urlLauncher(context, href)
.catchError((e) => Scaffold.of(context).showSnackBar(SnackBar(
content: Row(
children: [

View File

@ -40,8 +40,6 @@ class Post extends StatelessWidget {
// == ACTIONS ==
void _openLink() => urlLauncher(post.url);
void _openFullImage() {
// TODO: fullscreen media view
print('OPEN FULL IMAGE');
@ -146,6 +144,7 @@ class Post extends StatelessWidget {
@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
void _openLink() => urlLauncher(context, post.url);
final urlDomain = () {
if (post.url == null) return null;