Skip to content

Commit

Permalink
Merge pull request #249 from humhub/f-inforisque-6
Browse files Browse the repository at this point in the history
Mechanic for redirecting on r.email provider
  • Loading branch information
marc-farre authored Oct 21, 2024
2 parents 14b3459 + 603cc35 commit 6643251
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 77 deletions.
37 changes: 10 additions & 27 deletions lib/flavored/util/intent_plugin.f.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -27,7 +27,6 @@ class IntentPluginF extends ConsumerStatefulWidget {

class IntentPluginFState extends ConsumerState<IntentPluginF> {
StreamSubscription? intentDataStreamSubscription;
List<SharedMediaFile>? sharedFiles;
Object? _err;
Uri? _initialUri;
Uri? _latestUri;
Expand All @@ -37,20 +36,11 @@ class IntentPluginFState extends ConsumerState<IntentPluginF> {
void initState() {
logInfo([_err, _initialUri, _latestUri, _sub]);
super.initState();
intentDataStreamSubscription = ReceiveSharingIntent.getMediaStream().listen((List<SharedMediaFile> value) {
setState(() {
sharedFiles = value;
});
});

// For sharing images coming from outside the app while the app is closed
ReceiveSharingIntent.getInitialMedia().then((List<SharedMediaFile> value) {
setState(() {
sharedFiles = value;
});
WidgetsBinding.instance.addPostFrameCallback((_) {
_handleInitialUri();
_subscribeToUriStream();
});
_handleInitialUri();
_handleIncomingLinks();
}

@override
Expand All @@ -60,7 +50,7 @@ class IntentPluginFState extends ConsumerState<IntentPluginF> {

/// Handle incoming links - the ones that the app will recieve from the OS
/// while already started.
Future<void> _handleIncomingLinks() async {
Future<void> _subscribeToUriStream() async {
if (!kIsWeb) {
// It will handle app links while the app is already started - be it in
// the foreground or in the background.
Expand Down Expand Up @@ -91,25 +81,18 @@ class IntentPluginFState extends ConsumerState<IntentPluginF> {
// 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
Expand Down
4 changes: 2 additions & 2 deletions lib/flavored/util/notifications/channel.f.dart
Original file line number Diff line number Diff line change
@@ -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(
Expand Down Expand Up @@ -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);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions lib/flavored/web_view.f.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -100,7 +100,7 @@ class FlavoredWebViewState extends ConsumerState<WebViewF> {
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;
Expand Down
4 changes: 2 additions & 2 deletions lib/pages/opener.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -41,7 +41,7 @@ class OpenerState extends ConsumerState<Opener> 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);
}
Expand Down
4 changes: 2 additions & 2 deletions lib/pages/web_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -127,7 +127,7 @@ class WebViewAppState extends ConsumerState<WebView> {
_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),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class InitFromPush {
class InitFromUrl {
static String? _redirectUrlFromInit;

static setPayload(String payload) {
Expand Down
48 changes: 9 additions & 39 deletions lib/util/intent/intent_plugin.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,10 @@ class IntentPluginState extends ConsumerState<IntentPlugin> {
void initState() {
logInfo([_err, _initialUri, _latestUri, _sub]);
super.initState();
intentDataStreamSubscription = ReceiveSharingIntent.getMediaStream().listen((List<SharedMediaFile> value) {
setState(() {
sharedFiles = value;
});
});

// For sharing images coming from outside the app while the app is closed
ReceiveSharingIntent.getInitialMedia().then((List<SharedMediaFile> value) {
setState(() {
sharedFiles = value;
});
WidgetsBinding.instance.addPostFrameCallback((_) {
_handleInitialUri();
_subscribeToUriStream();
});
_handleInitialUri();
_handleIncomingLinks();
}

@override
Expand All @@ -61,7 +51,7 @@ class IntentPluginState extends ConsumerState<IntentPlugin> {

/// Handle incoming links - the ones that the app will recieve from the OS
/// while already started.
Future<void> _handleIncomingLinks() async {
Future<void> _subscribeToUriStream() async {
if (!kIsWeb) {
// It will handle app links while the app is already started - be it in
// the foreground or in the background.
Expand Down Expand Up @@ -99,20 +89,14 @@ class IntentPluginState extends ConsumerState<IntentPlugin> {
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
Expand Down Expand Up @@ -142,17 +126,3 @@ class IntentPluginState extends ConsumerState<IntentPlugin> {
return isNewRouteSameAsCurrent;
}
}

class InitFromIntent {
static String? _redirectUrl;

static setPayloadForInit(String payload) {
_redirectUrl = payload;
}

static String? usePayloadForInit() {
String? payload = _redirectUrl;
_redirectUrl = null;
return payload;
}
}
4 changes: 2 additions & 2 deletions lib/util/notifications/channel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -37,7 +37,7 @@ class NotificationChannel {
navigatorKey.currentState!.pushNamed(WebView.path, arguments: opener);
} else {
if (payload != null) {
InitFromPush.setPayload(payload);
InitFromUrl.setPayload(payload);
}
}
}
Expand Down

0 comments on commit 6643251

Please sign in to comment.