Skip to content

Commit

Permalink
安卓支持在外部app里通过e站链接唤起JHenTai
Browse files Browse the repository at this point in the history
Support open url from the app outside with JHenTai
  • Loading branch information
jiangtian616 committed Aug 24, 2022
1 parent afeb887 commit 918de8f
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 16 deletions.
14 changes: 14 additions & 0 deletions android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,20 @@
<action android:name="android.intent.action.MAIN"/>
<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="e-hentai.org"
android:pathPrefix="/g/"/>
<data
android:scheme="https"
android:host="exhentai.org"
android:pathPrefix="/g/"/>
</intent-filter>
</activity>
<!-- Don't delete the meta-data below.
This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
Expand Down
80 changes: 64 additions & 16 deletions lib/src/pages/home_page.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:async';

import 'package:clipboard/clipboard.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
Expand All @@ -11,9 +13,11 @@ import 'package:jhentai/src/pages/ranklist/ranklist_page_logic.dart';
import 'package:jhentai/src/pages/search/desktop/desktop_search_page_logic.dart';
import 'package:jhentai/src/pages/watched/watched_page_logic.dart';
import 'package:jhentai/src/setting/style_setting.dart';
import 'package:jhentai/src/utils/log.dart';
import 'package:jhentai/src/utils/toast_util.dart';
import 'package:jhentai/src/widget/will_pop_interceptor.dart';
import 'package:jhentai/src/widget/windows_app.dart';
import 'package:receive_sharing_intent/receive_sharing_intent.dart';

import '../consts/eh_consts.dart';
import '../model/jh_layout.dart';
Expand Down Expand Up @@ -44,17 +48,25 @@ class HomePage extends StatefulWidget {
}

class _HomePageState extends State<HomePage> {
StreamSubscription? _intentDataStreamSubscription;
String? _lastDetectedUrl;

@override
void initState() {
super.initState();
initToast(context);
_initPageLogic();
_initSharingIntent();
_handleUrlInClipBoard();
AppStateListener.registerDidChangeAppLifecycleStateCallback(resumeAndHandleUrlInClipBoard);
}

@override
void dispose() {
_intentDataStreamSubscription?.cancel();
super.dispose();
}

@override
Widget build(BuildContext context) {
return WindowsApp(
Expand Down Expand Up @@ -106,6 +118,58 @@ class _HomePageState extends State<HomePage> {
}
}

void _initPageLogic() {
/// Mobile layout v2
Get.lazyPut(() => DashboardPageLogic(), fenix: true);

/// Desktop layout
Get.lazyPut(() => GallerysPageLogic(), fenix: true);
Get.lazyPut(() => DesktopSearchPageLogic(), fenix: true);

/// Mobile layout v2 & Desktop layout
Get.lazyPut(() => PopularPageLogic(), fenix: true);
Get.lazyPut(() => RanklistPageLogic(), fenix: true);
Get.lazyPut(() => FavoritePageLogic(), fenix: true);
Get.lazyPut(() => WatchedPageLogic(), fenix: true);
Get.lazyPut(() => HistoryPageLogic(), fenix: true);
}

/// Listen to share or open urls/text coming from outside the app while the app is in the memory or is closed
void _initSharingIntent() {
if (!GetPlatform.isAndroid) {
return;
}

ReceiveSharingIntent.getInitialText().then(
(String? url) {
if (url != null) {
toRoute(
Routes.details,
arguments: url,
offAllBefore: false,
preventDuplicates: false,
);
}
},
);

_intentDataStreamSubscription = ReceiveSharingIntent.getTextStream().listen(
(String url) => toRoute(
Routes.details,
arguments: url,
offAllBefore: false,
preventDuplicates: false,
),
onError: (e) {
Log.error('ReceiveSharingIntent Error!', e);
Log.upload(e);
},
);
}

/// user open a url in other app by JHenTai
void _handleUrlFromOtherApp() async {}

/// a gallery url exists in clipboard, show dialog to check whether enter detail page
void _handleUrlInClipBoard() async {
String text = await FlutterClipboard.paste();
Expand Down Expand Up @@ -134,20 +198,4 @@ class _HomePageState extends State<HomePage> {
longDuration: true,
);
}

void _initPageLogic() {
/// Mobile layout v2
Get.lazyPut(() => DashboardPageLogic(), fenix: true);

/// Desktop layout
Get.lazyPut(() => GallerysPageLogic(), fenix: true);
Get.lazyPut(() => DesktopSearchPageLogic(), fenix: true);

/// Mobile layout v2 & Desktop layout
Get.lazyPut(() => PopularPageLogic(), fenix: true);
Get.lazyPut(() => RanklistPageLogic(), fenix: true);
Get.lazyPut(() => FavoritePageLogic(), fenix: true);
Get.lazyPut(() => WatchedPageLogic(), fenix: true);
Get.lazyPut(() => HistoryPageLogic(), fenix: true);
}
}
7 changes: 7 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1054,6 +1054,13 @@ packages:
url: "https://pub.flutter-io.cn"
source: hosted
version: "4.0.0"
receive_sharing_intent:
dependency: "direct main"
description:
name: receive_sharing_intent
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.4.5"
retry:
dependency: "direct main"
description:
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ dependencies:
battery_plus: ^2.1.4
throttling: ^1.0.0
bitsdojo_window: ^0.1.2
receive_sharing_intent: ^1.4.5

dev_dependencies:
flutter_test:
Expand Down

0 comments on commit 918de8f

Please sign in to comment.