Merge pull request #1636 from dpalou/MOBILE-2758

MOBILE-2758 message: Simplify message preferences
main
Juan Leyva 2018-11-28 13:36:50 +01:00 committed by GitHub
commit 62a37720da
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 125 additions and 52 deletions

View File

@ -37,12 +37,30 @@
<ion-card list *ngFor="let notification of component.notifications"> <ion-card list *ngFor="let notification of component.notifications">
<ion-item-divider color="light" text-wrap> <ion-item-divider color="light" text-wrap>
<ion-row no-padding> <ion-row no-padding>
<ng-container *ngIf="!groupMessagingEnabled">
<ion-col no-padding>{{ notification.displayname }}</ion-col> <ion-col no-padding>{{ notification.displayname }}</ion-col>
<ion-col col-2 text-center no-padding class="hidden-phone">{{ 'core.settings.loggedin' | translate }}</ion-col> <ion-col col-2 text-center no-padding class="hidden-phone">{{ 'core.settings.loggedin' | translate }}</ion-col>
<ion-col col-2 text-center no-padding class="hidden-phone">{{ 'core.settings.loggedoff' | translate }}</ion-col> <ion-col *ngIf="!groupMessagingEnabled" col-2 text-center no-padding class="hidden-phone">{{ 'core.settings.loggedoff' | translate }}</ion-col>
</ng-container>
<ng-container *ngIf="groupMessagingEnabled">
<ion-col no-padding>{{ 'addon.notifications.notificationpreferences' | translate }}</ion-col>
</ng-container>
</ion-row> </ion-row>
</ion-item-divider> </ion-item-divider>
<ng-container *ngFor="let processor of notification.processors"> <ng-container *ngFor="let processor of notification.processors">
<!-- If group messaging is enabled, display a simplified view. -->
<ng-container *ngIf="groupMessagingEnabled">
<ion-item text-wrap>
<ion-label>{{ processor.displayname }}</ion-label>
<ion-spinner item-end *ngIf="!preferences.disableall && notification.updating"></ion-spinner>
<ion-toggle item-end *ngIf="!preferences.disableall && !processor.locked" [(ngModel)]="processor.checked" (ionChange)="changePreference(notification, '', processor)" [disabled]="notification.updating">
</ion-toggle>
<ion-note item-end *ngIf="!preferences.disableall && processor.locked">{{ processor.lockedmessage }}</ion-note>
<ion-note item-end *ngIf="preferences.disableall">{{ 'core.settings.disabled' | translate }}</ion-note>
</ion-item>
</ng-container>
<ng-container *ngIf="!groupMessagingEnabled">
<!-- Tablet view --> <!-- Tablet view -->
<ion-row text-wrap class="hidden-phone" align-items-center> <ion-row text-wrap class="hidden-phone" align-items-center>
<ion-col margin-horizontal>{{ processor.displayname }}</ion-col> <ion-col margin-horizontal>{{ processor.displayname }}</ion-col>
@ -68,6 +86,7 @@
<ion-note item-end *ngIf="preferences.disableall">{{ 'core.settings.disabled' | translate }}</ion-note> <ion-note item-end *ngIf="preferences.disableall">{{ 'core.settings.disabled' | translate }}</ion-note>
</ion-item> </ion-item>
</ng-container> </ng-container>
</ng-container>
</ion-card> </ion-card>
</div> </div>
</ng-container> </ng-container>

View File

@ -38,6 +38,7 @@ export class AddonMessagesSettingsPage implements OnDestroy {
onlyContactsValue = AddonMessagesProvider.MESSAGE_PRIVACY_ONLYCONTACTS; onlyContactsValue = AddonMessagesProvider.MESSAGE_PRIVACY_ONLYCONTACTS;
courseMemberValue = AddonMessagesProvider.MESSAGE_PRIVACY_COURSEMEMBER; courseMemberValue = AddonMessagesProvider.MESSAGE_PRIVACY_COURSEMEMBER;
siteValue = AddonMessagesProvider.MESSAGE_PRIVACY_SITE; siteValue = AddonMessagesProvider.MESSAGE_PRIVACY_SITE;
groupMessagingEnabled: boolean;
protected previousContactableValue: number | boolean; protected previousContactableValue: number | boolean;
@ -47,6 +48,7 @@ export class AddonMessagesSettingsPage implements OnDestroy {
const currentSite = sitesProvider.getCurrentSite(); const currentSite = sitesProvider.getCurrentSite();
this.advancedContactable = currentSite && currentSite.isVersionGreaterEqualThan('3.6'); this.advancedContactable = currentSite && currentSite.isVersionGreaterEqualThan('3.6');
this.allowSiteMessaging = currentSite && currentSite.canUseAdvancedFeature('messagingallusers'); this.allowSiteMessaging = currentSite && currentSite.canUseAdvancedFeature('messagingallusers');
this.groupMessagingEnabled = this.messagesProvider.isGroupMessagingEnabled();
} }
/** /**
@ -65,6 +67,22 @@ export class AddonMessagesSettingsPage implements OnDestroy {
*/ */
protected fetchPreferences(): Promise<any> { protected fetchPreferences(): Promise<any> {
return this.messagesProvider.getMessagePreferences().then((preferences) => { return this.messagesProvider.getMessagePreferences().then((preferences) => {
if (this.groupMessagingEnabled) {
// Simplify the preferences.
for (const component of preferences.components) {
// Only display get the notification preferences.
component.notifications = component.notifications.filter((notification) => {
return notification.preferencekey == AddonMessagesProvider.NOTIFICATION_PREFERENCES_KEY;
});
for (const notification of component.notifications) {
for (const processor of notification.processors) {
processor.checked = processor.loggedin.checked || processor.loggedoff.checked;
}
}
}
}
this.preferences = preferences; this.preferences = preferences;
this.contactablePrivacy = preferences.blocknoncontacts; this.contactablePrivacy = preferences.blocknoncontacts;
this.previousContactableValue = this.contactablePrivacy; this.previousContactableValue = this.contactablePrivacy;
@ -136,6 +154,39 @@ export class AddonMessagesSettingsPage implements OnDestroy {
* @param {any} processor Notification processor. * @param {any} processor Notification processor.
*/ */
changePreference(notification: any, state: string, processor: any): void { changePreference(notification: any, state: string, processor: any): void {
if (this.groupMessagingEnabled) {
// Update both states at the same time.
const valueArray = [],
promises = [];
let value = 'none';
notification.processors.forEach((processor) => {
if (processor.checked) {
valueArray.push(processor.name);
}
});
if (value.length > 0) {
value = valueArray.join(',');
}
notification.updating = true;
promises.push(this.userProvider.updateUserPreference(notification.preferencekey + '_loggedin', value));
promises.push(this.userProvider.updateUserPreference(notification.preferencekey + '_loggedoff', value));
Promise.all(promises).then(() => {
// Update the preferences since they were modified.
this.updatePreferencesAfterDelay();
}).catch((error) => {
// Show error and revert change.
this.domUtils.showErrorModal(error);
processor.checked = !processor.checked;
}).finally(() => {
notification.updating = false;
});
} else {
// Update only the specified state.
const processorState = processor[state], const processorState = processor[state],
preferenceName = notification.preferencekey + '_' + processorState.name, preferenceName = notification.preferencekey + '_' + processorState.name,
valueArray = []; valueArray = [];
@ -167,6 +218,7 @@ export class AddonMessagesSettingsPage implements OnDestroy {
notification.updating[state] = false; notification.updating[state] = false;
}); });
} }
}
/** /**
* Refresh the list of preferences. * Refresh the list of preferences.

View File

@ -45,6 +45,8 @@ export class AddonMessagesProvider {
static MESSAGE_CONVERSATION_TYPE_GROUP = 2; // A group conversation. static MESSAGE_CONVERSATION_TYPE_GROUP = 2; // A group conversation.
static LIMIT_MESSAGES = 50; static LIMIT_MESSAGES = 50;
static NOTIFICATION_PREFERENCES_KEY = 'message_provider_moodle_instantmessage';
protected logger; protected logger;
constructor(logger: CoreLoggerProvider, private sitesProvider: CoreSitesProvider, private appProvider: CoreAppProvider, constructor(logger: CoreLoggerProvider, private sitesProvider: CoreSitesProvider, private appProvider: CoreAppProvider,