From 83f4f619524b225288c4bf7549cf3cb8ddef0e7f Mon Sep 17 00:00:00 2001 From: krawieck Date: Fri, 11 Sep 2020 23:54:28 +0200 Subject: [PATCH] baseline for handling a lot of stuff --- lib/url_launcher.dart | 68 +++++++++++++++++++++++++++++++++- lib/widgets/markdown_text.dart | 2 +- lib/widgets/post.dart | 3 +- 3 files changed, 69 insertions(+), 4 deletions(-) diff --git a/lib/url_launcher.dart b/lib/url_launcher.dart index 210fc15..1465601 100644 --- a/lib/url_launcher.dart +++ b/lib/url_launcher.dart @@ -1,6 +1,72 @@ +import 'package:flutter/material.dart'; +import 'package:provider/provider.dart'; import 'package:url_launcher/url_launcher.dart' as ul; -Future urlLauncher(String url) async { +import 'stores/accounts_store.dart'; + +Future urlLauncher(BuildContext c, String url) async { + final instances = c.read().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 { diff --git a/lib/widgets/markdown_text.dart b/lib/widgets/markdown_text.dart index 7ef6878..ef0ebba 100644 --- a/lib/widgets/markdown_text.dart +++ b/lib/widgets/markdown_text.dart @@ -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: [ diff --git a/lib/widgets/post.dart b/lib/widgets/post.dart index bdea2ab..625da16 100644 --- a/lib/widgets/post.dart +++ b/lib/widgets/post.dart @@ -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;