Skip to content

Commit

Permalink
修复通过url冷启动app的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
honjow committed Nov 30, 2022
1 parent 9efcd38 commit b0b2fb6
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 105 deletions.
59 changes: 59 additions & 0 deletions lib/common/provider/receive_sharing_provider.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import 'dart:async';

import 'package:eros_n/common/global.dart';
import 'package:eros_n/routes/routes.dart';
import 'package:eros_n/utils/get_utils/get_utils.dart';
import 'package:eros_n/utils/logger.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:receive_sharing_intent/receive_sharing_intent.dart';

class ReceiveSharingNotifier extends StateNotifier<void> {
ReceiveSharingNotifier(this.ref) : super(null);

final Ref ref;

String? sharedText;

Future<void> _goPage(
String url,
WidgetRef widgetRef, {
bool replace = true,
}) async {
if (url.isEmpty ||
!(await RouteUtil.goGalleryByUrl(widgetRef, url, replace: replace))) {
0
.milliseconds
.delay()
.then((value) => erosRouter.replaceNamed(NHRoutes.home));
}
}

void listenReceiveSharing(WidgetRef widgetRef) {
// For sharing or opening urls/text coming from outside the app while the app is in the memory
ReceiveSharingIntent.getTextStream().listen((String value) {
sharedText = value;
logger.e('getTextStream Shared: $sharedText');
_goPage(sharedText ?? '', widgetRef, replace: false);
}, onError: (err) {
logger.e('getTextStream error: $err');
});

// For sharing or opening urls/text coming from outside the app while the app is closed
ReceiveSharingIntent.getInitialText().then((String? value) {
logger.e('getInitialText Shared: $value');

if (value == sharedText) {
return;
}

sharedText = value ?? '';
logger.v('Shared: $sharedText');
_goPage(sharedText ?? '', widgetRef, replace: false);
});
}
}

final receiveSharingProvider =
StateNotifierProvider.autoDispose<ReceiveSharingNotifier, void>((ref) {
return ReceiveSharingNotifier(ref);
});
30 changes: 0 additions & 30 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ class _MyAppState extends ConsumerState<MyApp> with WidgetsBindingObserver {
void initState() {
super.initState();
WidgetsBinding.instance.addObserver(this);
listenReceiveSharing();
}

@override
Expand Down Expand Up @@ -188,35 +187,6 @@ class _MyAppState extends ConsumerState<MyApp> with WidgetsBindingObserver {
},
);
}

late String? sharedText = '';

void listenReceiveSharing() {
// For sharing or opening urls/text coming from outside the app while the app is in the memory
ReceiveSharingIntent.getTextStream().listen((String value) {
sharedText = value;
logger.d('getTextStream Shared: $sharedText');
_goPage(sharedText ?? '', replace: false);
}, onError: (err) {
logger.e('getTextStream error: $err');
});

// For sharing or opening urls/text coming from outside the app while the app is closed
ReceiveSharingIntent.getInitialText().then((String? value) {
sharedText = value ?? '';
logger.v('Shared: $sharedText');
_goPage(sharedText ?? '');
});
}

Future<void> _goPage(String url, {bool replace = true}) async {
if (url.isEmpty || !RouteUtil.goGalleryByUrl(ref, url, replace: replace)) {
0
.milliseconds
.delay()
.then((value) => erosRouter.replaceNamed(NHRoutes.home));
}
}
}

class LoggerObserver extends ProviderObserver {
Expand Down
90 changes: 26 additions & 64 deletions lib/pages/nav/index/index_view.dart
Original file line number Diff line number Diff line change
@@ -1,22 +1,28 @@
import 'package:auto_route/auto_route.dart';
import 'package:easy_debounce/easy_debounce.dart';
import 'package:eros_n/common/global.dart';
import 'package:eros_n/common/provider/receive_sharing_provider.dart';
import 'package:eros_n/generated/l10n.dart';
import 'package:eros_n/pages/nav/favorite/favorite_view.dart';
import 'package:eros_n/pages/nav/front/front_view.dart';
import 'package:eros_n/pages/nav/history/history_view.dart';
import 'package:eros_n/pages/nav/index/index_state.dart';
import 'package:eros_n/pages/nav/more/more_view.dart';
import 'package:eros_n/pages/nav/search/search_view.dart';
import 'package:eros_n/routes/routes.dart';
import 'package:eros_n/utils/get_utils/extensions/export.dart';
import 'package:flutter/material.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';

import '../../../utils/logger.dart';
import 'index_provider.dart';

class IndexPage extends HookConsumerWidget {
IndexPage({super.key});
class IndexPage extends StatefulHookConsumerWidget {
const IndexPage({super.key});

@override
ConsumerState<IndexPage> createState() => _IndexPageState();
}

class _IndexPageState extends ConsumerState<IndexPage> {
final pages = <Widget>[
const FrontPage(),
const FavoritePage(),
Expand All @@ -26,67 +32,23 @@ class IndexPage extends HookConsumerWidget {
];

@override
Widget build(BuildContext context, WidgetRef ref) {
logger.v('build IndexPage');
final state = ref.watch(indexProvider);
void initState() {
logger.d('initState');
super.initState();
WidgetsBinding.instance.addPostFrameCallback((_) {
EasyDebounce.debounce(
'listenReceiveSharing',
const Duration(milliseconds: 500),
() =>
ref.read(receiveSharingProvider.notifier).listenReceiveSharing(ref),
);
});
}

// return AutoTabsRouter.pageView(
// routes: [
// FrontRoute(),
// FavoriteRoute(),
// SearchRoute(query: ''),
// HistoryRoute(),
// MoreRoute(),
// ],
// builder: (context, child, _) {
// final IndexState state = ref.watch(indexProvider);
//
// return Scaffold(
// body: child,
// bottomNavigationBar: AnimatedContainer(
// height: state.hideNavigationBar
// ? 0
// : 80 + context.mediaQueryPadding.bottom,
// duration: 300.milliseconds,
// curve: Curves.ease,
// child: NavigationBar(
// selectedIndex: state.selectedIndex,
// // labelBehavior: NavigationDestinationLabelBehavior.alwaysHide,
// // height: 48,
// destinations: [
// NavigationDestination(
// icon: const Icon(Icons.home_outlined),
// selectedIcon: const Icon(Icons.home),
// label: L10n.of(context).home,
// ),
// NavigationDestination(
// icon: const Icon(Icons.favorite_outline),
// selectedIcon: const Icon(Icons.favorite),
// label: L10n.of(context).favorites,
// ),
// NavigationDestination(
// icon: const Icon(Icons.search),
// label: L10n.of(context).search,
// ),
// NavigationDestination(
// icon: const Icon(Icons.history),
// label: L10n.of(context).history,
// ),
// NavigationDestination(
// icon: const Icon(Icons.more_horiz),
// label: L10n.of(context).more,
// ),
// ],
// onDestinationSelected: (index) {
// ref
// .read(indexProvider.notifier)
// .setIndex(index, context: context, jumpToPage: true);
// },
// ),
// ),
// );
// },
// );
@override
Widget build(BuildContext context) {
logger.e('build IndexPage');
final state = ref.watch(indexProvider);

return Scaffold(
body: PageView(
Expand Down
9 changes: 2 additions & 7 deletions lib/pages/splash/splash_view.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import 'dart:async';

import 'package:eros_n/common/global.dart';
import 'package:eros_n/routes/routes.dart';
import 'package:eros_n/utils/get_utils/get_utils.dart';
Expand All @@ -14,13 +12,11 @@ class SplashPage extends StatefulHookConsumerWidget {
}

class _SplashPageState extends ConsumerState<SplashPage> {
StreamSubscription? _intentDataStreamSubscription;
late String? sharedText = '';

@override
void initState() {
super.initState();
0

10
.milliseconds
.delay()
.then((value) => erosRouter.replaceNamed(NHRoutes.home));
Expand All @@ -29,7 +25,6 @@ class _SplashPageState extends ConsumerState<SplashPage> {
@override
void dispose() {
super.dispose();
_intentDataStreamSubscription?.cancel();
}

@override
Expand Down
8 changes: 4 additions & 4 deletions lib/routes/routes.dart
Original file line number Diff line number Diff line change
Expand Up @@ -144,20 +144,20 @@ class RouteUtil {
}
}

static bool goGalleryByUrl(
static Future<bool> goGalleryByUrl(
WidgetRef ref,
String url, {
bool replace = false,
}) {
}) async {
final RegExp regGalleryUrl = RegExp(r'https?://nhentai.net/g/(\d+)/?');
final RegExp regGalleryPageUrl = RegExp(r'https://nhentai.net/g/(\d+)/\d+');

if (regGalleryUrl.hasMatch(url)) {
final String gid = regGalleryUrl.firstMatch(url)!.group(1)!;
RouteUtil.goGalleryByGid(ref, int.parse(gid), replace: replace);
await RouteUtil.goGalleryByGid(ref, int.parse(gid), replace: replace);
} else if (regGalleryPageUrl.hasMatch(url)) {
final String gid = regGalleryPageUrl.firstMatch(url)!.group(1)!;
RouteUtil.goGalleryByGid(ref, int.parse(gid), replace: replace);
await RouteUtil.goGalleryByGid(ref, int.parse(gid), replace: replace);
} else {
return false;
}
Expand Down
7 changes: 7 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,13 @@ packages:
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.5.4"
easy_debounce:
dependency: "direct main"
description:
name: easy_debounce
url: "https://pub.flutter-io.cn"
source: hosted
version: "2.0.2+1"
easy_image_viewer:
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 @@ -102,6 +102,7 @@ dependencies:
wakelock: ^0.6.2
uuid: ^3.0.6
animations: ^2.0.7
easy_debounce: ^2.0.2+1

dev_dependencies:
# flutter pub run apn_json2model
Expand Down

0 comments on commit b0b2fb6

Please sign in to comment.