From 84cc65489f7877fc57f9c95c2a7d61bf7100b3d2 Mon Sep 17 00:00:00 2001 From: bggRGjQaUbCoE Date: Thu, 13 Feb 2025 17:51:52 +0800 Subject: [PATCH] mod: scheme match Signed-off-by: bggRGjQaUbCoE --- lib/pages/main/view.dart | 2 ++ lib/utils/app_scheme.dart | 50 +++++++++++++++------------------------ 2 files changed, 21 insertions(+), 31 deletions(-) diff --git a/lib/pages/main/view.dart b/lib/pages/main/view.dart index f9730212..fc5f2498 100644 --- a/lib/pages/main/view.dart +++ b/lib/pages/main/view.dart @@ -5,6 +5,7 @@ import 'package:PiliPlus/common/widgets/network_img_layer.dart'; import 'package:PiliPlus/common/widgets/tabs.dart'; import 'package:PiliPlus/grpc/grpc_client.dart'; import 'package:PiliPlus/pages/mine/controller.dart'; +import 'package:PiliPlus/utils/app_scheme.dart'; import 'package:PiliPlus/utils/utils.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; @@ -156,6 +157,7 @@ class _MainAppState extends State await GrpcClient.instance.shutdown(); await GStorage.close(); EventBus().off(EventName.loginEvent); + PiliScheme.listener?.cancel(); super.dispose(); } diff --git a/lib/utils/app_scheme.dart b/lib/utils/app_scheme.dart index 45e9fe32..d07c308f 100644 --- a/lib/utils/app_scheme.dart +++ b/lib/utils/app_scheme.dart @@ -15,12 +15,15 @@ import 'utils.dart'; class PiliScheme { static late AppLinks appLinks; + static StreamSubscription? listener; + static Future init() async { // Register our protocol only on Windows platform // registerProtocolHandler('bilibili'); appLinks = AppLinks(); - appLinks.uriLinkStream.listen((uri) { + listener?.cancel(); + listener = appLinks.uriLinkStream.listen((uri) { debugPrint('onAppLink: $uri'); routePush(uri); }); @@ -53,12 +56,6 @@ class PiliScheme { final String host = uri.host.toLowerCase(); final String path = uri.path; - void launchURL() { - if (selfHandle.not) { - Utils.launchURL(uri.toString()); - } - } - switch (scheme) { case 'bilibili': switch (host) { @@ -68,6 +65,18 @@ class PiliScheme { (Route route) => route.isFirst, ); return true; + case 'pgc': + // bilibili://pgc/season/ep/123456?h5_awaken_params=random + String? id = RegExp(r'/(\d+)').firstMatch(path)?.group(1); + if (id != null) { + bool isEp = path.contains('/ep/'); + Utils.viewBangumi( + seasonId: isEp ? null : id, + epId: isEp ? id : null, + ); + return true; + } + return false; case 'space': // bilibili://space/12345678?frommodule=XX&h5awaken=random String? mid = RegExp(r'/(\d+)').firstMatch(path)?.group(1); @@ -75,7 +84,6 @@ class PiliScheme { Utils.toDupNamed('/member?mid=$mid', off: off); return true; } - launchURL(); return false; case 'video': if (uri.queryParameters['comment_root_id'] != null) { @@ -113,7 +121,6 @@ class PiliScheme { ); return true; } - launchURL(); return false; } @@ -131,7 +138,6 @@ class PiliScheme { ); return true; } - launchURL(); return false; case 'live': // bilibili://live/12345678?extra_jump_from=1&from=1&is_room_feed=1&h5awaken=random @@ -140,10 +146,9 @@ class PiliScheme { Utils.toDupNamed('/liveRoom?roomid=$roomId', off: off); return true; } - launchURL(); return false; case 'bangumi': - // to check + // bilibili://bangumi/season/12345678?h5_awaken_params=random if (path.startsWith('/season')) { String? seasonId = RegExp(r'/(\d+)').firstMatch(path)?.group(1); if (seasonId != null) { @@ -151,18 +156,13 @@ class PiliScheme { return true; } } - launchURL(); return false; case 'opus': // bilibili://opus/detail/12345678?h5awaken=random if (path.startsWith('/detail')) { bool hasMatch = await _onPushDynDetail(path, off); - if (hasMatch.not) { - launchURL(); - } return hasMatch; } - launchURL(); return false; case 'search': Utils.toDupNamed( @@ -187,7 +187,6 @@ class PiliScheme { ); return true; } - launchURL(); return false; case 'comment': if (path.startsWith("/detail/")) { @@ -229,7 +228,6 @@ class PiliScheme { ); return true; } - launchURL(); return false; case 'following': if (path.startsWith("/detail/")) { @@ -246,11 +244,8 @@ class PiliScheme { actions: [ IconButton( tooltip: '前往', - onPressed: () async { - bool hasMatch = await _onPushDynDetail(path, off); - if (hasMatch.not) { - launchURL(); - } + onPressed: () { + _onPushDynDetail(path, off); }, icon: const Icon(Icons.open_in_new), ), @@ -268,13 +263,9 @@ class PiliScheme { return true; } else { bool hasMatch = await _onPushDynDetail(path, off); - if (hasMatch.not) { - launchURL(); - } return hasMatch; } } - launchURL(); return false; case 'album': String? rid = RegExp(r'/(\d+)').firstMatch(path)?.group(1); @@ -297,14 +288,12 @@ class PiliScheme { } return true; } - launchURL(); return false; default: if (selfHandle.not) { debugPrint('$uri'); SmartDialog.showToast('未知路径:$uri,请截图反馈给开发者'); } - launchURL(); return false; } case 'http' || 'https': @@ -328,7 +317,6 @@ class PiliScheme { debugPrint('$uri'); SmartDialog.showToast('未知路径:$uri,请截图反馈给开发者'); } - launchURL(); return false; } }