Skip to content

Commit

Permalink
Fix Show Opener and introduce the openerState
Browse files Browse the repository at this point in the history
  • Loading branch information
marc-farre committed Dec 13, 2024
1 parent 85164a9 commit 8b337ce
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 62 deletions.
41 changes: 27 additions & 14 deletions Events.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@

class Events
{
private const SESSION_VAR_LOGOUT = 'mobileAppHandleLogout';
private const SESSION_VAR_LOGIN = 'mobileAppHandleLogin';

public static function onBeforeRequest()
{
/** @var Module $module */
Expand Down Expand Up @@ -90,16 +87,30 @@ public static function onNotificationInfoWidget($event)

public static function onLayoutAddonInit($event)
{
if (Yii::$app->session->has(self::SESSION_VAR_LOGOUT)) {
MobileAppHelper::unregisterNotificationScript(); // Before Logout
MobileAppHelper::registerShowOpenerScript();
Yii::$app->session->remove(self::SESSION_VAR_LOGOUT);
// If the mobile app Opener page is open (after login and switching instance)
if (Yii::$app->session->has(MobileAppHelper::SESSION_VAR_HIDE_OPENER)) {
MobileAppHelper::registerHideOpenerScript();
Yii::$app->session->remove(MobileAppHelper::SESSION_VAR_HIDE_OPENER);
} elseif (MobileAppHelper::openerState()) {
MobileAppHelper::registerHideOpenerScript();
}

if (Yii::$app->session->has(self::SESSION_VAR_LOGIN)) {
MobileAppHelper::registerLoginScript();
// After login
if (Yii::$app->session->has(MobileAppHelper::SESSION_VAR_REGISTER_NOTIFICATION)) {
MobileAppHelper::registerNotificationScript();
Yii::$app->session->remove(self::SESSION_VAR_LOGIN);
Yii::$app->session->remove(MobileAppHelper::SESSION_VAR_REGISTER_NOTIFICATION);
}

// Before logout
if (Yii::$app->session->has(MobileAppHelper::SESSION_VAR_UNREGISTER_NOTIFICATION)) {
MobileAppHelper::unregisterNotificationScript();
Yii::$app->session->remove(MobileAppHelper::SESSION_VAR_UNREGISTER_NOTIFICATION);
}

// After logout
if (Yii::$app->session->has(MobileAppHelper::SESSION_VAR_SHOW_OPENER)) {
MobileAppHelper::registerShowOpenerScript();
Yii::$app->session->remove(MobileAppHelper::SESSION_VAR_SHOW_OPENER);
}

if (Yii::$app->user->isGuest) {
Expand All @@ -117,14 +128,16 @@ public static function onLayoutAddonInit($event)
FirebaseAsset::register(Yii::$app->view);
}

public static function onAfterLogout()
public static function onAfterLogin()
{
Yii::$app->session->set(self::SESSION_VAR_LOGOUT, 1);
Yii::$app->session->set(MobileAppHelper::SESSION_VAR_HIDE_OPENER, 1);
Yii::$app->session->set(MobileAppHelper::SESSION_VAR_REGISTER_NOTIFICATION, 1);
}

public static function onAfterLogin()
public static function onAfterLogout()
{
Yii::$app->session->set(self::SESSION_VAR_LOGIN, 1);
Yii::$app->session->set(MobileAppHelper::SESSION_VAR_UNREGISTER_NOTIFICATION, 1);
Yii::$app->session->set(MobileAppHelper::SESSION_VAR_SHOW_OPENER, 1);
}

public static function onAuthChoiceBeforeRun(Event $event)
Expand Down
2 changes: 1 addition & 1 deletion config.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
['humhub\modules\web\pwa\controllers\ServiceWorkerController', Controller::EVENT_INIT, [Events::class, 'onServiceWorkerControllerInit']],
[LayoutAddons::class, LayoutAddons::EVENT_INIT, [Events::class, 'onLayoutAddonInit']],
[Application::class, Application::EVENT_BEFORE_REQUEST, [Events::class, 'onBeforeRequest']],
[User::class, User::EVENT_AFTER_LOGOUT, [Events::class, 'onAfterLogout']],
[User::class, User::EVENT_AFTER_LOGIN, [Events::class, 'onAfterLogin']],
[User::class, User::EVENT_AFTER_LOGOUT, [Events::class, 'onAfterLogout']],
[AuthChoice::class, AuthChoice::EVENT_BEFORE_RUN, [Events::class, 'onAuthChoiceBeforeRun']],
//[NotificationInfoWidget::class, \humhub\widgets\BaseStack::EVENT_RUN, [Events::class, 'onNotificationInfoWidget']],
[AccountTopMenu::class, AccountTopMenu::EVENT_INIT, [Events::class, 'onAccountTopMenuInit']],
Expand Down
34 changes: 33 additions & 1 deletion controllers/AdminController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
namespace humhub\modules\fcmPush\controllers;

use humhub\modules\admin\components\Controller;
use humhub\modules\fcmPush\models\ConfigureForm;
use humhub\modules\admin\notifications\NewVersionAvailable;
use humhub\modules\fcmPush\models\FcmUser;
use humhub\modules\fcmPush\Module;
use humhub\modules\user\models\User;
use Yii;

/**
Expand All @@ -26,4 +28,34 @@ public function actionIndex()
return $this->render('index', ['model' => $model]);
}

/**
* @return string
*/
public function actionMobileApp()
{
if (Yii::$app->request->get('triggerNotification') == 1) {

/** @var User $user */
$user = Yii::$app->user->getIdentity();

$updateNotification = new NewVersionAvailable();
$updateNotification->sendBulk(User::find()->where(['user.id' => $user->id]));
$this->view->setStatusMessage('success', 'Notification queued!');
return $this->redirect('mobile-app');
}

if (Yii::$app->request->get('deleteToken') != "") {
$t = FcmUser::findOne(['id' => Yii::$app->request->get('deleteToken')]);
if ($t->delete() !== false) {
$this->view->setStatusMessage('success', 'Token deleted!');
return $this->redirect('mobile-app');
}

$this->view->setStatusMessage('warning', 'Token NOT deleted!');
return $this->redirect('mobile-app');
}

return $this->render('mobile-app');
}

}
41 changes: 3 additions & 38 deletions controllers/MobileAppController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,55 +2,20 @@

namespace humhub\modules\fcmPush\controllers;

use humhub\modules\admin\components\Controller;
use humhub\modules\admin\notifications\NewVersionAvailable;
use humhub\components\Controller;
use humhub\modules\fcmPush\helpers\MobileAppHelper;
use humhub\modules\fcmPush\models\FcmUser;
use humhub\modules\user\models\User;
use Yii;

class MobileAppController extends Controller
{
/**
* Renders the index view for the module
*
* @return string
*/
public function actionIndex()
{

if (Yii::$app->request->get('triggerNotification') == 1) {

/** @var User $user */
$user = Yii::$app->user->getIdentity();

$updateNotification = new NewVersionAvailable();
$updateNotification->sendBulk(User::find()->where(['user.id' => $user->id]));
$this->view->setStatusMessage('success', 'Notification queued!');
return $this->redirect('index');
}

if (Yii::$app->request->get('deleteToken') != "") {
$t = FcmUser::findOne(['id' => Yii::$app->request->get('deleteToken')]);
if ($t->delete() !== false) {
$this->view->setStatusMessage('success', 'Token deleted!');
return $this->redirect('index');
} else {
$this->view->setStatusMessage('warning', 'Token NOT deleted!');
return $this->redirect('index');
}
}

return $this->render('index');
}

public function actionInstanceOpener()
{
// Send to the mobile app to display the instance opener
MobileAppHelper::registerShowOpenerScript();
Yii::$app->session->set(MobileAppHelper::SESSION_VAR_SHOW_OPENER, 1);

// Stay in the same page, for when we come back from the mobile app to this instance
return $this->redirect(Yii::$app->request->referrer);
return $this->refresh();
}

}
28 changes: 26 additions & 2 deletions helpers/MobileAppHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,16 @@

class MobileAppHelper
{
public static function registerLoginScript()
{
public const SESSION_VAR_SHOW_OPENER = 'mobileAppShowOpener';
/**
* @deprecated Remove when minimal HumHub mobile app support is v1.0.124 and later
*/
public const SESSION_VAR_HIDE_OPENER = 'mobileAppHideOpener';
public const SESSION_VAR_REGISTER_NOTIFICATION = 'mobileAppRegisterNotification';
public const SESSION_VAR_UNREGISTER_NOTIFICATION = 'mobileAppUnregisterNotification';

public static function registerHideOpenerScript()
{
if (!static::isAppRequest()) {
return;
}
Expand Down Expand Up @@ -91,10 +98,27 @@ public static function isIosApp(): bool
&& Yii::$app->request->headers->get('x-humhub-app-is-ios');
}

/**
* True if the mobile app supports multi instance to display the Opener landing page without logout for switching instance
*
* @since HumHub mobile app v1.0.124
*/
public static function isMultiInstanceApp(): bool
{
return
static::isAppRequest()
&& Yii::$app->request->headers->get('x-humhub-app-is-multi-instance');
}

/**
* True if the mobile app Opener landing page is visible and should be hidden.
*
* @since HumHub mobile app v1.0.124
*/
public static function openerState(): bool
{
return
static::isAppRequest()
&& Yii::$app->request->headers->get('x-humhub-app-opener-state');
}
}
4 changes: 2 additions & 2 deletions views/admin/index.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php

use humhub\modules\fcmPush\models\ConfigureForm;
use humhub\modules\ui\form\widgets\ActiveForm;
use humhub\modules\ui\icon\widgets\Icon;
use humhub\widgets\Button;
use humhub\modules\ui\form\widgets\ActiveForm;
use yii\helpers\Html;

/* @var $model ConfigureForm */
Expand Down Expand Up @@ -70,6 +70,6 @@

<?php ActiveForm::end(); ?>

<?= Html::a('Mobile App Debug', ['/fcm-push/mobile-app'], ['class' => 'pull-right']); ?>
<?= Html::a('Mobile App Debug', ['/fcm-push/admin/mobile-app'], ['class' => 'pull-right']) ?>
</div>
</div>
8 changes: 4 additions & 4 deletions views/mobile-app/index.php → views/admin/mobile-app.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@

<?= Html::a('Show Opener', '#', ['class' => 'btn btn-default postFlutterMsgLink', 'data-message' => Json::encode(['type' => 'showOpener'])]); ?>
<?= Html::a('Hide Opener', '#', ['class' => 'btn btn-default postFlutterMsgLink', 'data-message' => Json::encode(['type' => 'hideOpener'])]); ?>
<?= Html::a('Open this page as POST Request', ['index'], ['data-method' => 'POST', 'class' => 'btn btn-default']); ?>
<?= Html::a('Open this page as POST Request', ['mobile-app'], ['data-method' => 'POST', 'class' => 'btn btn-default']); ?>

</div>
</div>
Expand All @@ -56,7 +56,7 @@
Administrative Notifications!</a>. It may take a few minutes.
</p>

<?= Html::a('Trigger "HumHub Update" notification', ['index', 'triggerNotification' => 1], ['class' => 'btn btn-primary pull-right']) ?>
<?= Html::a('Trigger "HumHub Update" notification', ['mobile-app', 'triggerNotification' => 1], ['class' => 'btn btn-primary pull-right']) ?>

</div>
</div>
Expand Down Expand Up @@ -107,7 +107,7 @@
<?= $fcm->sender_id ?>
&middot;

<?= Html::a('Delete', ['index', 'deleteToken' => $fcm->id, 'confirm' => 'PWA: You may need to delete token from localStorage to trigger resave!']) ?>
<?= Html::a('Delete', ['mobile-app', 'deleteToken' => $fcm->id, 'confirm' => 'PWA: You may need to delete token from localStorage to trigger resave!']) ?>
</li>
<?php endforeach; ?>
</ul>
Expand Down Expand Up @@ -215,4 +215,4 @@
}
});

</script>
</script>

0 comments on commit 8b337ce

Please sign in to comment.