MOBILE-3976 messages: Use new enabled setting on 4.0 onward

main
Pau Ferrer Ocaña 2022-01-28 14:43:02 +01:00
parent 1f6c5a672b
commit 0b50611700
6 changed files with 110 additions and 109 deletions

View File

@ -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",

View File

@ -16,7 +16,7 @@
<!-- Contactable privacy. -->
<ion-card>
<ion-item *ngIf="!advancedContactable">
<ion-label>{{ 'addon.messages.blocknoncontacts' | translate }}</ion-label>
<ion-label>{{ 'addon.messages.blocknoncontacts' | translate }}</ion-label>
<ion-toggle [(ngModel)]="contactablePrivacy" (ngModelChange)="saveContactablePrivacy(contactablePrivacy)">
</ion-toggle>
</ion-item>
@ -87,24 +87,29 @@
<ion-card-content class="ion-no-padding">
<ng-container *ngFor="let processor of notification.processors">
<ion-item-divider class="ion-text-wrap">
<ion-label>{{ processor.displayname }}</ion-label>
<ion-label>
<p class="item-heading">{{ processor.displayname }}</p>
</ion-label>
</ion-item-divider>
<!-- If notifications not disabled, show toggles.
If notifications are disabled, show "Disabled" instead of toggle. -->
<ion-item *ngFor="let state of ['loggedin', 'loggedoff']" class="ion-text-wrap">
<ion-label>{{ 'core.settings.' + state | translate }}</ion-label>
<core-button-with-spinner slot="end" *ngIf="!preferences.disableall"
[loading]="notification.updating && notification.updating[state]">
<ion-toggle *ngIf="!processor.locked" [(ngModel)]="processor[state].checked"
(ngModelChange)="changePreference(notification, state, processor)">
</ion-toggle>
<ion-note *ngIf="processor.locked">
{{'core.settings.locked' | translate }}
</ion-note>
</core-button-with-spinner>
<ion-note slot="end" *ngIf="preferences.disableall">
{{ 'core.settings.disabled' | translate }}
</ion-note>
<ion-label>{{ 'core.settings.' + state | translate }}</ion-label>
<ng-container *ngIf="!preferences.disableall">
<!-- If notifications enabled, show toggle. -->
<core-button-with-spinner *ngIf="!processor.locked" [loading]="notification['updating'+state]">
<ion-toggle [(ngModel)]="processor[state].checked"
(ngModelChange)="changePreferenceLegacy(notification, processor, state)">
</ion-toggle>
</core-button-with-spinner>
<span *ngIf="processor.locked && processor[state].checked" class="text-gray">
{{'core.settings.forced' | translate }}
</span>
<span *ngIf="processor.locked && !processor[state].checked" class="text-gray">
{{'core.settings.disallowed' | translate }}
</span>
</ng-container>
<span *ngIf="preferences.disableall">{{ 'core.settings.disabled' | translate }}</span>
</ion-item>
</ng-container>
</ion-card-content>
@ -125,17 +130,17 @@
<!-- If group messaging is enabled, display a simplified view. -->
<ion-item class="ion-text-wrap">
<ion-label>{{ processor.displayname }}</ion-label>
<core-button-with-spinner *ngIf="!preferences.disableall" [loading]="!!notification.updating" slot="end">
<ion-toggle *ngIf="!processor.locked" [(ngModel)]="processor.checked"
(ngModelChange)="changePreference(notification, '', processor)">
</ion-toggle>
<ion-note *ngIf="processor.locked">
<ng-container *ngIf="!preferences.disableall">
<!-- If notifications enabled, show toggle. -->
<core-button-with-spinner *ngIf="!processor.locked" [loading]="notification.updating">
<ion-toggle [(ngModel)]="processor.enabled" (ngModelChange)="changePreference(notification, processor)">
</ion-toggle>
</core-button-with-spinner>
<span class="text-gray" *ngIf="processor.locked">
{{ processor.lockedmessage }}
</ion-note>
</core-button-with-spinner>
<ion-note slot="end" *ngIf="preferences.disableall">
{{ 'core.settings.disabled' | translate }}
</ion-note>
</span>
</ng-container>
<span *ngIf="preferences.disableall" class="text-gray">{{ 'core.settings.disabled' | translate }}</span>
</ion-item>
</ng-container>
</ion-card>

View File

@ -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<void> {
// 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<void>[] = [];
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<void>[] = [];
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<void> {
// 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.
};

View File

@ -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.
};
/**

View File

@ -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?.
};

View File

@ -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",