From 93251a55947aaa2a31bc4820cc1efccca9536ad7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pau=20Ferrer=20Oca=C3=B1a?= Date: Fri, 28 Jan 2022 17:41:10 +0100 Subject: [PATCH] MOBILE-3976 notifications: Use new enabled setting on 4.0 onward --- .../pages/settings/settings.html | 197 ++++++++++++------ .../pages/settings/settings.scss | 6 +- .../notifications/pages/settings/settings.ts | 82 ++++++-- .../services/notifications-helper.ts | 34 +-- .../notifications/services/notifications.ts | 2 + 5 files changed, 202 insertions(+), 119 deletions(-) diff --git a/src/addons/notifications/pages/settings/settings.html b/src/addons/notifications/pages/settings/settings.html index cafdb246d..6bd5e89d8 100644 --- a/src/addons/notifications/pages/settings/settings.html +++ b/src/addons/notifications/pages/settings/settings.html @@ -35,75 +35,144 @@ - + {{ processor.displayname }} - - - - - {{ component.displayname }} - - {{ 'core.settings.loggedin' | translate }} - - - {{ 'core.settings.loggedoff' | translate }} - - - - - - - - - {{ notification.displayname }} - - - - - - - - {{'core.settings.locked' | translate }} - - - {{ 'core.settings.disabled' | translate }} - - - + + + - - - {{ notification.displayname }} - - - - {{ 'core.settings.' + state | translate }} - - - - - - {{'core.settings.locked' | translate }} - - {{ 'core.settings.disabled' | translate }} - - - + + + + + + + + + + + + + +

{{ component.displayname }}

+
+ +

{{ 'core.settings.loggedin' | translate }}

+
+ +

{{ 'core.settings.loggedoff' | translate }}

+
+
+
+
+
+ + + + + + +

{{ notification.displayname }}

+
+ + + + + + + + + {{'core.settings.forced' | translate }} + + + {{'core.settings.disallowed' | translate }} + + + + {{ 'core.settings.disabled' | translate }} + +
+
+
+ + + +

{{ notification.displayname }}

+
+ + + +

{{ 'core.settings.' + state | translate }}

+
+
+ + + + + + {{'core.settings.forced' | translate }} + + + {{'core.settings.disallowed' | translate }} + +
+ {{ 'core.settings.disabled' | translate }} +
+
+
+
+ + + + + + +

{{ component.displayname }}

+
+
+ + + + +

{{ notification.displayname }}

+
+ +
+ + + + + + {{'core.settings.forced' | translate }} + + + {{'core.settings.disallowed' | translate }} + +
+ {{ 'core.settings.disabled' | translate }} +
+
+
+ +
diff --git a/src/addons/notifications/pages/settings/settings.scss b/src/addons/notifications/pages/settings/settings.scss index 8fea99c17..d224357cb 100644 --- a/src/addons/notifications/pages/settings/settings.scss +++ b/src/addons/notifications/pages/settings/settings.scss @@ -1,5 +1,3 @@ -:host { - .addon-notifications-table-content ion-row { - min-height: 35px; - } +.addon-notifications-table-content ion-row { + min-height: 35px; } diff --git a/src/addons/notifications/pages/settings/settings.ts b/src/addons/notifications/pages/settings/settings.ts index d8cc886d3..a9fbe1210 100644 --- a/src/addons/notifications/pages/settings/settings.ts +++ b/src/addons/notifications/pages/settings/settings.ts @@ -27,7 +27,6 @@ import { CoreError } from '@classes/errors/error'; import { CoreEvents } from '@singletons/events'; import { AddonNotifications, - AddonNotificationsPreferencesProcessor, AddonNotificationsPreferencesNotificationProcessorState, } from '../../services/notifications'; import { @@ -51,16 +50,20 @@ export class AddonNotificationsSettingsPage implements OnInit, OnDestroy { preferences?: AddonNotificationsPreferencesFormatted; components?: AddonNotificationsPreferencesComponentFormatted[]; - currentProcessor?: AddonNotificationsPreferencesProcessor; + currentProcessorName = 'airnotifier'; preferencesLoaded = false; notificationSound = false; canChangeSound: boolean; processorHandlers: AddonMessageOutputHandlerData[] = []; + loggedInOffLegacyMode = false; protected updateTimeout?: number; constructor() { this.canChangeSound = CoreLocalNotifications.canDisableSound(); + + const currentSite = CoreSites.getRequiredCurrentSite(); + this.loggedInOffLegacyMode = !currentSite.isVersionGreaterEqualThan('4.0'); } /** @@ -83,20 +86,20 @@ export class AddonNotificationsSettingsPage implements OnInit, OnDestroy { try { const preferences = await AddonNotifications.getNotificationPreferences(); - if (!this.currentProcessor) { - // Initialize current processor. Load "Mobile" (airnotifier) if available. - this.currentProcessor = AddonNotificationsHelper.getProcessor(preferences.processors, 'airnotifier'); + // Initialize current processor. Load "Mobile" (airnotifier) if available. + let currentProcessor = preferences.processors.find((processor) => processor.name == this.currentProcessorName); + if (!currentProcessor) { + currentProcessor = preferences.processors[0]; } - if (!this.currentProcessor) { + if (!currentProcessor) { // Shouldn't happen. throw new CoreError('No processor found'); } preferences.enableall = !preferences.disableall; this.preferences = AddonNotificationsHelper.formatPreferences(preferences); - this.loadProcessor(this.currentProcessor); - + this.loadProcessor(currentProcessor); } catch (error) { CoreDomUtils.showErrorModal(error); } finally { @@ -114,7 +117,7 @@ export class AddonNotificationsSettingsPage implements OnInit, OnDestroy { return; } - this.currentProcessor = processor; + this.currentProcessorName = processor.name; this.processorHandlers = []; this.components = AddonNotificationsHelper.getProcessorComponents( processor.name, @@ -199,27 +202,21 @@ export class AddonNotificationsSettingsPage implements OnInit, OnDestroy { * @param state State name, ['loggedin', 'loggedoff']. * @return Promise resolved when done. */ - async changePreference(notification: AddonNotificationsPreferencesNotificationFormatted, state: string): Promise { - const processor = notification.processorsByName?.[this.currentProcessor?.name || '']; + async changePreferenceLegacy(notification: AddonNotificationsPreferencesNotificationFormatted, state: string): Promise { + const processor = notification.processorsByName?.[this.currentProcessorName]; if (!processor) { return; } const processorState: ProcessorStateFormatted = processor[state]; const preferenceName = notification.preferencekey + '_' + processorState.name; - let value: string | undefined; - notification.processors.forEach((processor) => { - if (processor[state].checked) { - if (!value) { - value = processor.name; - } else { - value += ',' + processor.name; - } - } - }); + let value = notification.processors + .filter((processor) => processor[state].checked) + .map((processor) => processor.name) + .join(','); - if (!value) { + if (value == '') { value = 'none'; } @@ -239,6 +236,45 @@ export class AddonNotificationsSettingsPage implements OnInit, OnDestroy { } } + /** + * Change the value of a certain preference. + * + * @param notification Notification object. + * @return Promise resolved when done. + */ + async changePreference(notification: AddonNotificationsPreferencesNotificationFormatted): Promise { + const processor = notification.processorsByName?.[this.currentProcessorName]; + if (!processor) { + return; + } + + const preferenceName = notification.preferencekey + '_enabled'; + + let value = notification.processors + .filter((processor) => processor.enabled) + .map((processor) => processor.name) + .join(','); + + if (value == '') { + value = 'none'; + } + + processor.updating = true; + + try { + await CoreUser.updateUserPreference(preferenceName, value); + + // Update the preferences since they were modified. + this.updatePreferencesAfterDelay(); + } catch (error) { + // Show error and revert change. + CoreDomUtils.showErrorModal(error); + processor.enabled = !processor.enabled; + } finally { + processor.updating = false; + } + } + /** * Enable all notifications changed. * @@ -294,6 +330,8 @@ export class AddonNotificationsSettingsPage implements OnInit, OnDestroy { /** * State in notification processor in notification preferences component with some calculated data. + * + * @deprecated 4.0 */ type ProcessorStateFormatted = AddonNotificationsPreferencesNotificationProcessorState & { updating?: boolean; // Calculated in the app. Whether the state is being updated. diff --git a/src/addons/notifications/services/notifications-helper.ts b/src/addons/notifications/services/notifications-helper.ts index 8ccd98f55..8eb24bfdc 100644 --- a/src/addons/notifications/services/notifications-helper.ts +++ b/src/addons/notifications/services/notifications-helper.ts @@ -77,34 +77,6 @@ export class AddonNotificationsHelperProvider { return formattedPreferences; } - /** - * Get a certain processor from a list of processors. - * - * @param processors List of processors. - * @param name Name of the processor to get. - * @param fallback True to return first processor if not found, false to not return any. Defaults to true. - * @return Processor. - */ - getProcessor( - processors: AddonNotificationsPreferencesProcessor[], - name: string, - fallback: boolean = true, - ): AddonNotificationsPreferencesProcessor | undefined { - if (!processors || !processors.length) { - return; - } - - const processor = processors.find((processor) => processor.name == name); - if (processor) { - return processor; - } - - // Processor not found, return first if requested. - if (fallback) { - return processors[0]; - } - } - /** * Return the components and notifications that have a certain processor. * @@ -166,7 +138,11 @@ export type AddonNotificationsPreferencesComponentFormatted = Omit; // Calculated in the app. + processorsByName?: Record; // Calculated in the app. +}; + +type AddonNotificationsPreferencesNotificationProcessorFormatted = AddonNotificationsPreferencesNotificationProcessor & { + updating?: boolean; // Calculated in the app. Whether the state is being updated. }; /** diff --git a/src/addons/notifications/services/notifications.ts b/src/addons/notifications/services/notifications.ts index 28c60c1ed..1fab101db 100644 --- a/src/addons/notifications/services/notifications.ts +++ b/src/addons/notifications/services/notifications.ts @@ -451,6 +451,8 @@ export type AddonNotificationsPreferencesNotificationProcessorState = { checked: boolean; // Is checked?. }; +export type AddonNotificationsPreferencesNotificationProcessorStateSetting = 'loggedoff' | 'loggedin' | 'enabled'; + /** * Params of core_message_get_messages WS. */