From 603cc35f27ad090a00f480356801ebe240ab589b Mon Sep 17 00:00:00 2001 From: primozratej Date: Mon, 21 Oct 2024 19:06:54 +0200 Subject: [PATCH] Mechanic for redirecting on r.email provider --- lib/flavored/util/intent_plugin.f.dart | 37 ++++---------- .../util/notifications/channel.f.dart | 4 +- lib/flavored/web_view.f.dart | 4 +- lib/pages/opener.dart | 4 +- lib/pages/web_view.dart | 4 +- ...init_from_push.dart => init_from_url.dart} | 2 +- lib/util/intent/intent_plugin.dart | 48 ++++--------------- lib/util/notifications/channel.dart | 4 +- 8 files changed, 30 insertions(+), 77 deletions(-) rename lib/util/{notifications/init_from_push.dart => init_from_url.dart} (92%) diff --git a/lib/flavored/util/intent_plugin.f.dart b/lib/flavored/util/intent_plugin.f.dart index 9fad039..5bf83d2 100644 --- a/lib/flavored/util/intent_plugin.f.dart +++ b/lib/flavored/util/intent_plugin.f.dart @@ -6,9 +6,9 @@ import 'package:flutter/services.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:humhub/flavored/web_view.f.dart'; import 'package:humhub/util/const.dart'; +import 'package:humhub/util/init_from_url.dart'; import 'package:humhub/util/loading_provider.dart'; import 'package:loggy/loggy.dart'; -import 'package:receive_sharing_intent/receive_sharing_intent.dart'; import 'package:uni_links/uni_links.dart'; bool _initialUriIsHandled = false; @@ -27,7 +27,6 @@ class IntentPluginF extends ConsumerStatefulWidget { class IntentPluginFState extends ConsumerState { StreamSubscription? intentDataStreamSubscription; - List? sharedFiles; Object? _err; Uri? _initialUri; Uri? _latestUri; @@ -37,20 +36,11 @@ class IntentPluginFState extends ConsumerState { void initState() { logInfo([_err, _initialUri, _latestUri, _sub]); super.initState(); - intentDataStreamSubscription = ReceiveSharingIntent.getMediaStream().listen((List value) { - setState(() { - sharedFiles = value; - }); - }); - // For sharing images coming from outside the app while the app is closed - ReceiveSharingIntent.getInitialMedia().then((List value) { - setState(() { - sharedFiles = value; - }); + WidgetsBinding.instance.addPostFrameCallback((_) { + _handleInitialUri(); + _subscribeToUriStream(); }); - _handleInitialUri(); - _handleIncomingLinks(); } @override @@ -60,7 +50,7 @@ class IntentPluginFState extends ConsumerState { /// Handle incoming links - the ones that the app will recieve from the OS /// while already started. - Future _handleIncomingLinks() async { + Future _subscribeToUriStream() async { if (!kIsWeb) { // It will handle app links while the app is already started - be it in // the foreground or in the background. @@ -91,25 +81,18 @@ class IntentPluginFState extends ConsumerState { // In this example app this is an almost useless guard, but it is here to // show we are not going to call getInitialUri multiple times, even if this // was a widget that will be disposed of (ex. a navigation route change). - if (!_initialUriIsHandled) { _initialUriIsHandled = true; try { - final uri = await getInitialUri(); - if (uri == null || !mounted) return; + Uri? uri = await getInitialUri(); + if (uri == null) return; setState(() => _initialUri = uri); - if (!mounted) { - return; - } _latestUri = uri; - String? redirectUrl = uri.queryParameters['url']; - if (redirectUrl != null && navigatorKey.currentState != null) { + String? redirectUrl = uri.toString(); + if (navigatorKey.currentState != null) { tryNavigateWithOpener(redirectUrl); } else { - if (redirectUrl != null) { - navigatorKey.currentState!.pushNamed(WebViewF.path, arguments: redirectUrl); - return; - } + InitFromUrl.setPayload(redirectUrl); } } on PlatformException { // Platform messages may fail but we ignore the exception diff --git a/lib/flavored/util/notifications/channel.f.dart b/lib/flavored/util/notifications/channel.f.dart index 69b264d..6c9e90b 100644 --- a/lib/flavored/util/notifications/channel.f.dart +++ b/lib/flavored/util/notifications/channel.f.dart @@ -1,7 +1,7 @@ import 'package:humhub/flavored/web_view.f.dart'; import 'package:humhub/util/const.dart'; +import 'package:humhub/util/init_from_url.dart'; import 'package:humhub/util/notifications/channel.dart'; -import 'package:humhub/util/notifications/init_from_push.dart'; class NotificationChannelF extends NotificationChannel { const NotificationChannelF( @@ -29,7 +29,7 @@ class NotificationChannelF extends NotificationChannel { navigatorKey.currentState!.pushNamed(WebViewF.path, arguments: payload); } else { if (payload != null) { - InitFromPush.setPayload(payload); + InitFromUrl.setPayload(payload); } } } diff --git a/lib/flavored/web_view.f.dart b/lib/flavored/web_view.f.dart index 3a655b3..cdea1b3 100644 --- a/lib/flavored/web_view.f.dart +++ b/lib/flavored/web_view.f.dart @@ -13,8 +13,8 @@ import 'package:humhub/models/channel_message.dart'; import 'package:humhub/util/black_list_rules.dart'; import 'package:humhub/util/const.dart'; import 'package:humhub/util/extensions.dart'; +import 'package:humhub/util/init_from_url.dart'; import 'package:humhub/util/loading_provider.dart'; -import 'package:humhub/util/notifications/init_from_push.dart'; import 'package:humhub/util/push/provider.dart'; import 'package:humhub/util/show_dialog.dart'; import 'package:humhub/util/web_view_global_controller.dart'; @@ -100,7 +100,7 @@ class FlavoredWebViewState extends ConsumerState { URLRequest get _initialRequest { var payload = ModalRoute.of(context)!.settings.arguments; String? url = instance.manifest.startUrl; - String? payloadForInitFromPush = InitFromPush.usePayload(); + String? payloadForInitFromPush = InitFromUrl.usePayload(); String? payloadFromPush; if (payload is String) payloadFromPush = payload; if (payloadForInitFromPush != null) url = payloadForInitFromPush; diff --git a/lib/pages/opener.dart b/lib/pages/opener.dart index 2e8ed3d..072d24e 100644 --- a/lib/pages/opener.dart +++ b/lib/pages/opener.dart @@ -5,7 +5,7 @@ import 'package:flutter_gen/gen_l10n/app_localizations.dart'; import 'package:humhub/pages/help/help.dart'; import 'package:humhub/pages/web_view.dart'; import 'package:humhub/util/const.dart'; -import 'package:humhub/util/intent/intent_plugin.dart'; +import 'package:humhub/util/init_from_url.dart'; import 'package:humhub/util/notifications/channel.dart'; import 'package:humhub/util/openers/opener_controller.dart'; import 'package:humhub/util/providers.dart'; @@ -41,7 +41,7 @@ class OpenerState extends ConsumerState with SingleTickerProviderStateMi ref.read(languageSwitcherVisibilityProvider.notifier).toggleVisibility(true); }); - String? urlIntent = InitFromIntent.usePayloadForInit(); + String? urlIntent = InitFromUrl.usePayload(); if (urlIntent != null) { await ref.read(notificationChannelProvider).value!.onTap(urlIntent); } diff --git a/lib/pages/web_view.dart b/lib/pages/web_view.dart index a54e517..a1271c2 100644 --- a/lib/pages/web_view.dart +++ b/lib/pages/web_view.dart @@ -16,8 +16,8 @@ import 'package:humhub/util/connectivity_plugin.dart'; import 'package:humhub/util/const.dart'; import 'package:humhub/util/extensions.dart'; import 'package:humhub/util/file_handler.dart'; +import 'package:humhub/util/init_from_url.dart'; import 'package:humhub/util/loading_provider.dart'; -import 'package:humhub/util/notifications/init_from_push.dart'; import 'package:humhub/util/providers.dart'; import 'package:humhub/util/openers/universal_opener_controller.dart'; import 'package:humhub/util/push/provider.dart'; @@ -127,7 +127,7 @@ class WebViewAppState extends ConsumerState { _manifest = manifestPush.manifest; url = manifestPush.remoteMessage.data['url']; } - String? payloadFromPush = InitFromPush.usePayload(); + String? payloadFromPush = InitFromUrl.usePayload(); if (payloadFromPush != null) url = payloadFromPush; return URLRequest( url: WebUri(url ?? _manifest.startUrl), diff --git a/lib/util/notifications/init_from_push.dart b/lib/util/init_from_url.dart similarity index 92% rename from lib/util/notifications/init_from_push.dart rename to lib/util/init_from_url.dart index a0e4102..4052d92 100644 --- a/lib/util/notifications/init_from_push.dart +++ b/lib/util/init_from_url.dart @@ -1,4 +1,4 @@ -class InitFromPush { +class InitFromUrl { static String? _redirectUrlFromInit; static setPayload(String payload) { diff --git a/lib/util/intent/intent_plugin.dart b/lib/util/intent/intent_plugin.dart index ee9f779..44c4e79 100644 --- a/lib/util/intent/intent_plugin.dart +++ b/lib/util/intent/intent_plugin.dart @@ -38,20 +38,10 @@ class IntentPluginState extends ConsumerState { void initState() { logInfo([_err, _initialUri, _latestUri, _sub]); super.initState(); - intentDataStreamSubscription = ReceiveSharingIntent.getMediaStream().listen((List value) { - setState(() { - sharedFiles = value; - }); - }); - - // For sharing images coming from outside the app while the app is closed - ReceiveSharingIntent.getInitialMedia().then((List value) { - setState(() { - sharedFiles = value; - }); + WidgetsBinding.instance.addPostFrameCallback((_) { + _handleInitialUri(); + _subscribeToUriStream(); }); - _handleInitialUri(); - _handleIncomingLinks(); } @override @@ -61,7 +51,7 @@ class IntentPluginState extends ConsumerState { /// Handle incoming links - the ones that the app will recieve from the OS /// while already started. - Future _handleIncomingLinks() async { + Future _subscribeToUriStream() async { if (!kIsWeb) { // It will handle app links while the app is already started - be it in // the foreground or in the background. @@ -99,20 +89,14 @@ class IntentPluginState extends ConsumerState { final uri = await getInitialUri(); if (uri == null || !mounted) return; setState(() => _initialUri = uri); - if (!mounted) { - return; - } _latestUri = uri; - String? redirectUrl = uri.queryParameters['url']; - if (redirectUrl != null && navigatorKey.currentState != null) { + String? redirectUrl = uri.toString(); + if (navigatorKey.currentState != null) { tryNavigateWithOpener(redirectUrl); } else { - if (redirectUrl != null) { - UniversalOpenerController opener = UniversalOpenerController(url: redirectUrl); - await opener.initHumHub(); - navigatorKey.currentState!.pushNamed(WebView.path, arguments: opener); - return; - } + UniversalOpenerController opener = UniversalOpenerController(url: redirectUrl); + await opener.initHumHub(); + navigatorKey.currentState!.pushNamed(WebView.path, arguments: opener); } } on PlatformException { // Platform messages may fail but we ignore the exception @@ -142,17 +126,3 @@ class IntentPluginState extends ConsumerState { return isNewRouteSameAsCurrent; } } - -class InitFromIntent { - static String? _redirectUrl; - - static setPayloadForInit(String payload) { - _redirectUrl = payload; - } - - static String? usePayloadForInit() { - String? payload = _redirectUrl; - _redirectUrl = null; - return payload; - } -} diff --git a/lib/util/notifications/channel.dart b/lib/util/notifications/channel.dart index 659e902..430bab6 100644 --- a/lib/util/notifications/channel.dart +++ b/lib/util/notifications/channel.dart @@ -2,7 +2,7 @@ import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:humhub/flavored/util/notifications/channel.f.dart'; import 'package:humhub/pages/web_view.dart'; import 'package:humhub/util/const.dart'; -import 'package:humhub/util/notifications/init_from_push.dart'; +import 'package:humhub/util/init_from_url.dart'; import 'package:humhub/util/openers/universal_opener_controller.dart'; import 'package:package_info_plus/package_info_plus.dart'; @@ -37,7 +37,7 @@ class NotificationChannel { navigatorKey.currentState!.pushNamed(WebView.path, arguments: opener); } else { if (payload != null) { - InitFromPush.setPayload(payload); + InitFromUrl.setPayload(payload); } } }