feat(android): add intent share support

This commit is contained in:
Kingkor Roy Tirtho 2023-12-16 17:07:11 +06:00
parent 7cad452d82
commit f5917e5b65
4 changed files with 70 additions and 1 deletions

View File

@ -48,6 +48,22 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data
android:scheme="https"
android:host="open.spotify.com"
/>
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/*" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />

View File

@ -5,6 +5,8 @@ import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:spotify/spotify.dart';
import 'package:spotube/collections/routes.dart';
import 'package:spotube/provider/spotify_provider.dart';
import 'package:flutter_sharing_intent/flutter_sharing_intent.dart';
import 'package:flutter_sharing_intent/model/sharing_file.dart';
void useDeepLinking(WidgetRef ref) {
// single instance no worries
@ -13,6 +15,45 @@ void useDeepLinking(WidgetRef ref) {
final queryClient = useQueryClient();
useEffect(() {
void uriListener(List<SharedFile> files) async {
for (final file in files) {
if (file.type != SharedMediaType.URL) continue;
final url = Uri.parse(file.value!);
if (url.pathSegments.length != 2) continue;
switch (url.pathSegments.first) {
case "album":
router.push(
"/album/${url.pathSegments.last}",
extra: await queryClient.fetchQuery<Album, dynamic>(
"album/${url.pathSegments.last}",
() => spotify.albums.get(url.pathSegments.last),
),
);
break;
case "artist":
router.push("/artist/${url.pathSegments.last}");
break;
case "playlist":
router.push(
"/playlist/${url.pathSegments.last}",
extra: await queryClient.fetchQuery<Playlist, dynamic>(
"playlist/${url.pathSegments.last}",
() => spotify.playlists.get(url.pathSegments.last),
),
);
break;
default:
break;
}
}
}
FlutterSharingIntent.instance.getInitialSharing().then(uriListener);
final mediaStream =
FlutterSharingIntent.instance.getMediaStream().listen(uriListener);
final subscription = appLinks.allStringLinkStream.listen((uri) async {
final startSegment = uri.split(":").take(2).join(":");
final endSegment = uri.split(":").last;
@ -44,6 +85,9 @@ void useDeepLinking(WidgetRef ref) {
}
});
return subscription.cancel;
return () {
mediaStream.cancel();
subscription.cancel();
};
}, [spotify, queryClient]);
}

View File

@ -915,6 +915,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "3.0.0"
flutter_sharing_intent:
dependency: "direct main"
description:
name: flutter_sharing_intent
sha256: "6eb896e6523b735e8230eeb206fd3b9f220f11ce879c2400a90b443147036ff9"
url: "https://pub.dev"
source: hosted
version: "1.1.0"
flutter_svg:
dependency: "direct main"
description:

View File

@ -121,6 +121,7 @@ dependencies:
skeletonizer: ^0.8.0
app_links: ^3.5.0
win32_registry: ^1.1.2
flutter_sharing_intent: ^1.1.0
dev_dependencies:
build_runner: ^2.3.2