From 04df865aa361c0b14f074ad42fdfedd779c970a0 Mon Sep 17 00:00:00 2001 From: Galin Iliev Date: Wed, 12 Jul 2023 14:01:48 +0300 Subject: [PATCH] G4E-6295: Update Enable notifications/toasts behaviour in app manager. Now the checkboxes listen for outer source changes. --- js/glue-related.js | 61 +++++++++++++++++++++++++++++++++++++++++----- js/index.js | 13 +++------- js/settings.js | 18 +------------- js/utils.js | 7 ------ 4 files changed, 59 insertions(+), 40 deletions(-) diff --git a/js/glue-related.js b/js/glue-related.js index 192c099..9c180f9 100644 --- a/js/glue-related.js +++ b/js/glue-related.js @@ -1,4 +1,9 @@ -import { setSettings, getSetting, getSettings } from './settings.js'; +import { + setSettings, + getSetting, + getSettings, + setSetting, +} from './settings.js'; import { setOrientation, setWindowSize, @@ -287,23 +292,23 @@ async function clearDefaultLayout() { async function trackNotificationsEnabled() { await gluePromise; - let notificationMethoExists = new rxjs.BehaviorSubject(false); - notificationMethoExists.next( + let notificationMethodExists = new rxjs.BehaviorSubject(false); + notificationMethodExists.next( glue.agm.methods({ name: 'T42.Notifications.Show' }).length > 0 ); glue.agm.methodAdded(() => { - notificationMethoExists.next( + notificationMethodExists.next( glue.agm.methods({ name: 'T42.Notifications.Show' }).length > 0 ); }); glue.agm.methodRemoved(() => { - notificationMethoExists.next( + notificationMethodExists.next( glue.agm.methods({ name: 'T42.Notifications.Show' }).length > 0 ); }); - notificationMethoExists + notificationMethodExists .pipe(rxjs.operators.distinctUntilChanged()) .subscribe((data) => notificationEnabledObs.next(data)); } @@ -322,6 +327,48 @@ async function configureNotifications(config) { } } +async function getNotificationsConfiguration() { + const glue = await gluePromise; + const { enable, enableToasts } = await glue.notifications.getConfiguration(); + const setting = { + enableNotifications: enable, + enableToasts, + }; + + setSetting(setting); + updatePrefs(setting); +} + +async function trackNotificationsConfigurationChange() { + const glue = await gluePromise; + + await glue.notifications.onConfigurationChanged((config) => { + const { enable, enableToasts } = config; + const setting = { + enableNotifications: enable, + enableToasts, + }; + + setSetting(setting); + updatePrefs(setting); + + const notificationPanel = q('#notification-panel'); + const enableNotificationsCheckbox = q('#enable-notifications'); + const enableToastsCheckbox = q('#enable-toasts'); + + if (enable) { + notificationPanel.classList.remove('d-none'); + enableNotificationsCheckbox.checked = true; + enableToastsCheckbox.disabled = false; + } else { + notificationPanel.classList.add('d-none'); + enableNotificationsCheckbox.checked = false; + enableToastsCheckbox.checked = false; + enableToastsCheckbox.disabled = true; + } + }); +} + async function openNotificationPanel() { await gluePromise; glue.agm.invoke('T42.Notifications.Show'); @@ -597,4 +644,6 @@ export { getPrimaryScaleFactor, getScaleFactor, windowCenter, + getNotificationsConfiguration, + trackNotificationsConfigurationChange, }; diff --git a/js/index.js b/js/index.js index 6ab7a08..66eef0a 100644 --- a/js/index.js +++ b/js/index.js @@ -45,6 +45,9 @@ document.addEventListener('DOMContentLoaded', () => { async function init() { await glueModule.getPrefs(); + await glueModule.getNotificationsConfiguration(); + await glueModule.trackNotificationsConfigurationChange(); + finishLoading(); observeAppElement(); @@ -56,7 +59,6 @@ async function init() { printLayouts(); printFavoriteApps(); printNotificationCount(); - printNotificationButton(); printInitialToastState(); handleAppClick(); @@ -328,15 +330,6 @@ function printNotificationCount() { }); } -function printNotificationButton() { - const notificationsEnabled = getSetting('enableNotifications'); - const notificationButton = q('#notification-panel'); - - if (!notificationsEnabled) { - notificationButton.classList.add('d-none'); - } -} - function printInitialToastState() { const notificationsEnabled = getSetting('enableNotifications'); const enableToasts = q('#enable-toasts'); diff --git a/js/settings.js b/js/settings.js index 518b0ed..ce274d2 100644 --- a/js/settings.js +++ b/js/settings.js @@ -1,8 +1,4 @@ -import { - updatePrefs, - checkNotificationsConfigure, - configureNotifications, -} from './glue-related.js'; +import { updatePrefs } from './glue-related.js'; let settings = { showTutorial: true, @@ -31,17 +27,6 @@ async function init() { } async function populateSettings() { - const methodExists = await checkNotificationsConfigure(); - - if (methodExists) { - configureNotifications({ - enable: settings.enableNotifications, - enableToasts: settings.enableToasts, - }); - } else { - q('.settings-notifications').classList.add('d-none'); - } - for (const setting in settings) { if ( typeof settings[setting] === 'boolean' && @@ -84,7 +69,6 @@ function getSettings() { function setSettings(prefs) { settings = { ...settings, ...prefs }; - populateSettings(); init(); } diff --git a/js/utils.js b/js/utils.js index ec144dc..6b9474d 100644 --- a/js/utils.js +++ b/js/utils.js @@ -406,20 +406,13 @@ async function handleEnableNotifications() { } function handleEnableNotificationsClick() { - const notificationPanel = q('#notification-panel'); const enableNotifications = q('#enable-notifications'); - const enableToasts = q('#enable-toasts'); enableNotifications.addEventListener('click', (e) => { if (e.target.checked) { configureNotifications({ enable: true, enableToasts: false }); - notificationPanel.classList.remove('d-none'); - enableToasts.disabled = false; } else { configureNotifications({ enable: false, enableToasts: false }); - notificationPanel.classList.add('d-none'); - enableToasts.checked = false; - enableToasts.disabled = true; } }); }