Skip to content

Commit

Permalink
Merge pull request #39 from Glue42/G4E-6295-update-enable-notificatio…
Browse files Browse the repository at this point in the history
…ns-behavior

G4E-6295: Update Enable notifications/toasts  behaviour in app manage…
  • Loading branch information
gdavidkov authored Aug 3, 2023
2 parents 3b7ade5 + 7c43a5f commit 277db73
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 40 deletions.
61 changes: 55 additions & 6 deletions js/glue-related.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { setSettings, getSetting, getSettings } from './settings.js';
import {
setSettings,
getSetting,
getSettings,
setSetting,
} from './settings.js';
import {
setOrientation,
setWindowSize,
Expand Down Expand Up @@ -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));
}
Expand All @@ -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');
Expand Down Expand Up @@ -597,4 +644,6 @@ export {
getPrimaryScaleFactor,
getScaleFactor,
windowCenter,
getNotificationsConfiguration,
trackNotificationsConfigurationChange,
};
13 changes: 3 additions & 10 deletions js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ document.addEventListener('DOMContentLoaded', () => {

async function init() {
await glueModule.getPrefs();
await glueModule.getNotificationsConfiguration();
await glueModule.trackNotificationsConfigurationChange();

finishLoading();

observeAppElement();
Expand All @@ -56,7 +59,6 @@ async function init() {
printLayouts();
printFavoriteApps();
printNotificationCount();
printNotificationButton();
printInitialToastState();

handleAppClick();
Expand Down Expand Up @@ -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');
Expand Down
18 changes: 1 addition & 17 deletions js/settings.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
import {
updatePrefs,
checkNotificationsConfigure,
configureNotifications,
} from './glue-related.js';
import { updatePrefs } from './glue-related.js';

let settings = {
showTutorial: true,
Expand Down Expand Up @@ -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' &&
Expand Down Expand Up @@ -84,7 +69,6 @@ function getSettings() {

function setSettings(prefs) {
settings = { ...settings, ...prefs };
populateSettings();
init();
}

Expand Down
7 changes: 0 additions & 7 deletions js/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
});
}
Expand Down

0 comments on commit 277db73

Please sign in to comment.