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"/> android:host="matrix.to"/>
</intent-filter> </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> </activity>
<!-- Don't delete the meta-data below. <!-- Don't delete the meta-data below.
This is used by the Flutter tool to generate GeneratedPluginRegistrant.java --> 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:fluffychat/views/settings.dart';
import 'package:flutter/foundation.dart'; import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_speed_dial/flutter_speed_dial.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 } enum SelectMode { normal, multi_select, share }
@ -62,32 +64,31 @@ class _ChatListState extends State<ChatList> {
searchController.addListener( searchController.addListener(
() => setState(() => null), () => setState(() => null),
); );
if (kIsWeb) { initUniLinks();
getSharedData();
}
super.initState(); super.initState();
} }
StreamSubscription _intentDataStreamSubscription; StreamSubscription _intentDataStreamSubscription;
void processSharedText(String text) { StreamSubscription _onUniLinksub;
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,
});
}
}
void getSharedData() { Future<void> initUniLinks() async {
// For sharing or opening urls/text coming from outside the app while the app is in the memory if (kIsWeb) return;
_intentDataStreamSubscription = ReceiveSharingIntent.getTextStream() _onUniLinksub ??= getLinksStream().listen(
.listen(processSharedText, onError: (err) {}); (String initialLink) {
// For sharing or opening urls/text coming from outside the app while the app is closed try {
ReceiveSharingIntent.getInitialText().then(processSharedText); 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 @override
@ -97,6 +98,7 @@ class _ChatListState extends State<ChatList> {
() => setState(() => null), () => setState(() => null),
); );
_intentDataStreamSubscription?.cancel(); _intentDataStreamSubscription?.cancel();
_onUniLinksub?.cancel();
super.dispose(); super.dispose();
} }

View file

@ -366,13 +366,6 @@ packages:
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "2.0.5" 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: share:
dependency: "direct main" dependency: "direct main"
description: description:
@ -455,6 +448,13 @@ packages:
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.1.6" 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: universal_html:
dependency: "direct main" dependency: "direct main"
description: description:

View file

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