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,36 +37,55 @@
<ion-card list *ngFor="let notification of component.notifications">
<ion-item-divider color="light" text-wrap>
<ion-row no-padding>
<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.loggedoff' | translate }}</ion-col>
<ng-container *ngIf="!groupMessagingEnabled">
<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 *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-item-divider>
<ng-container *ngFor="let processor of notification.processors">
<!-- Tablet view -->
<ion-row text-wrap class="hidden-phone" align-items-center>
<ion-col margin-horizontal>{{ processor.displayname }}</ion-col>
<ion-col col-2 text-center *ngFor="let state of ['loggedin', 'loggedoff']">
<!-- If notifications not disabled, show toggle. -->
<ion-spinner [hidden]="preferences.disableall || !(notification.updating && notification.updating[state])"></ion-spinner>
<ion-toggle *ngIf="!preferences.disableall && !processor.locked" [(ngModel)]="processor[state].checked" (ionChange)="changePreference(notification, state, processor)" [disabled]="notification.updating && notification.updating[state]">
<!-- 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>
<div padding class="text-gray" *ngIf="!preferences.disableall && processor.locked">{{'core.settings.locked' | translate }}</div>
<!-- If notifications are disabled, show "Disabled" instead of toggle. -->
<span *ngIf="preferences.disableall">{{ 'core.settings.disabled' | translate }}</span>
</ion-col>
</ion-row>
<!-- Phone view -->
<ion-list-header text-wrap class="hidden-tablet">{{ processor.displayname }}</ion-list-header>
<!-- If notifications not disabled, show toggles. If notifications are disabled, show "Disabled" instead of toggle. -->
<ion-item *ngFor="let state of ['loggedin', 'loggedoff']" text-wrap class="hidden-tablet">
<ion-label>{{ 'core.settings.' + state | translate }}</ion-label>
<ion-spinner item-end *ngIf="!preferences.disableall && (notification.updating && notification.updating[state])"></ion-spinner>
<ion-toggle item-end *ngIf="!preferences.disableall && !processor.locked" [(ngModel)]="processor[state].checked" (ionChange)="changePreference(notification, state, processor)" [disabled]="notification.updating && notification.updating[state]">
</ion-toggle>
<ion-note item-end *ngIf="!preferences.disableall && processor.locked">{{'core.settings.locked' | translate }}</ion-note>
<ion-note item-end *ngIf="preferences.disableall">{{ 'core.settings.disabled' | translate }}</ion-note>
</ion-item>
<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 -->
<ion-row text-wrap class="hidden-phone" align-items-center>
<ion-col margin-horizontal>{{ processor.displayname }}</ion-col>
<ion-col col-2 text-center *ngFor="let state of ['loggedin', 'loggedoff']">
<!-- If notifications not disabled, show toggle. -->
<ion-spinner [hidden]="preferences.disableall || !(notification.updating && notification.updating[state])"></ion-spinner>
<ion-toggle *ngIf="!preferences.disableall && !processor.locked" [(ngModel)]="processor[state].checked" (ionChange)="changePreference(notification, state, processor)" [disabled]="notification.updating && notification.updating[state]">
</ion-toggle>
<div padding class="text-gray" *ngIf="!preferences.disableall && processor.locked">{{'core.settings.locked' | translate }}</div>
<!-- If notifications are disabled, show "Disabled" instead of toggle. -->
<span *ngIf="preferences.disableall">{{ 'core.settings.disabled' | translate }}</span>
</ion-col>
</ion-row>
<!-- Phone view -->
<ion-list-header text-wrap class="hidden-tablet">{{ processor.displayname }}</ion-list-header>
<!-- If notifications not disabled, show toggles. If notifications are disabled, show "Disabled" instead of toggle. -->
<ion-item *ngFor="let state of ['loggedin', 'loggedoff']" text-wrap class="hidden-tablet">
<ion-label>{{ 'core.settings.' + state | translate }}</ion-label>
<ion-spinner item-end *ngIf="!preferences.disableall && (notification.updating && notification.updating[state])"></ion-spinner>
<ion-toggle item-end *ngIf="!preferences.disableall && !processor.locked" [(ngModel)]="processor[state].checked" (ionChange)="changePreference(notification, state, processor)" [disabled]="notification.updating && notification.updating[state]">
</ion-toggle>
<ion-note item-end *ngIf="!preferences.disableall && processor.locked">{{'core.settings.locked' | translate }}</ion-note>
<ion-note item-end *ngIf="preferences.disableall">{{ 'core.settings.disabled' | translate }}</ion-note>
</ion-item>
</ng-container>
</ng-container>
</ion-card>
</div>

View File

@ -38,6 +38,7 @@ export class AddonMessagesSettingsPage implements OnDestroy {
onlyContactsValue = AddonMessagesProvider.MESSAGE_PRIVACY_ONLYCONTACTS;
courseMemberValue = AddonMessagesProvider.MESSAGE_PRIVACY_COURSEMEMBER;
siteValue = AddonMessagesProvider.MESSAGE_PRIVACY_SITE;
groupMessagingEnabled: boolean;
protected previousContactableValue: number | boolean;
@ -47,6 +48,7 @@ export class AddonMessagesSettingsPage implements OnDestroy {
const currentSite = sitesProvider.getCurrentSite();
this.advancedContactable = currentSite && currentSite.isVersionGreaterEqualThan('3.6');
this.allowSiteMessaging = currentSite && currentSite.canUseAdvancedFeature('messagingallusers');
this.groupMessagingEnabled = this.messagesProvider.isGroupMessagingEnabled();
}
/**
@ -65,6 +67,22 @@ export class AddonMessagesSettingsPage implements OnDestroy {
*/
protected fetchPreferences(): Promise<any> {
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.contactablePrivacy = preferences.blocknoncontacts;
this.previousContactableValue = this.contactablePrivacy;
@ -136,36 +154,70 @@ export class AddonMessagesSettingsPage implements OnDestroy {
* @param {any} processor Notification processor.
*/
changePreference(notification: any, state: string, processor: any): void {
const processorState = processor[state],
preferenceName = notification.preferencekey + '_' + processorState.name,
valueArray = [];
let value = 'none';
if (this.groupMessagingEnabled) {
// Update both states at the same time.
const valueArray = [],
promises = [];
let value = 'none';
notification.processors.forEach((processor) => {
if (processor[state].checked) {
valueArray.push(processor.name);
notification.processors.forEach((processor) => {
if (processor.checked) {
valueArray.push(processor.name);
}
});
if (value.length > 0) {
value = valueArray.join(',');
}
});
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],
preferenceName = notification.preferencekey + '_' + processorState.name,
valueArray = [];
let value = 'none';
notification.processors.forEach((processor) => {
if (processor[state].checked) {
valueArray.push(processor.name);
}
});
if (value.length > 0) {
value = valueArray.join(',');
}
if (!notification.updating) {
notification.updating = {};
}
notification.updating[state] = true;
this.userProvider.updateUserPreference(preferenceName, value).then(() => {
// Update the preferences since they were modified.
this.updatePreferencesAfterDelay();
}).catch((message) => {
// Show error and revert change.
this.domUtils.showErrorModal(message);
processorState.checked = !processorState.checked;
}).finally(() => {
notification.updating[state] = false;
});
}
if (!notification.updating) {
notification.updating = {};
}
notification.updating[state] = true;
this.userProvider.updateUserPreference(preferenceName, value).then(() => {
// Update the preferences since they were modified.
this.updatePreferencesAfterDelay();
}).catch((message) => {
// Show error and revert change.
this.domUtils.showErrorModal(message);
processorState.checked = !processorState.checked;
}).finally(() => {
notification.updating[state] = false;
});
}
/**

View File

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