From d91942f1b4418d8d274d899b1f93c52127d9b91b Mon Sep 17 00:00:00 2001 From: Dani Palou Date: Tue, 24 Jul 2018 14:48:23 +0200 Subject: [PATCH] MOBILE-2514 user: Fix wrong plugins when viewing 2 users at same time --- .../providers/user-add-contact-handler.ts | 16 +++-- .../providers/user-block-contact-handler.ts | 16 +++-- src/core/user/pages/profile/profile.ts | 3 +- src/core/user/providers/user-delegate.ts | 68 ++++++++++++------- 4 files changed, 64 insertions(+), 39 deletions(-) diff --git a/src/addon/messages/providers/user-add-contact-handler.ts b/src/addon/messages/providers/user-add-contact-handler.ts index 0dac7a96d..4fdc8250b 100644 --- a/src/addon/messages/providers/user-add-contact-handler.ts +++ b/src/addon/messages/providers/user-add-contact-handler.ts @@ -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} Promise resolved when done. */ protected checkButton(userId: number): Promise { - 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 }); } /** diff --git a/src/addon/messages/providers/user-block-contact-handler.ts b/src/addon/messages/providers/user-block-contact-handler.ts index a329c06d2..bc63f6f30 100644 --- a/src/addon/messages/providers/user-block-contact-handler.ts +++ b/src/addon/messages/providers/user-block-contact-handler.ts @@ -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} Promise resolved when done. */ protected checkButton(userId: number): Promise { - 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 }); } /** diff --git a/src/core/user/pages/profile/profile.ts b/src/core/user/pages/profile/profile.ts index 90eaf8b2c..2f9952852 100644 --- a/src/core/user/pages/profile/profile.ts +++ b/src/core/user/pages/profile/profile.ts @@ -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(); } } diff --git a/src/core/user/providers/user-delegate.ts b/src/core/user/providers/user-delegate.ts index 0261ee2f3..7bab647d1 100644 --- a/src/core/user/providers/user-delegate.ts +++ b/src/core/user/providers/user-delegate.ts @@ -162,26 +162,32 @@ export class CoreUserDelegate extends CoreDelegate { */ static UPDATE_HANDLER_EVENT = 'CoreUserDelegate_update_handler_event'; - protected observableHandlers: Subject = - new BehaviorSubject([]); - 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 // 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) => { - return userHandler.name == data.handler; - }); + 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([]) + }; + } + + 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; } }