MOBILE-2514 user: Fix wrong plugins when viewing 2 users at same time

main
Dani Palou 2018-07-24 14:48:23 +02:00
parent 5841425b6b
commit d91942f1b4
4 changed files with 64 additions and 39 deletions

View File

@ -91,7 +91,7 @@ export class AddonMessagesAddContactUserHandler implements CoreUserProfileHandle
return;
}
this.disabled = true;
this.updateButton({spinner: true});
this.updateButton(user.id, {spinner: true});
this.messagesProvider.isContact(user.id).then((isContact) => {
if (isContact) {
@ -123,11 +123,11 @@ export class AddonMessagesAddContactUserHandler implements CoreUserProfileHandle
* @return {Promise<void>} Promise resolved when done.
*/
protected checkButton(userId: number): Promise<void> {
this.updateButton({spinner: true});
this.updateButton(userId, {spinner: true});
return this.messagesProvider.isContact(userId).then((isContact) => {
if (isContact) {
this.updateButton({
this.updateButton(userId, {
title: 'addon.messages.removecontact',
class: 'addon-messages-removecontact-handler',
icon: 'remove',
@ -135,7 +135,7 @@ export class AddonMessagesAddContactUserHandler implements CoreUserProfileHandle
spinner: false
});
} else {
this.updateButton({
this.updateButton(userId, {
title: 'addon.messages.addcontact',
class: 'addon-messages-addcontact-handler',
icon: 'add',
@ -145,17 +145,19 @@ export class AddonMessagesAddContactUserHandler implements CoreUserProfileHandle
}
}).catch(() => {
// This fails for some reason, let's just hide the button.
this.updateButton({hidden: true});
this.updateButton(userId, {hidden: true});
});
}
/**
* Triggers the event to update the handler information.
*
* @param {number} userId The user ID the handler belongs to.
* @param {any} data Data that should be updated.
*/
protected updateButton(data: any): void {
protected updateButton(userId: number, data: any): void {
// This fails for some reason, let's just hide the button.
this.eventsProvider.trigger(CoreUserDelegate.UPDATE_HANDLER_EVENT, { handler: this.name, data: data });
this.eventsProvider.trigger(CoreUserDelegate.UPDATE_HANDLER_EVENT, { handler: this.name, data: data, userId: userId });
}
/**

View File

@ -92,7 +92,7 @@ export class AddonMessagesBlockContactUserHandler implements CoreUserProfileHand
return;
}
this.disabled = true;
this.updateButton({spinner: true});
this.updateButton(user.id, {spinner: true});
this.messagesProvider.isBlocked(user.id).then((isBlocked) => {
if (isBlocked) {
@ -124,11 +124,11 @@ export class AddonMessagesBlockContactUserHandler implements CoreUserProfileHand
* @return {Promise<void>} Promise resolved when done.
*/
protected checkButton(userId: number): Promise<void> {
this.updateButton({spinner: true});
this.updateButton(userId, {spinner: true});
return this.messagesProvider.isBlocked(userId).then((isBlocked) => {
if (isBlocked) {
this.updateButton({
this.updateButton(userId, {
title: 'addon.messages.unblockcontact',
class: 'addon-messages-unblockcontact-handler',
icon: 'checkmark-circle',
@ -136,7 +136,7 @@ export class AddonMessagesBlockContactUserHandler implements CoreUserProfileHand
spinner: false
});
} else {
this.updateButton({
this.updateButton(userId, {
title: 'addon.messages.blockcontact',
class: 'addon-messages-blockcontact-handler',
icon: 'close-circle',
@ -146,17 +146,19 @@ export class AddonMessagesBlockContactUserHandler implements CoreUserProfileHand
}
}).catch(() => {
// This fails for some reason, let's just hide the button.
this.updateButton({hidden: true});
this.updateButton(userId, {hidden: true});
});
}
/**
* Triggers the event to update the handler information.
*
* @param {number} userId The user ID the handler belongs to.
* @param {any} data Data that should be updated.
*/
protected updateButton(data: any): void {
protected updateButton(userId: number, data: any): void {
// This fails for some reason, let's just hide the button.
this.eventsProvider.trigger(CoreUserDelegate.UPDATE_HANDLER_EVENT, { handler: this.name, data: data });
this.eventsProvider.trigger(CoreUserDelegate.UPDATE_HANDLER_EVENT, { handler: this.name, data: data, userId: userId });
}
/**

View File

@ -122,7 +122,7 @@ export class CoreUserProfilePage {
}
});
this.isLoadingHandlers = !this.userDelegate.areHandlersLoaded();
this.isLoadingHandlers = !this.userDelegate.areHandlersLoaded(user.id);
});
}).catch((error) => {
@ -212,6 +212,5 @@ export class CoreUserProfilePage {
ngOnDestroy(): void {
this.subscription && this.subscription.unsubscribe();
this.obsProfileRefreshed && this.obsProfileRefreshed.off();
this.userDelegate.clearUserHandlers();
}
}

View File

@ -162,26 +162,32 @@ export class CoreUserDelegate extends CoreDelegate {
*/
static UPDATE_HANDLER_EVENT = 'CoreUserDelegate_update_handler_event';
protected observableHandlers: Subject<CoreUserProfileHandlerToDisplay[]> =
new BehaviorSubject<CoreUserProfileHandlerToDisplay[]>([]);
protected userHandlers: CoreUserProfileHandlerToDisplay[] = [];
protected featurePrefix = 'CoreUserDelegate_';
protected loaded = false;
// Hold the handlers and the observable to notify them for each user.
protected userHandlers: {
[userId: number]: {
loaded: boolean, // Whether the handlers are loaded.
handlers: CoreUserProfileHandlerToDisplay[], // List of handlers.
observable: Subject<CoreUserProfileHandlerToDisplay[]> // Observale to notify the handlers.
}} = {};
constructor(protected loggerProvider: CoreLoggerProvider, protected sitesProvider: CoreSitesProvider,
private coursesProvider: CoreCoursesProvider, protected eventsProvider: CoreEventsProvider) {
super('CoreUserDelegate', loggerProvider, sitesProvider, eventsProvider);
eventsProvider.on(CoreUserDelegate.UPDATE_HANDLER_EVENT, (data) => {
if (data && data.handler) {
const handler = this.userHandlers.find((userHandler) => {
if (data && data.handler && this.userHandlers[data.userId]) {
const userData = this.userHandlers[data.userId],
handler = userData.handlers.find((userHandler) => {
return userHandler.name == data.handler;
});
if (handler) {
for (const x in data.data) {
handler.data[x] = data.data[x];
}
this.observableHandlers.next(this.userHandlers);
userData.observable.next(userData.handlers);
}
}
});
@ -192,17 +198,23 @@ export class CoreUserDelegate extends CoreDelegate {
*
* @return {boolean} True if handlers are loaded, false otherwise.
*/
areHandlersLoaded(): boolean {
return this.loaded;
areHandlersLoaded(userId: number): boolean {
return this.userHandlers[userId] && this.userHandlers[userId].loaded;
}
/**
* Clear current user handlers.
*
* @param {number} userId The user to clear.
*/
clearUserHandlers(): void {
this.loaded = false;
this.userHandlers = [];
this.observableHandlers.next(this.userHandlers);
clearUserHandlers(userId: number): void {
const userData = this.userHandlers[userId];
if (userData) {
userData.handlers = [];
userData.observable.next([]);
userData.loaded = false;
}
}
/**
@ -236,7 +248,17 @@ export class CoreUserDelegate extends CoreDelegate {
promise = Promise.resolve();
}
this.userHandlers = [];
// Initialize the user handlers if it isn't initialized already.
if (!this.userHandlers[user.id]) {
this.userHandlers[user.id] = {
loaded: false,
handlers: [],
observable: new BehaviorSubject<CoreUserProfileHandlerToDisplay[]>([])
};
}
const userData = this.userHandlers[user.id];
userData.handlers = [];
promise.then(() => {
const promises = [];
@ -247,7 +269,7 @@ export class CoreUserDelegate extends CoreDelegate {
isEnabledForUser = handler.isEnabledForUser(user, courseId, navOptions, admOptions),
promise = Promise.resolve(isEnabledForUser).then((enabled) => {
if (enabled) {
this.userHandlers.push({
userData.handlers.push({
name: name,
data: handler.getDisplayData(user, courseId),
priority: handler.priority,
@ -264,18 +286,18 @@ export class CoreUserDelegate extends CoreDelegate {
return Promise.all(promises).then(() => {
// Sort them by priority.
this.userHandlers.sort((a, b) => {
userData.handlers.sort((a, b) => {
return b.priority - a.priority;
});
this.loaded = true;
this.observableHandlers.next(this.userHandlers);
userData.loaded = true;
userData.observable.next(userData.handlers);
});
}).catch(() => {
// Never fails.
this.loaded = true;
this.observableHandlers.next(this.userHandlers);
userData.loaded = true;
userData.observable.next(userData.handlers);
});
return this.observableHandlers;
return userData.observable;
}
}