Open links with uni_links

This commit is contained in:
Christian Pauly 2020-02-10 14:14:55 +01:00
parent 1bf9dbecf6
commit 05bdd5968a
4 changed files with 31 additions and 36 deletions

View File

@ -38,13 +38,6 @@
android:host="matrix.to"/>
</intent-filter>
<!--TODO: Add this filter, if you want to support sharing text into your app-->
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/*" />
</intent-filter>
</activity>
<!-- Don't delete the meta-data below.
This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->

View File

@ -13,8 +13,10 @@ import 'package:fluffychat/views/new_private_chat.dart';
import 'package:fluffychat/views/settings.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_speed_dial/flutter_speed_dial.dart';
import 'package:receive_sharing_intent/receive_sharing_intent.dart';
import 'package:toast/toast.dart';
import 'package:uni_links/uni_links.dart';
enum SelectMode { normal, multi_select, share }
@ -62,32 +64,31 @@ class _ChatListState extends State<ChatList> {
searchController.addListener(
() => setState(() => null),
);
if (kIsWeb) {
getSharedData();
}
initUniLinks();
super.initState();
}
StreamSubscription _intentDataStreamSubscription;
void processSharedText(String text) {
if (text?.isEmpty ?? true) return;
if (text.startsWith("https://matrix.to/#/")) {
UrlLauncher(context, text).openMatrixToUrl();
} else {
setState(() => Matrix.of(context).shareContent = {
"msgtype": "m.text",
"body": text,
});
}
}
StreamSubscription _onUniLinksub;
void getSharedData() {
// For sharing or opening urls/text coming from outside the app while the app is in the memory
_intentDataStreamSubscription = ReceiveSharingIntent.getTextStream()
.listen(processSharedText, onError: (err) {});
// For sharing or opening urls/text coming from outside the app while the app is closed
ReceiveSharingIntent.getInitialText().then(processSharedText);
Future<void> initUniLinks() async {
if (kIsWeb) return;
_onUniLinksub ??= getLinksStream().listen(
(String initialLink) {
try {
if (initialLink?.isEmpty ?? true) return;
if (initialLink.startsWith("https://matrix.to/#/")) {
UrlLauncher(context, initialLink).openMatrixToUrl();
}
} on PlatformException {
debugPrint("initUniLinks failed during platform exception");
}
},
onError: (error) => Toast.show(
I18n.of(context).oopsSomethingWentWrong + " " + error.toString(), context,
duration: 5),
);
}
@override
@ -97,6 +98,7 @@ class _ChatListState extends State<ChatList> {
() => setState(() => null),
);
_intentDataStreamSubscription?.cancel();
_onUniLinksub?.cancel();
super.dispose();
}

View File

@ -366,13 +366,6 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.5"
receive_sharing_intent:
dependency: "direct main"
description:
name: receive_sharing_intent
url: "https://pub.dartlang.org"
source: hosted
version: "1.3.2"
share:
dependency: "direct main"
description:
@ -455,6 +448,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.6"
uni_links:
dependency: "direct main"
description:
name: uni_links
url: "https://pub.dartlang.org"
source: hosted
version: "0.2.0"
universal_html:
dependency: "direct main"
description:

View File

@ -45,10 +45,10 @@ dependencies:
path_provider: ^1.5.1
webview_flutter: ^0.3.19+4
share: ^0.6.3+5
receive_sharing_intent: ^1.3.2
flutter_secure_storage: ^3.3.1+1
http: ^0.12.0+4
universal_html: ^1.1.12
uni_links: ^0.2.0
intl: ^0.16.0
intl_translation: ^0.17.9