Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use the new DeviceDetectorHelper helper instead of MobileAppHelper & … #67

Merged
merged 2 commits into from
Jan 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions Events.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace humhub\modules\fcmPush;

use humhub\components\mail\Message;
use humhub\helpers\DeviceDetectorHelper;
use humhub\modules\fcmPush\assets\FcmPushAsset;
use humhub\modules\fcmPush\assets\FirebaseAsset;
use humhub\modules\fcmPush\components\MailerMessage;
Expand Down Expand Up @@ -92,7 +93,7 @@ public static function onLayoutAddonInit($event)
if (Yii::$app->session->has(MobileAppHelper::SESSION_VAR_HIDE_OPENER)) {
MobileAppHelper::registerHideOpenerScript();
Yii::$app->session->remove(MobileAppHelper::SESSION_VAR_HIDE_OPENER);
} elseif (MobileAppHelper::openerState()) {
} elseif (DeviceDetectorHelper::appOpenerState()) {
MobileAppHelper::registerHideOpenerScript();
}

Expand Down Expand Up @@ -156,14 +157,14 @@ public static function onAuthChoiceBeforeRun(Event $event)
/** @var Module $module */
$module = Yii::$app->getModule('fcm-push');

if (MobileAppHelper::isIosApp() && $module->getConfigureForm()->disableAuthChoicesIos) {
if (DeviceDetectorHelper::isIosApp() && $module->getConfigureForm()->disableAuthChoicesIos) {
$sender->setClients([]);
}
}

public static function onAccountTopMenuInit(Event $event)
{
if (!MobileAppHelper::isMultiInstanceApp()) {
if (!DeviceDetectorHelper::isMultiInstanceApp()) {
return;
}

Expand Down
4 changes: 3 additions & 1 deletion docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ Changelog

2.1.4 (Unreleased)
-----------------------
- Enh: Add "Open native console" button for the mobile app
- Enh #66: Add "Open native console" button for the mobile app
- Chg #67: The minimal HumHub version is now 1.17
- Chg #67: Use the new `DeviceDetectorHelper` helper instead of `MobileAppHelper`

2.1.3 (January 6, 2024)
-----------------------
Expand Down
74 changes: 10 additions & 64 deletions helpers/MobileAppHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace humhub\modules\fcmPush\helpers;

use humhub\helpers\DeviceDetectorHelper;
use Yii;
use yii\helpers\Json;
use yii\helpers\Url;
Expand All @@ -16,9 +17,9 @@ class MobileAppHelper
public const SESSION_VAR_REGISTER_NOTIFICATION = 'mobileAppRegisterNotification';
public const SESSION_VAR_UNREGISTER_NOTIFICATION = 'mobileAppUnregisterNotification';

public static function registerHideOpenerScript()
public static function registerHideOpenerScript(): void
{
if (!static::isAppRequest()) {
if (!DeviceDetectorHelper::isAppRequest()) {
return;
}

Expand All @@ -28,9 +29,9 @@ public static function registerHideOpenerScript()
self::sendFlutterMessage($message);
}

public static function registerShowOpenerScript()
public static function registerShowOpenerScript(): void
{
if (!static::isAppRequest()) {
if (!DeviceDetectorHelper::isAppRequest()) {
return;
}

Expand All @@ -40,9 +41,9 @@ public static function registerShowOpenerScript()
self::sendFlutterMessage($message);
}

public static function registerNotificationScript()
public static function registerNotificationScript(): void
{
if (!static::isAppRequest()) {
if (!DeviceDetectorHelper::isAppRequest()) {
return;
}

Expand All @@ -51,9 +52,9 @@ public static function registerNotificationScript()
self::sendFlutterMessage($message);
}

public static function unregisterNotificationScript()
public static function unregisterNotificationScript(): void
{
if (!static::isAppRequest()) {
if (!DeviceDetectorHelper::isAppRequest()) {
return;
}

Expand All @@ -62,63 +63,8 @@ public static function unregisterNotificationScript()
self::sendFlutterMessage($message);
}

public static function isAppRequest()
{
return (
(Yii::$app->request->headers->get('x-requested-with', null, true) === 'com.humhub.app') ||
(Yii::$app->request->headers->has('x-humhub-app'))
);
}

/**
* Determines whether the app is a branded app with custom firebase configuration.
* @return bool
*/
public static function isAppWithCustomFcm(): bool
{
return (
Yii::$app->request->headers->has('x-humhub-app-bundle-id') &&
!str_contains(
Yii::$app->request->headers->get('x-humhub-app-bundle-id', '', true),
'com.humhub.app',
)
);

}

private static function sendFlutterMessage($msg)
private static function sendFlutterMessage($msg): void
{
Yii::$app->view->registerJs('if (window.flutterChannel) { window.flutterChannel.postMessage(\'' . $msg . '\'); }');
}

public static function isIosApp(): bool
{
return
static::isAppRequest()
&& 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');
}
}
3 changes: 2 additions & 1 deletion helpers/WebAppHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace humhub\modules\fcmPush\helpers;

use humhub\helpers\DeviceDetectorHelper;
use Yii;

class WebAppHelper
Expand All @@ -10,7 +11,7 @@ class WebAppHelper

public static function unregisterNotificationScript()
{
if (MobileAppHelper::isAppRequest()) {
if (DeviceDetectorHelper::isAppRequest()) {
return;
}

Expand Down
2 changes: 1 addition & 1 deletion module.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
],
"homepage": "https://github.com/humhub/fcm-push",
"humhub": {
"minVersion": "1.14"
"minVersion": "1.17"
},
"version": "2.1.4",
"screenshots": [
Expand Down
4 changes: 2 additions & 2 deletions services/DriverService.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

namespace humhub\modules\fcmPush\services;

use humhub\helpers\DeviceDetectorHelper;
use humhub\modules\fcmPush\driver\DriverInterface;
use humhub\modules\fcmPush\driver\Fcm;
use humhub\modules\fcmPush\driver\Proxy;
use humhub\modules\fcmPush\helpers\MobileAppHelper;
use humhub\modules\fcmPush\models\ConfigureForm;

class DriverService
Expand Down Expand Up @@ -66,7 +66,7 @@ public function getWebDriver(): ?DriverInterface

public function getMobileAppDriver(): ?DriverInterface
{
if (MobileAppHelper::isAppWithCustomFcm()) {
if (DeviceDetectorHelper::isAppWithCustomFcm()) {
return $this->getConfiguredDriverByType(Fcm::class);
}

Expand Down
6 changes: 3 additions & 3 deletions views/admin/mobile-app.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

/* @var $this \humhub\modules\ui\view\components\View */

use humhub\helpers\DeviceDetectorHelper;
use humhub\libs\Html;
use humhub\modules\fcmPush\helpers\MobileAppHelper;
use humhub\modules\fcmPush\models\FcmUser;
use yii\helpers\Json;
use yii\helpers\Url;
Expand All @@ -20,12 +20,12 @@
</div>
<div class="panel-body">

<?php if (MobileAppHelper::isAppRequest()): ?>
<?php if (DeviceDetectorHelper::isAppRequest()): ?>
<p class="alert alert-success">
<strong>App Detection</strong> - Current Request: Is App Request
</p>

<?php if (MobileAppHelper::isAppWithCustomFcm()): ?>
<?php if (DeviceDetectorHelper::isAppWithCustomFcm()): ?>
<p class="alert alert-success">
<strong>FCM Detection</strong> - App is using custom Firebase
</p>
Expand Down
Loading