Skip to content

Commit

Permalink
Merge pull request #541 from lamarios/fix/open-app-from-url
Browse files Browse the repository at this point in the history
fix issues with app links not opening the related video
  • Loading branch information
lamarios authored Apr 28, 2024
2 parents 4c2ea48 + ac8be04 commit 3155da5
Showing 1 changed file with 25 additions and 11 deletions.
36 changes: 25 additions & 11 deletions lib/app/states/app.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ final log = Logger('HomeState');

class AppCubit extends Cubit<AppState> {
late final StreamSubscription intentDataStreamSubscription;

AppCubit(super.initialState) {
onReady();
initState();
Expand Down Expand Up @@ -45,11 +46,11 @@ class AppCubit extends Cubit<AppState> {
intentDataStreamSubscription =
ReceiveSharingIntent.getMediaStream().listen((shared) {
final String? value = shared
.where((element) => element.type == SharedMediaType.file)
.map((e) => e.message)
.where((element) => element.type == SharedMediaType.url)
.map((e) => e.path)
.firstOrNull;
if (value != null) {
openAppLink(value);
openAppLink(value, false);
}
}, onError: (err) {
log.warning("getLinkStream error: $err");
Expand All @@ -58,11 +59,11 @@ class AppCubit extends Cubit<AppState> {
// For sharing or opening urls/text coming from outside the app while the app is closed
ReceiveSharingIntent.getInitialMedia().then((shared) {
final String? value = shared
.where((element) => element.type == SharedMediaType.file)
.map((e) => e.message)
.where((element) => element.type == SharedMediaType.url)
.map((e) => e.path)
.firstOrNull;
if (value != null) {
openAppLink(value);
openAppLink(value, true);
}
ReceiveSharingIntent.reset();
});
Expand All @@ -76,19 +77,32 @@ class AppCubit extends Cubit<AppState> {
super.close();
}

void openAppLink(String url) {
void openAppLink(String url, bool fullStack) {
try {
log.fine('opening $url, full stack: $fullStack');
Uri uri = Uri.parse(url);
String? videoId;
if (youtubeHosts.contains(uri.host)) {
if (uri.pathSegments.length == 1 &&
uri.pathSegments.contains("watch") &&
uri.queryParameters.containsKey('v')) {
String videoId = uri.queryParameters['v']!;
appRouter.push(VideoRoute(videoId: videoId));
videoId = uri.queryParameters['v']!;
}
if (uri.host == 'youtu.be' && uri.pathSegments.length == 1) {
String videoId = uri.pathSegments[0];
appRouter.push(VideoRoute(videoId: videoId));
videoId = uri.pathSegments[0];
}

if (videoId != null) {
if (fullStack) {
appRouter.replaceAll([
MainRoute(children: [
const MainContentRoute(),
VideoRoute(videoId: videoId)
])
]);
} else {
appRouter.push(VideoRoute(videoId: videoId));
}
}
}
} catch (err, stacktrace) {
Expand Down

0 comments on commit 3155da5

Please sign in to comment.