From 0b5061170075634575e5dd38efdc32f91aa2211e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pau=20Ferrer=20Oca=C3=B1a?= Date: Fri, 28 Jan 2022 14:43:02 +0100 Subject: [PATCH] MOBILE-3976 messages: Use new enabled setting on 4.0 onward --- scripts/langindex.json | 3 +- .../messages/pages/settings/settings.html | 55 +++---- .../messages/pages/settings/settings.page.ts | 135 +++++++++--------- src/addons/messages/services/messages.ts | 14 +- .../notifications/services/notifications.ts | 9 +- src/core/features/settings/lang.json | 3 +- 6 files changed, 110 insertions(+), 109 deletions(-) diff --git a/scripts/langindex.json b/scripts/langindex.json index aae5bdd55..40c4b9144 100644 --- a/scripts/langindex.json +++ b/scripts/langindex.json @@ -2162,6 +2162,7 @@ "core.settings.disableall": "message", "core.settings.disabled": "lesson", "core.settings.disabledfeatures": "tool_mobile", + "core.settings.disallowed": "message", "core.settings.displayformat": "local_moodlemobileapp", "core.settings.enabledownloadsection": "local_moodlemobileapp", "core.settings.enablefirebaseanalytics": "local_moodlemobileapp", @@ -2176,6 +2177,7 @@ "core.settings.filesystemroot": "local_moodlemobileapp", "core.settings.fontsize": "local_moodlemobileapp", "core.settings.fontsizecharacter": "block_accessibility/char", + "core.settings.forced": "message", "core.settings.forcedsetting": "local_moodlemobileapp", "core.settings.forcesafeareamargins": "local_moodlemobileapp", "core.settings.general": "moodle", @@ -2186,7 +2188,6 @@ "core.settings.license": "moodle", "core.settings.localnotifavailable": "local_moodlemobileapp", "core.settings.locationhref": "local_moodlemobileapp", - "core.settings.locked": "admin", "core.settings.loggedin": "message", "core.settings.loggedoff": "message", "core.settings.navigatorlanguage": "local_moodlemobileapp", diff --git a/src/addons/messages/pages/settings/settings.html b/src/addons/messages/pages/settings/settings.html index 08bf0c664..ef1d86892 100644 --- a/src/addons/messages/pages/settings/settings.html +++ b/src/addons/messages/pages/settings/settings.html @@ -16,7 +16,7 @@ - {{ 'addon.messages.blocknoncontacts' | translate }} + {{ 'addon.messages.blocknoncontacts' | translate }} @@ -87,24 +87,29 @@ - {{ processor.displayname }} + +

{{ processor.displayname }}

+
- {{ 'core.settings.' + state | translate }} - - - - - {{'core.settings.locked' | translate }} - - - - {{ 'core.settings.disabled' | translate }} - + {{ 'core.settings.' + state | translate }} + + + + + + + + {{'core.settings.forced' | translate }} + + + {{'core.settings.disallowed' | translate }} + + + {{ 'core.settings.disabled' | translate }}
@@ -125,17 +130,17 @@ {{ processor.displayname }} - - - - + + + + + + + {{ processor.lockedmessage }} - - - - {{ 'core.settings.disabled' | translate }} - + + + {{ 'core.settings.disabled' | translate }}
diff --git a/src/addons/messages/pages/settings/settings.page.ts b/src/addons/messages/pages/settings/settings.page.ts index b986e55b3..06b4902b1 100644 --- a/src/addons/messages/pages/settings/settings.page.ts +++ b/src/addons/messages/pages/settings/settings.page.ts @@ -27,6 +27,7 @@ import { CoreSites } from '@services/sites'; import { CoreDomUtils } from '@services/utils/dom'; import { CoreConstants } from '@/core/constants'; import { IonRefresher } from '@ionic/angular'; +import { AddonNotificationsPreferencesNotificationProcessorState } from '@addons/notifications/services/notifications'; /** * Page that displays the messages settings page. @@ -50,14 +51,16 @@ export class AddonMessagesSettingsPage implements OnInit, OnDestroy { groupMessagingEnabled = false; sendOnEnter = false; + protected loggedInOffLegacyMode = false; protected previousContactableValue?: number | boolean; constructor() { - const currentSite = CoreSites.getCurrentSite(); - this.advancedContactable = !!currentSite?.isVersionGreaterEqualThan('3.6'); - this.allowSiteMessaging = !!currentSite?.canUseAdvancedFeature('messagingallusers'); + const currentSite = CoreSites.getRequiredCurrentSite(); + this.advancedContactable = !!currentSite.isVersionGreaterEqualThan('3.6'); + this.allowSiteMessaging = !!currentSite.canUseAdvancedFeature('messagingallusers'); this.groupMessagingEnabled = AddonMessages.isGroupMessagingEnabled(); + this.loggedInOffLegacyMode = !currentSite.isVersionGreaterEqualThan('4.0'); this.asyncInit(); } @@ -90,13 +93,16 @@ export class AddonMessagesSettingsPage implements OnInit, OnDestroy { component.notifications = component.notifications.filter((notification) => notification.preferencekey == AddonMessagesProvider.NOTIFICATION_PREFERENCES_KEY); - component.notifications.forEach((notification) => { - notification.processors.forEach( - (processor: AddonMessagesMessagePreferencesNotificationProcessorFormatted) => { - processor.checked = processor.loggedin.checked || processor.loggedoff.checked; - }, - ); - }); + if (this.loggedInOffLegacyMode) { + // Load enabled from loggedin / loggedoff values. + component.notifications.forEach((notification) => { + notification.processors.forEach( + (processor) => { + processor.enabled = processor.loggedin.checked || processor.loggedoff.checked; + }, + ); + }); + } } } @@ -165,74 +171,70 @@ export class AddonMessagesSettingsPage implements OnInit, OnDestroy { } /** - * Change the value of a certain preference. + * Change the value of a certain preference. Versions 3.6 onwards. * * @param notification Notification object. - * @param state State name, ['loggedin', 'loggedoff']. * @param processor Notification processor. */ async changePreference( notification: AddonMessagesMessagePreferencesNotificationFormatted, - state: string, - processor: AddonMessagesMessagePreferencesNotificationProcessorFormatted, + processor: AddonMessagesMessagePreferencesNotificationProcessor, ): Promise { + // Update both states at the same time. + let value = notification.processors + .filter((processor) => processor.enabled) + .map((processor) => processor.name) + .join(','); - const valueArray: string[] = []; - let value = 'none'; + if (value == '') { + value = 'none'; + } - if (this.groupMessagingEnabled) { - // Update both states at the same time. - const promises: Promise[] = []; - - notification.processors.forEach((processor: AddonMessagesMessagePreferencesNotificationProcessorFormatted) => { - if (processor.checked) { - valueArray.push(processor.name); - } - }); - - if (value.length > 0) { - value = valueArray.join(','); - } - - notification.updating = true; + notification.updating = true; + const promises: Promise[] = []; + if (this.loggedInOffLegacyMode) { promises.push(CoreUser.updateUserPreference(notification.preferencekey + '_loggedin', value)); promises.push(CoreUser.updateUserPreference(notification.preferencekey + '_loggedoff', value)); - - try { - await Promise.all(promises); - // Update the preferences since they were modified. - this.updatePreferencesAfterDelay(); - } catch (error) { - // Show error and revert change. - CoreDomUtils.showErrorModal(error); - processor.checked = !processor.checked; - } finally { - notification.updating = false; - } - - return; + } else { + promises.push(CoreUser.updateUserPreference(notification.preferencekey + '_enabled', value)); } + try { + await Promise.all(promises); + // Update the preferences since they were modified. + this.updatePreferencesAfterDelay(); + } catch (error) { + // Show error and revert change. + CoreDomUtils.showErrorModal(error); + processor.enabled = !processor.enabled; + } finally { + notification.updating = false; + } + } + + /** + * Change the value of a certain preference. Only on version 3.5. + * + * @param notification Notification object. + * @param processor Notification processor. + * @param state State name, ['loggedin', 'loggedoff']. + */ + async changePreferenceLegacy( + notification: AddonMessagesMessagePreferencesNotificationFormatted, + processor: AddonMessagesMessagePreferencesNotificationProcessor, + state: 'loggedin' | 'loggedoff', + ): Promise { // Update only the specified state. - const processorState = processor[state]; + const processorState: AddonNotificationsPreferencesNotificationProcessorState = processor[state]; const preferenceName = notification.preferencekey + '_' + processorState.name; - notification.processors.forEach((processor) => { - if (processor[state].checked) { - valueArray.push(processor.name); - } - }); + const value = notification.processors + .filter((processor) => processor[state].checked) + .map((processor) => processor.name) + .join(','); - if (value.length > 0) { - value = valueArray.join(','); - } - - if (!notification.updating) { - notification.updating = {}; - } - - notification.updating[state] = true; + notification['updating'+state] = true; try { await CoreUser.updateUserPreference(preferenceName, value); // Update the preferences since they were modified. @@ -242,7 +244,7 @@ export class AddonMessagesSettingsPage implements OnInit, OnDestroy { CoreDomUtils.showErrorModal(error); processorState.checked = !processorState.checked; } finally { - notification.updating[state] = false; + notification['updating'+state] = false; } } @@ -288,12 +290,7 @@ export class AddonMessagesSettingsPage implements OnInit, OnDestroy { * Message preferences notification with some caclulated data. */ type AddonMessagesMessagePreferencesNotificationFormatted = AddonMessagesMessagePreferencesNotification & { - updating?: boolean | {[state: string]: boolean}; // Calculated in the app. Whether the notification is being updated. -}; - -/** - * Message preferences notification processor with some caclulated data. - */ -type AddonMessagesMessagePreferencesNotificationProcessorFormatted = AddonMessagesMessagePreferencesNotificationProcessor & { - checked?: boolean; // Calculated in the app. Whether the processor is checked either for loggedin or loggedoff. + updating?: boolean; // Calculated in the app. Whether the notification is being updated. + updatingloggedin?: boolean; // Calculated in the app. Whether the notification is being updated. + updatingloggedoff?: boolean; // Calculated in the app. Whether the notification is being updated. }; diff --git a/src/addons/messages/services/messages.ts b/src/addons/messages/services/messages.ts index ba07393c5..c53ddfac3 100644 --- a/src/addons/messages/services/messages.ts +++ b/src/addons/messages/services/messages.ts @@ -32,6 +32,7 @@ import { makeSingleton } from '@singletons'; import { CoreError } from '@classes/errors/error'; import { AddonMessagesSyncEvents, AddonMessagesSyncProvider } from './messages-sync'; import { CoreWSError } from '@classes/errors/wserror'; +import { AddonNotificationsPreferencesNotificationProcessorState } from '@addons/notifications/services/notifications'; const ROOT_CACHE_KEY = 'mmaMessages:'; @@ -3047,16 +3048,9 @@ export type AddonMessagesMessagePreferencesNotificationProcessor = { locked: boolean; // Is locked by admin?. lockedmessage?: string; // @since 3.6. Text to display if locked. userconfigured: number; // Is configured?. - loggedin: { - name: string; // Name. - displayname: string; // Display name. - checked: boolean; // Is checked?. - }; - loggedoff: { - name: string; // Name. - displayname: string; // Display name. - checked: boolean; // Is checked?. - }; + enabled?: boolean; // @since 4.0. Processor enabled. + loggedin: AddonNotificationsPreferencesNotificationProcessorState; // @deprecated removed on 4.0. + loggedoff: AddonNotificationsPreferencesNotificationProcessorState; // @deprecated removed on 4.0. }; /** diff --git a/src/addons/notifications/services/notifications.ts b/src/addons/notifications/services/notifications.ts index 86b5f5f49..28c60c1ed 100644 --- a/src/addons/notifications/services/notifications.ts +++ b/src/addons/notifications/services/notifications.ts @@ -435,15 +435,18 @@ export type AddonNotificationsPreferencesNotificationProcessor = { locked: boolean; // Is locked by admin?. lockedmessage?: string; // @since 3.6. Text to display if locked. userconfigured: number; // Is configured?. - loggedin: AddonNotificationsPreferencesNotificationProcessorState; - loggedoff: AddonNotificationsPreferencesNotificationProcessorState; + enabled?: boolean; // @since 4.0. Processor enabled. + loggedin: AddonNotificationsPreferencesNotificationProcessorState; // @deprecated removed on 4.0. + loggedoff: AddonNotificationsPreferencesNotificationProcessorState; // @deprecated removed on 4.0. }; /** * State in notification processor in notification preferences component. + * + * @deprecated removed on 4.0. */ export type AddonNotificationsPreferencesNotificationProcessorState = { - name: string; // Name. + name: 'loggedoff' | 'loggedin'; // Name. displayname: string; // Display name. checked: boolean; // Is checked?. }; diff --git a/src/core/features/settings/lang.json b/src/core/features/settings/lang.json index edaa16204..a7fe5e31f 100644 --- a/src/core/features/settings/lang.json +++ b/src/core/features/settings/lang.json @@ -29,6 +29,7 @@ "disableall": "Disable notifications", "disabled": "Disabled", "disabledfeatures": "Disabled features", + "disallowed": "Disallowed", "displayformat": "Display format", "enabledownloadsection": "Enable download sections", "enablefirebaseanalytics": "Enable Firebase analytics", @@ -43,6 +44,7 @@ "filesystemroot": "File system root", "fontsize": "Text size", "fontsizecharacter": "A", + "forced": "Locked", "forcedsetting": "This setting has been forced by your site configuration.", "forcesafeareamargins": "Force safe area margins", "general": "General", @@ -53,7 +55,6 @@ "license": "Licence", "localnotifavailable": "Local notifications available", "locationhref": "Web view URL", - "locked": "Locked", "loggedin": "Online", "loggedoff": "Offline", "navigatorlanguage": "Navigator language",