From 6e07cdfa41539aa98b8db9a4812063f7fc734571 Mon Sep 17 00:00:00 2001 From: Christian Pauly Date: Sun, 16 May 2021 16:38:52 +0200 Subject: [PATCH] fix: Open URIs --- lib/main.dart | 6 ++++++ lib/views/chat_list.dart | 24 +++++++++++++++++++++++- lib/views/homeserver_picker.dart | 20 ++++++++++++-------- pubspec.lock | 21 +++++++++++++++++++++ pubspec.yaml | 1 + 5 files changed, 63 insertions(+), 9 deletions(-) diff --git a/lib/main.dart b/lib/main.dart index 5c7977da..a6806228 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -55,6 +55,12 @@ class FluffyChatApp extends StatelessWidget { const FluffyChatApp({Key key, this.testWidget, this.testClient}) : super(key: key); + + /// getInitialLink may rereturn the value multiple times if this view is + /// opened multiple times for example if the user logs out after they logged + /// in with qr code or magic link. + static bool gotInitialLink = false; + @override Widget build(BuildContext context) { return AdaptiveTheme( diff --git a/lib/views/chat_list.dart b/lib/views/chat_list.dart index 21919603..be2381b4 100644 --- a/lib/views/chat_list.dart +++ b/lib/views/chat_list.dart @@ -13,6 +13,8 @@ import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:future_loading_dialog/future_loading_dialog.dart'; import 'package:receive_sharing_intent/receive_sharing_intent.dart'; +import 'package:uni_links/uni_links.dart'; +import '../main.dart'; import 'widgets/matrix.dart'; import '../utils/matrix_file_extension.dart'; import '../utils/url_launcher.dart'; @@ -35,6 +37,8 @@ class ChatListController extends State { StreamSubscription _intentFileStreamSubscription; + StreamSubscription _intentUriStreamSubscription; + final selectedRoomIds = {}; void _processIncomingSharedFiles(List files) { @@ -57,7 +61,6 @@ class ChatListController extends State { if (text.toLowerCase().startsWith(AppConfig.inviteLinkPrefix) || (text.toLowerCase().startsWith(AppConfig.schemePrefix) && !RegExp(r'\s').hasMatch(text))) { - UrlLauncher(context, text).openMatrixToUrl(); return; } Matrix.of(context).shareContent = { @@ -66,6 +69,17 @@ class ChatListController extends State { }; } + void _processIncomingUris(String text) async { + if (text == null || !text.startsWith(AppConfig.appOpenUrlScheme)) return; + if (text.toLowerCase().startsWith(AppConfig.inviteLinkPrefix) || + (text.toLowerCase().startsWith(AppConfig.schemePrefix) && + !RegExp(r'\s').hasMatch(text))) { + AdaptivePageLayout.of(context).popUntilIsFirst(); + UrlLauncher(context, text).openMatrixToUrl(); + return; + } + } + void _initReceiveSharingIntent() { if (!PlatformInfos.isMobile) return; @@ -82,6 +96,13 @@ class ChatListController extends State { // For sharing or opening urls/text coming from outside the app while the app is closed ReceiveSharingIntent.getInitialText().then(_processIncomingSharedText); + + // For receiving shared Uris + _intentDataStreamSubscription = linkStream.listen(_processIncomingUris); + if (FluffyChatApp.gotInitialLink == false) { + FluffyChatApp.gotInitialLink = true; + getInitialLink().then(_processIncomingUris); + } } @override @@ -94,6 +115,7 @@ class ChatListController extends State { void dispose() { _intentDataStreamSubscription?.cancel(); _intentFileStreamSubscription?.cancel(); + _intentUriStreamSubscription?.cancel(); super.dispose(); } diff --git a/lib/views/homeserver_picker.dart b/lib/views/homeserver_picker.dart index 2d0d136d..62907dbf 100644 --- a/lib/views/homeserver_picker.dart +++ b/lib/views/homeserver_picker.dart @@ -7,13 +7,14 @@ import 'package:fluffychat/views/widgets/matrix.dart'; import 'package:fluffychat/config/app_config.dart'; import 'package:fluffychat/config/setting_keys.dart'; import 'package:fluffychat/utils/platform_infos.dart'; +import 'package:uni_links/uni_links.dart'; import 'package:flutter/foundation.dart'; import 'package:flutter_gen/gen_l10n/l10n.dart'; import 'package:flutter/material.dart'; import 'package:future_loading_dialog/future_loading_dialog.dart'; -import 'package:receive_sharing_intent/receive_sharing_intent.dart'; import 'package:url_launcher/url_launcher.dart'; +import '../main.dart'; import '../utils/localized_exception_extension.dart'; import 'package:universal_html/html.dart' as html; @@ -41,24 +42,27 @@ class HomeserverPickerController extends State { ); } - void _processIncomingSharedText(String text) async { + void _processIncomingUris(String text) async { if (text == null || !text.startsWith(AppConfig.appOpenUrlScheme)) return; AdaptivePageLayout.of(context).popUntilIsFirst(); final token = Uri.parse(text).queryParameters['loginToken']; - _loginWithToken(token); + if (token != null) _loginWithToken(token); } - void _initReceiveSharingContent() { + void _initReceiveUri() { if (!PlatformInfos.isMobile) return; - _intentDataStreamSubscription = - ReceiveSharingIntent.getTextStream().listen(_processIncomingSharedText); - ReceiveSharingIntent.getInitialText().then(_processIncomingSharedText); + // For receiving shared Uris + _intentDataStreamSubscription = linkStream.listen(_processIncomingUris); + if (FluffyChatApp.gotInitialLink == false) { + FluffyChatApp.gotInitialLink = true; + getInitialLink().then(_processIncomingUris); + } } @override void initState() { super.initState(); - _initReceiveSharingContent(); + _initReceiveUri(); if (kIsWeb) { WidgetsBinding.instance.addPostFrameCallback((_) { final token = diff --git a/pubspec.lock b/pubspec.lock index d37b23b5..b6582234 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -1103,6 +1103,27 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "1.3.0" + uni_links: + dependency: "direct main" + description: + name: uni_links + url: "https://pub.dartlang.org" + source: hosted + version: "0.5.1" + uni_links_platform_interface: + dependency: transitive + description: + name: uni_links_platform_interface + url: "https://pub.dartlang.org" + source: hosted + version: "1.0.0" + uni_links_web: + dependency: transitive + description: + name: uni_links_web + url: "https://pub.dartlang.org" + source: hosted + version: "0.1.0" unifiedpush: dependency: "direct main" description: diff --git a/pubspec.yaml b/pubspec.yaml index bc81b72e..e7dd03f6 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -66,6 +66,7 @@ dependencies: sqflite: ^2.0.0+3 # Still used to obtain the database location sqlite3: ^1.0.0 swipe_to_action: ^0.1.0 + uni_links: ^0.5.1 unifiedpush: ^1.0.2 universal_html: ^2.0.8 url_launcher: ^6.0.3