Skip to content

Commit

Permalink
Merge pull request #157 from humhub/145-bypass-the-form-url-when-the-…
Browse files Browse the repository at this point in the history
…app-is-opened-from-a-clicked-link-or-a-push-notification

145 bypass the form url when the app is opened from a clicked link or a push notification
  • Loading branch information
luke- authored Feb 6, 2024
2 parents b9f8b24 + 7917b3a commit fa20fef
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 8 deletions.
9 changes: 8 additions & 1 deletion lib/pages/opener.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:humhub/pages/web_view.dart';
import 'package:humhub/util/const.dart';
import 'package:humhub/util/form_helper.dart';
import 'package:humhub/util/intent/intent_plugin.dart';
import 'package:humhub/util/notifications/channel.dart';
import 'package:humhub/util/opener_controller.dart';
import 'package:humhub/util/providers.dart';
import 'package:rive/rive.dart';
Expand Down Expand Up @@ -41,12 +43,17 @@ class OpenerState extends ConsumerState<Opener> with SingleTickerProviderStateMi
_animationReverse = SimpleAnimation('animation', autoplay: true);
_controllerReverse = _animationReverse;

WidgetsBinding.instance.addPostFrameCallback((_) {
WidgetsBinding.instance.addPostFrameCallback((_) async {
Future.delayed(const Duration(milliseconds: 700), () {
setState(() {
_textFieldAddInfoVisibility = true;
});
});

String? urlIntent = InitFromIntent.usePayloadForInit();
if(urlIntent != null){
await RedirectNotificationChannel().onTap(urlIntent);
}
});
}

Expand Down
2 changes: 1 addition & 1 deletion lib/pages/web_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ class WebViewAppState extends ConsumerState<WebViewApp> {
manifest = manifestPush.manifest;
url = manifestPush.remoteMessage.data['url'];
}
String? payloadFromPush = RedirectUrlFromInit.usePayloadForInit();
String? payloadFromPush = InitFromPush.usePayload();
if (payloadFromPush != null) url = payloadFromPush;
return URLRequest(url: Uri.parse(url ?? manifest.baseUrl), headers: ref.read(humHubProvider).customHeaders);
}
Expand Down
23 changes: 21 additions & 2 deletions lib/util/intent/intent_plugin.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:humhub/pages/web_view.dart';
import 'package:humhub/util/notifications/channel.dart';
import 'package:humhub/util/router.dart';
import 'package:humhub/util/universal_opener_controller.dart';
import 'package:loggy/loggy.dart';
Expand Down Expand Up @@ -92,6 +91,7 @@ class IntentPluginState extends ConsumerState<IntentPlugin> {
// 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 {
Expand All @@ -102,10 +102,14 @@ class IntentPluginState extends ConsumerState<IntentPlugin> {
_latestUri = uri;
String? redirectUrl = uri.queryParameters['url'];
if (redirectUrl != null && navigatorKey.currentState != null) {
logInfo('MD22 IF open: $_initialUriIsHandled');
tryNavigateWithOpener(redirectUrl);
} else {
if (redirectUrl != null) {
RedirectUrlFromInit.setPayloadForInit(redirectUrl);
UniversalOpenerController opener = UniversalOpenerController(url: redirectUrl);
await opener.initHumHub();
navigatorKey.currentState!.pushNamed(WebViewApp.path, arguments: opener);
return;
}
}
} on PlatformException {
Expand All @@ -120,6 +124,7 @@ class IntentPluginState extends ConsumerState<IntentPlugin> {
}

Future<bool> tryNavigateWithOpener(String redirectUrl) async {
logInfo('MD initHumHub payload: $redirectUrl');
bool isNewRouteSameAsCurrent = false;
navigatorKey.currentState!.popUntil((route) {
if (route.settings.name == WebViewApp.path) {
Expand All @@ -134,3 +139,17 @@ 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;
}
}
9 changes: 5 additions & 4 deletions lib/util/notifications/channel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class RedirectNotificationChannel extends NotificationChannel {
}
return true;
});
// TODO: Check here what does it happen if the WebView and Manifest is not init and we want to open it from the opener.
UniversalOpenerController opener = UniversalOpenerController(url: payload);
await opener.initHumHub();
if (isNewRouteSameAsCurrent) {
Expand All @@ -59,20 +60,20 @@ class RedirectNotificationChannel extends NotificationChannel {
navigatorKey.currentState!.pushNamed(WebViewApp.path, arguments: opener);
} else {
if (payload != null) {
RedirectUrlFromInit.setPayloadForInit(payload);
InitFromPush.setPayload(payload);
}
}
}
}

class RedirectUrlFromInit {
class InitFromPush {
static String? _redirectUrlFromInit;

static setPayloadForInit(String payload) {
static setPayload(String payload) {
_redirectUrlFromInit = payload;
}

static String? usePayloadForInit() {
static String? usePayload() {
String? payload = _redirectUrlFromInit;
_redirectUrlFromInit = null;
return payload;
Expand Down

0 comments on commit fa20fef

Please sign in to comment.