Skip to content

Commit

Permalink
feat: 适配app & deep link
Browse files Browse the repository at this point in the history
  • Loading branch information
FoxSensei001 committed Dec 26, 2024
1 parent 340176e commit fb30d49
Show file tree
Hide file tree
Showing 13 changed files with 167 additions and 3 deletions.
21 changes: 21 additions & 0 deletions android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,27 @@
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
<!-- Deep Links -->
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<!-- Custom Scheme -->
<data android:scheme="iwara" />
</intent-filter>

<!-- App Links -->
<intent-filter android:autoVerify="true">
<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" />
<data android:host="iwara.tv" />
<data android:pathPattern="/video/.*" />
<data android:pathPattern="/profile/.*" />
<data android:pathPattern="/playlist/.*" />
<data android:pathPattern="/image/.*" />
</intent-filter>
</activity>
<!-- Don't delete the meta-data below.
This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
Expand Down
15 changes: 15 additions & 0 deletions ios/Runner/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,25 @@
<key>UIRequiresPersistentWiFi</key>
<true/>

<!-- 允许打开其他app -->
<key>LSApplicationQueriesSchemes</key>
<array>
<string>sms</string>
<string>tel</string>
</array>

<!-- 深度链接 -->
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLSchemes</key>
<array>
<string>iwara</string>
</array>
</dict>
</array>
<key>FlutterDeepLinkingEnabled</key>
<true/>

</dict>
</plist>
59 changes: 59 additions & 0 deletions lib/app/services/deep_link_service.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import 'package:app_links/app_links.dart';
import 'package:get/get.dart';
import 'package:i_iwara/app/services/app_service.dart';

class DeepLinkService extends GetxService {
final _appLinks = AppLinks();

Future<void> init() async {
// 监听所有链接事件
_appLinks.uriLinkStream.listen((Uri? uri) {
if (uri != null) {
_handleLink(uri);
}
});

// 检查初始链接(从外部打开应用时)
final uri = await _appLinks.getInitialLink();
if (uri != null) {
_handleLink(uri);
}
}

void _handleLink(Uri uri) {
// 处理不同类型的链接
final pathSegments = uri.pathSegments;

if (pathSegments.isEmpty) return;

switch (pathSegments[0]) {
case 'video':
if (pathSegments.length > 1) {
final videoId = pathSegments[1];
NaviService.navigateToVideoDetailPage(videoId);
}
break;

case 'profile':
if (pathSegments.length > 1) {
final userName = pathSegments[1];
NaviService.navigateToAuthorProfilePage(userName);
}
break;

case 'playlist':
if (pathSegments.length > 1) {
final playlistId = pathSegments[1];
NaviService.navigateToPlayListDetail(playlistId, isMine: false);
}
break;

case 'image':
if (pathSegments.length > 1) {
final imageId = pathSegments[1];
NaviService.navigateToGalleryDetailPage(imageId);
}
break;
}
}
}
15 changes: 12 additions & 3 deletions lib/app/ui/widgets/custom_markdown_body_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -220,19 +220,28 @@ class _CustomMarkdownBodyState extends State<CustomMarkdownBody> {
}

void _onTapLink(String text, String? href, String title) async {
print('senko 点击的链接是:$href');
if (href == null) return;

try {
Uri uri = Uri.parse(href);
if (href.startsWith(
'${CommonConstants.iwaraBaseUrl}${ApiConstants.profilePrefix()}')) {
final userName = href.split('/').last;
final userName = uri.pathSegments.last;
NaviService.navigateToAuthorProfilePage(userName);
} else if (href.startsWith(
'${CommonConstants.iwaraBaseUrl}${ApiConstants.galleryDetail()}')) {
final imageId = href.split('/').last;
final imageId = uri.pathSegments.last;
NaviService.navigateToGalleryDetailPage(imageId);
} else if (href.startsWith(
'${CommonConstants.iwaraBaseUrl}/video/')) {
final videoId = uri.pathSegments[1];
NaviService.navigateToVideoDetailPage(videoId);
} else if (href.startsWith(
'${CommonConstants.iwaraBaseUrl}/playlist/')) {
final playlistId = uri.pathSegments.last;
NaviService.navigateToPlayListDetail(playlistId, isMine: false);
} else {
final uri = Uri.parse(href);
if (await canLaunchUrl(uri)) {
await launchUrl(uri);
} else {
Expand Down
3 changes: 3 additions & 0 deletions lib/common/constants.dart
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ class ApiConstants {
// 图库详情
static String galleryDetail() => '/image';

// 视频详情
static String videoDetail() => '/video';

// 用户详情
static String userProfile(String userName) => '/profile/$userName';

Expand Down
5 changes: 5 additions & 0 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import 'package:get/get.dart';
import 'package:get_storage/get_storage.dart';
import 'package:i_iwara/app/services/app_service.dart';
import 'package:i_iwara/app/services/comment_service.dart';
import 'package:i_iwara/app/services/deep_link_service.dart';
import 'package:i_iwara/app/services/gallery_service.dart';
import 'package:i_iwara/app/services/light_service.dart';
import 'package:i_iwara/app/services/play_list_service.dart';
Expand Down Expand Up @@ -48,6 +49,10 @@ void main() {

// 确保Flutter初始化
WidgetsFlutterBinding.ensureInitialized();
// 初始化深度链接服务
final deepLinkService = DeepLinkService();
await deepLinkService.init();
Get.put(deepLinkService);
// 现在有 简中、英文、日文
// 获取系统语言,如果是支持的语言,则使用,如果不是,则使用英文;
String systemLanguage = Get.deviceLocale?.languageCode ?? 'en';
Expand Down
4 changes: 4 additions & 0 deletions linux/flutter/generated_plugin_registrant.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <dynamic_color/dynamic_color_plugin.h>
#include <file_selector_linux/file_selector_plugin.h>
#include <flutter_secure_storage_linux/flutter_secure_storage_linux_plugin.h>
#include <gtk/gtk_plugin.h>
#include <irondash_engine_context/irondash_engine_context_plugin.h>
#include <media_kit_libs_linux/media_kit_libs_linux_plugin.h>
#include <media_kit_video/media_kit_video_plugin.h>
Expand All @@ -28,6 +29,9 @@ void fl_register_plugins(FlPluginRegistry* registry) {
g_autoptr(FlPluginRegistrar) flutter_secure_storage_linux_registrar =
fl_plugin_registry_get_registrar_for_plugin(registry, "FlutterSecureStorageLinuxPlugin");
flutter_secure_storage_linux_plugin_register_with_registrar(flutter_secure_storage_linux_registrar);
g_autoptr(FlPluginRegistrar) gtk_registrar =
fl_plugin_registry_get_registrar_for_plugin(registry, "GtkPlugin");
gtk_plugin_register_with_registrar(gtk_registrar);
g_autoptr(FlPluginRegistrar) irondash_engine_context_registrar =
fl_plugin_registry_get_registrar_for_plugin(registry, "IrondashEngineContextPlugin");
irondash_engine_context_plugin_register_with_registrar(irondash_engine_context_registrar);
Expand Down
1 change: 1 addition & 0 deletions linux/flutter/generated_plugins.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ list(APPEND FLUTTER_PLUGIN_LIST
dynamic_color
file_selector_linux
flutter_secure_storage_linux
gtk
irondash_engine_context
media_kit_libs_linux
media_kit_video
Expand Down
2 changes: 2 additions & 0 deletions macos/Flutter/GeneratedPluginRegistrant.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import FlutterMacOS
import Foundation

import app_links
import device_info_plus
import dynamic_color
import file_selector_macos
Expand All @@ -25,6 +26,7 @@ import wakelock_plus
import window_manager

func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
AppLinksMacosPlugin.register(with: registry.registrar(forPlugin: "AppLinksMacosPlugin"))
DeviceInfoPlusMacosPlugin.register(with: registry.registrar(forPlugin: "DeviceInfoPlusMacosPlugin"))
DynamicColorPlugin.register(with: registry.registrar(forPlugin: "DynamicColorPlugin"))
FileSelectorPlugin.register(with: registry.registrar(forPlugin: "FileSelectorPlugin"))
Expand Down
40 changes: 40 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,38 @@ packages:
url: "https://pub.dev"
source: hosted
version: "6.11.0"
app_links:
dependency: "direct main"
description:
name: app_links
sha256: "433df2e61b10519407475d7f69e470789d23d593f28224c38ba1068597be7950"
url: "https://pub.dev"
source: hosted
version: "6.3.3"
app_links_linux:
dependency: transitive
description:
name: app_links_linux
sha256: f5f7173a78609f3dfd4c2ff2c95bd559ab43c80a87dc6a095921d96c05688c81
url: "https://pub.dev"
source: hosted
version: "1.0.3"
app_links_platform_interface:
dependency: transitive
description:
name: app_links_platform_interface
sha256: "05f5379577c513b534a29ddea68176a4d4802c46180ee8e2e966257158772a3f"
url: "https://pub.dev"
source: hosted
version: "2.0.2"
app_links_web:
dependency: transitive
description:
name: app_links_web
sha256: af060ed76183f9e2b87510a9480e56a5352b6c249778d07bd2c95fc35632a555
url: "https://pub.dev"
source: hosted
version: "1.0.4"
archive:
dependency: transitive
description:
Expand Down Expand Up @@ -586,6 +618,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "2.3.2"
gtk:
dependency: transitive
description:
name: gtk
sha256: e8ce9ca4b1df106e4d72dad201d345ea1a036cc12c360f1a7d5a758f78ffa42c
url: "https://pub.dev"
source: hosted
version: "2.1.0"
http:
dependency: transitive
description:
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ dependencies:
slang_flutter: ^4.4.0
dynamic_color: ^1.7.0
oktoast: ^3.4.0
app_links: ^6.3.3

dev_dependencies:
flutter_test:
Expand Down
3 changes: 3 additions & 0 deletions windows/flutter/generated_plugin_registrant.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include "generated_plugin_registrant.h"

#include <app_links/app_links_plugin_c_api.h>
#include <dynamic_color/dynamic_color_plugin_c_api.h>
#include <file_selector_windows/file_selector_windows.h>
#include <flutter_secure_storage_windows/flutter_secure_storage_windows_plugin.h>
Expand All @@ -21,6 +22,8 @@
#include <window_manager/window_manager_plugin.h>

void RegisterPlugins(flutter::PluginRegistry* registry) {
AppLinksPluginCApiRegisterWithRegistrar(
registry->GetRegistrarForPlugin("AppLinksPluginCApi"));
DynamicColorPluginCApiRegisterWithRegistrar(
registry->GetRegistrarForPlugin("DynamicColorPluginCApi"));
FileSelectorWindowsRegisterWithRegistrar(
Expand Down
1 change: 1 addition & 0 deletions windows/flutter/generated_plugins.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#

list(APPEND FLUTTER_PLUGIN_LIST
app_links
dynamic_color
file_selector_windows
flutter_secure_storage_windows
Expand Down

0 comments on commit fb30d49

Please sign in to comment.