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; return;
} }
this.disabled = true; this.disabled = true;
this.updateButton({spinner: true}); this.updateButton(user.id, {spinner: true});
this.messagesProvider.isContact(user.id).then((isContact) => { this.messagesProvider.isContact(user.id).then((isContact) => {
if (isContact) { if (isContact) {
@ -123,11 +123,11 @@ export class AddonMessagesAddContactUserHandler implements CoreUserProfileHandle
* @return {Promise<void>} Promise resolved when done. * @return {Promise<void>} Promise resolved when done.
*/ */
protected checkButton(userId: number): Promise<void> { protected checkButton(userId: number): Promise<void> {
this.updateButton({spinner: true}); this.updateButton(userId, {spinner: true});
return this.messagesProvider.isContact(userId).then((isContact) => { return this.messagesProvider.isContact(userId).then((isContact) => {
if (isContact) { if (isContact) {
this.updateButton({ this.updateButton(userId, {
title: 'addon.messages.removecontact', title: 'addon.messages.removecontact',
class: 'addon-messages-removecontact-handler', class: 'addon-messages-removecontact-handler',
icon: 'remove', icon: 'remove',
@ -135,7 +135,7 @@ export class AddonMessagesAddContactUserHandler implements CoreUserProfileHandle
spinner: false spinner: false
}); });
} else { } else {
this.updateButton({ this.updateButton(userId, {
title: 'addon.messages.addcontact', title: 'addon.messages.addcontact',
class: 'addon-messages-addcontact-handler', class: 'addon-messages-addcontact-handler',
icon: 'add', icon: 'add',
@ -145,17 +145,19 @@ export class AddonMessagesAddContactUserHandler implements CoreUserProfileHandle
} }
}).catch(() => { }).catch(() => {
// This fails for some reason, let's just hide the button. // 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. * 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. * @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 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; return;
} }
this.disabled = true; this.disabled = true;
this.updateButton({spinner: true}); this.updateButton(user.id, {spinner: true});
this.messagesProvider.isBlocked(user.id).then((isBlocked) => { this.messagesProvider.isBlocked(user.id).then((isBlocked) => {
if (isBlocked) { if (isBlocked) {
@ -124,11 +124,11 @@ export class AddonMessagesBlockContactUserHandler implements CoreUserProfileHand
* @return {Promise<void>} Promise resolved when done. * @return {Promise<void>} Promise resolved when done.
*/ */
protected checkButton(userId: number): Promise<void> { protected checkButton(userId: number): Promise<void> {
this.updateButton({spinner: true}); this.updateButton(userId, {spinner: true});
return this.messagesProvider.isBlocked(userId).then((isBlocked) => { return this.messagesProvider.isBlocked(userId).then((isBlocked) => {
if (isBlocked) { if (isBlocked) {
this.updateButton({ this.updateButton(userId, {
title: 'addon.messages.unblockcontact', title: 'addon.messages.unblockcontact',
class: 'addon-messages-unblockcontact-handler', class: 'addon-messages-unblockcontact-handler',
icon: 'checkmark-circle', icon: 'checkmark-circle',
@ -136,7 +136,7 @@ export class AddonMessagesBlockContactUserHandler implements CoreUserProfileHand
spinner: false spinner: false
}); });
} else { } else {
this.updateButton({ this.updateButton(userId, {
title: 'addon.messages.blockcontact', title: 'addon.messages.blockcontact',
class: 'addon-messages-blockcontact-handler', class: 'addon-messages-blockcontact-handler',
icon: 'close-circle', icon: 'close-circle',
@ -146,17 +146,19 @@ export class AddonMessagesBlockContactUserHandler implements CoreUserProfileHand
} }
}).catch(() => { }).catch(() => {
// This fails for some reason, let's just hide the button. // 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. * 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. * @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 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) => { }).catch((error) => {
@ -212,6 +212,5 @@ export class CoreUserProfilePage {
ngOnDestroy(): void { ngOnDestroy(): void {
this.subscription && this.subscription.unsubscribe(); this.subscription && this.subscription.unsubscribe();
this.obsProfileRefreshed && this.obsProfileRefreshed.off(); 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'; static UPDATE_HANDLER_EVENT = 'CoreUserDelegate_update_handler_event';
protected observableHandlers: Subject<CoreUserProfileHandlerToDisplay[]> =
new BehaviorSubject<CoreUserProfileHandlerToDisplay[]>([]);
protected userHandlers: CoreUserProfileHandlerToDisplay[] = [];
protected featurePrefix = 'CoreUserDelegate_'; 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, constructor(protected loggerProvider: CoreLoggerProvider, protected sitesProvider: CoreSitesProvider,
private coursesProvider: CoreCoursesProvider, protected eventsProvider: CoreEventsProvider) { private coursesProvider: CoreCoursesProvider, protected eventsProvider: CoreEventsProvider) {
super('CoreUserDelegate', loggerProvider, sitesProvider, eventsProvider); super('CoreUserDelegate', loggerProvider, sitesProvider, eventsProvider);
eventsProvider.on(CoreUserDelegate.UPDATE_HANDLER_EVENT, (data) => { eventsProvider.on(CoreUserDelegate.UPDATE_HANDLER_EVENT, (data) => {
if (data && data.handler) { if (data && data.handler && this.userHandlers[data.userId]) {
const handler = this.userHandlers.find((userHandler) => { const userData = this.userHandlers[data.userId],
handler = userData.handlers.find((userHandler) => {
return userHandler.name == data.handler; return userHandler.name == data.handler;
}); });
if (handler) { if (handler) {
for (const x in data.data) { for (const x in data.data) {
handler.data[x] = data.data[x]; 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. * @return {boolean} True if handlers are loaded, false otherwise.
*/ */
areHandlersLoaded(): boolean { areHandlersLoaded(userId: number): boolean {
return this.loaded; return this.userHandlers[userId] && this.userHandlers[userId].loaded;
} }
/** /**
* Clear current user handlers. * Clear current user handlers.
*
* @param {number} userId The user to clear.
*/ */
clearUserHandlers(): void { clearUserHandlers(userId: number): void {
this.loaded = false; const userData = this.userHandlers[userId];
this.userHandlers = [];
this.observableHandlers.next(this.userHandlers); if (userData) {
userData.handlers = [];
userData.observable.next([]);
userData.loaded = false;
}
} }
/** /**
@ -236,7 +248,17 @@ export class CoreUserDelegate extends CoreDelegate {
promise = Promise.resolve(); 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(() => { promise.then(() => {
const promises = []; const promises = [];
@ -247,7 +269,7 @@ export class CoreUserDelegate extends CoreDelegate {
isEnabledForUser = handler.isEnabledForUser(user, courseId, navOptions, admOptions), isEnabledForUser = handler.isEnabledForUser(user, courseId, navOptions, admOptions),
promise = Promise.resolve(isEnabledForUser).then((enabled) => { promise = Promise.resolve(isEnabledForUser).then((enabled) => {
if (enabled) { if (enabled) {
this.userHandlers.push({ userData.handlers.push({
name: name, name: name,
data: handler.getDisplayData(user, courseId), data: handler.getDisplayData(user, courseId),
priority: handler.priority, priority: handler.priority,
@ -264,18 +286,18 @@ export class CoreUserDelegate extends CoreDelegate {
return Promise.all(promises).then(() => { return Promise.all(promises).then(() => {
// Sort them by priority. // Sort them by priority.
this.userHandlers.sort((a, b) => { userData.handlers.sort((a, b) => {
return b.priority - a.priority; return b.priority - a.priority;
}); });
this.loaded = true; userData.loaded = true;
this.observableHandlers.next(this.userHandlers); userData.observable.next(userData.handlers);
}); });
}).catch(() => { }).catch(() => {
// Never fails. // Never fails.
this.loaded = true; userData.loaded = true;
this.observableHandlers.next(this.userHandlers); userData.observable.next(userData.handlers);
}); });
return this.observableHandlers; return userData.observable;
} }
} }