From 1aa9c08a3e28863c77c76e09d0484b3bcdc2ece4 Mon Sep 17 00:00:00 2001 From: Christian Pauly Date: Sun, 22 Nov 2020 21:51:30 +0100 Subject: [PATCH] refactor: matrix to link prefix --- lib/config/app_config.dart | 1 + lib/utils/url_launcher.dart | 5 +++-- lib/views/chat_details.dart | 15 ++++++--------- lib/views/chat_list.dart | 3 ++- 4 files changed, 12 insertions(+), 12 deletions(-) diff --git a/lib/config/app_config.dart b/lib/config/app_config.dart index 14d7799e..07443e05 100644 --- a/lib/config/app_config.dart +++ b/lib/config/app_config.dart @@ -18,6 +18,7 @@ abstract class AppConfig { static bool renderHtml = false; static bool hideRedactedEvents = false; static bool hideUnknownEvents = false; + static String matrixToLinkPrefix = 'https://matrix.to/#/'; static void loadFromJson(Map json) { if (json['application_name'] is String) { diff --git a/lib/utils/url_launcher.dart b/lib/utils/url_launcher.dart index 7ca7c34f..3875b12e 100644 --- a/lib/utils/url_launcher.dart +++ b/lib/utils/url_launcher.dart @@ -2,6 +2,7 @@ import 'package:adaptive_dialog/adaptive_dialog.dart'; import 'package:famedlysdk/famedlysdk.dart'; import 'package:fluffychat/components/dialogs/simple_dialogs.dart'; import 'package:fluffychat/components/matrix.dart'; +import 'package:fluffychat/config/app_config.dart'; import 'package:fluffychat/utils/app_route.dart'; import 'package:fluffychat/views/chat.dart'; import 'package:flutter/material.dart'; @@ -14,7 +15,7 @@ class UrlLauncher { const UrlLauncher(this.context, this.url); void launchUrl() { - if (url.startsWith('https://matrix.to/#/') || + if (url.startsWith(AppConfig.matrixToLinkPrefix) || {'#', '@', '!', '+', '\$'}.contains(url[0])) { return openMatrixToUrl(); } @@ -23,7 +24,7 @@ class UrlLauncher { void openMatrixToUrl() async { final matrix = Matrix.of(context); - final identifier = url.replaceAll('https://matrix.to/#/', ''); + final identifier = url.replaceAll(AppConfig.matrixToLinkPrefix, ''); if (identifier[0] == '#' || identifier[0] == '!') { // sometimes we have identifiers which have an event id and additional query parameters // we want to separate those. diff --git a/lib/views/chat_details.dart b/lib/views/chat_details.dart index 3b190ee9..a5db8994 100644 --- a/lib/views/chat_details.dart +++ b/lib/views/chat_details.dart @@ -1,4 +1,6 @@ import 'package:adaptive_dialog/adaptive_dialog.dart'; +import 'package:fluffychat/config/app_config.dart'; +import 'package:fluffychat/utils/fluffy_share.dart'; import 'package:flushbar/flushbar_helper.dart'; import 'package:famedlysdk/famedlysdk.dart'; import 'package:famedlysdk/matrix_api.dart'; @@ -16,7 +18,6 @@ import 'package:fluffychat/views/chat_list.dart'; import 'package:fluffychat/views/invitation_selection.dart'; import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; -import 'package:flutter/services.dart'; import 'package:flutter_gen/gen_l10n/l10n.dart'; import 'package:image_picker/image_picker.dart'; import 'package:matrix_link_text/link_text.dart'; @@ -198,14 +199,10 @@ class _ChatDetailsState extends State { if (widget.room.canonicalAlias?.isNotEmpty ?? false) IconButton( icon: Icon(Icons.share), - onPressed: () { - Clipboard.setData( - ClipboardData(text: widget.room.canonicalAlias), - ); - FlushbarHelper.createSuccess( - message: L10n.of(context).copiedToClipboard) - .show(context); - }, + onPressed: () => FluffyShare.share( + AppConfig.matrixToLinkPrefix + + widget.room.canonicalAlias, + context), ), ChatSettingsPopupMenu(widget.room, false) ], diff --git a/lib/views/chat_list.dart b/lib/views/chat_list.dart index 95801b73..3e823011 100644 --- a/lib/views/chat_list.dart +++ b/lib/views/chat_list.dart @@ -7,6 +7,7 @@ import 'package:famedlysdk/matrix_api.dart'; import 'package:fluffychat/components/connection_status_header.dart'; import 'package:fluffychat/components/dialogs/simple_dialogs.dart'; import 'package:fluffychat/components/list_items/public_room_list_item.dart'; +import 'package:fluffychat/config/app_config.dart'; import 'package:fluffychat/utils/fluffy_share.dart'; import 'package:fluffychat/utils/platform_infos.dart'; import 'package:flutter/foundation.dart'; @@ -157,7 +158,7 @@ class _ChatListState extends State { if (Navigator.of(context).canPop()) { Navigator.of(context).popUntil((r) => r.isFirst); } - if (text.startsWith('https://matrix.to/#/')) { + if (text.startsWith(AppConfig.matrixToLinkPrefix)) { UrlLauncher(context, text).openMatrixToUrl(); return; }