From 260e151e878f64dd49b2bbb8598a4b871fda66fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Farr=C3=A9?= Date: Thu, 12 Dec 2024 14:41:34 +0000 Subject: [PATCH] For Multi-Instance apps, add a new item in the account menu to display the Opener app page, allowing to switch to the other HumHub instance --- Events.php | 23 ++++++++++++++++++++++- config.php | 11 +++++++---- controllers/MobileAppController.php | 10 ++++++++++ docs/CHANGELOG.md | 1 + helpers/MobileAppHelper.php | 9 ++++++++- 5 files changed, 48 insertions(+), 6 deletions(-) diff --git a/Events.php b/Events.php index 7a3c08e..718987c 100644 --- a/Events.php +++ b/Events.php @@ -11,6 +11,8 @@ use humhub\modules\fcmPush\services\DriverService; use humhub\modules\fcmPush\widgets\PushNotificationInfoWidget; use humhub\modules\notification\targets\MobileTargetProvider; +use humhub\modules\ui\menu\MenuLink; +use humhub\modules\user\widgets\AccountTopMenu; use humhub\modules\user\widgets\AuthChoice; use humhub\modules\web\pwa\controllers\ManifestController; use humhub\modules\web\pwa\controllers\ServiceWorkerController; @@ -90,7 +92,7 @@ public static function onLayoutAddonInit($event) { if (Yii::$app->session->has(self::SESSION_VAR_LOGOUT)) { MobileAppHelper::unregisterNotificationScript(); // Before Logout - MobileAppHelper::registerLogoutScript(); + MobileAppHelper::registerShowOpenerScript(); Yii::$app->session->remove(self::SESSION_VAR_LOGOUT); } @@ -137,4 +139,23 @@ public static function onAuthChoiceBeforeRun(Event $event) $sender->setClients([]); } } + + public static function onAccountTopMenuInit(Event $event) + { + if (!MobileAppHelper::isMultiInstanceApp()) { + return; + } + + /** @var AccountTopMenu $menu */ + $menu = $event->sender; + + $menu->addEntry(new MenuLink([ + 'label' => Yii::t('FcmPushModule.base', 'Switch network'), + 'url' => ['/fcm-push/mobile-app/instance-opener'], + 'icon' => 'arrows-h', + 'sortOrder' => 699, // Just before "Logout" + 'isActive' => true, + 'isVisible' => true, + ])); + } } diff --git a/config.php b/config.php index 4216cf3..1088e16 100644 --- a/config.php +++ b/config.php @@ -3,13 +3,15 @@ /** @noinspection MissedFieldInspection */ -use humhub\modules\fcmPush\Events; use humhub\components\Controller; -//use humhub\modules\notification\widgets\NotificationInfoWidget; +use humhub\modules\fcmPush\Events; +use humhub\modules\user\components\User; +use humhub\modules\user\widgets\AccountTopMenu; use humhub\modules\user\widgets\AuthChoice; use humhub\widgets\LayoutAddons; use yii\base\Application; -use humhub\modules\user\components\User; + +//use humhub\modules\notification\widgets\NotificationInfoWidget; return [ 'id' => 'fcm-push', @@ -23,7 +25,8 @@ [User::class, User::EVENT_AFTER_LOGOUT, [Events::class, 'onAfterLogout']], [User::class, User::EVENT_AFTER_LOGIN, [Events::class, 'onAfterLogin']], [AuthChoice::class, AuthChoice::EVENT_BEFORE_RUN, [Events::class, 'onAuthChoiceBeforeRun']], - //[NotificationInfoWidget::class, \humhub\widgets\BaseStack::EVENT_RUN, [Events::class, 'onNotificationInfoWidget']] + //[NotificationInfoWidget::class, \humhub\widgets\BaseStack::EVENT_RUN, [Events::class, 'onNotificationInfoWidget']], + [AccountTopMenu::class, AccountTopMenu::EVENT_INIT, [Events::class, 'onAccountTopMenuInit']], ], 'consoleControllerMap' => [ 'firebase' => 'humhub\modules\fcmPush\commands\SendController', diff --git a/controllers/MobileAppController.php b/controllers/MobileAppController.php index 068d498..00efbe4 100644 --- a/controllers/MobileAppController.php +++ b/controllers/MobileAppController.php @@ -4,6 +4,7 @@ use humhub\modules\admin\components\Controller; use humhub\modules\admin\notifications\NewVersionAvailable; +use humhub\modules\fcmPush\helpers\MobileAppHelper; use humhub\modules\fcmPush\models\FcmUser; use humhub\modules\user\models\User; use Yii; @@ -43,4 +44,13 @@ public function actionIndex() return $this->render('index'); } + public function actionInstanceOpener() + { + // Send to the mobile app to display the instance opener + MobileAppHelper::registerShowOpenerScript(); + + // Stay in the same page, for when we come back from the mobile app to this instance + return $this->redirect(Yii::$app->request->referrer); + } + } diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 4ba8552..8c306b8 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -4,6 +4,7 @@ Changelog 2.1.1 (unreleased) ------------------------ - Fix #58: iOS mobile app detection for iPad devices +- Enh: For Multi-Instance apps, add a new item in the account menu to display the Opener app page, allowing to switch to the other HumHub instance 2.1.0 (October 21, 2024) ------------------------ diff --git a/helpers/MobileAppHelper.php b/helpers/MobileAppHelper.php index 9d655cb..a4078f0 100644 --- a/helpers/MobileAppHelper.php +++ b/helpers/MobileAppHelper.php @@ -21,7 +21,7 @@ public static function registerLoginScript() self::sendFlutterMessage($message); } - public static function registerLogoutScript() + public static function registerShowOpenerScript() { if (!static::isAppRequest()) { return; @@ -90,4 +90,11 @@ public static function isIosApp(): bool static::isAppRequest() && Yii::$app->request->headers->get('x-humhub-app-is-ios'); } + + public static function isMultiInstanceApp(): bool + { + return + static::isAppRequest() + && Yii::$app->request->headers->get('x-humhub-app-is-multi-instance'); + } }