From 2dbb6209942dd397363275047eeb4a717f6ac802 Mon Sep 17 00:00:00 2001 From: Noel De Martin Date: Wed, 11 May 2022 14:10:03 +0200 Subject: [PATCH] MOBILE-3321 user: Decouple event constants --- .../components/user-avatar/user-avatar.ts | 4 +-- .../features/user/pages/about/about.page.ts | 12 +++---- .../user/pages/profile/profile.page.ts | 6 ++-- .../features/user/services/user-delegate.ts | 29 ++++++++++++----- src/core/features/user/services/user.ts | 32 +++++++------------ 5 files changed, 44 insertions(+), 39 deletions(-) diff --git a/src/core/components/user-avatar/user-avatar.ts b/src/core/components/user-avatar/user-avatar.ts index 220a58bc3..96f7afd80 100644 --- a/src/core/components/user-avatar/user-avatar.ts +++ b/src/core/components/user-avatar/user-avatar.ts @@ -17,7 +17,7 @@ import { Component, Input, OnInit, OnChanges, OnDestroy, SimpleChange } from '@a import { CoreSites } from '@services/sites'; import { CoreUtils } from '@services/utils/utils'; import { CoreEventObserver, CoreEvents } from '@singletons/events'; -import { CoreUserProvider, CoreUserBasicData } from '@features/user/services/user'; +import { USER_PROFILE_PICTURE_UPDATED, CoreUserBasicData } from '@features/user/services/user'; import { CoreNavigator } from '@services/navigator'; import { CoreNetwork } from '@services/network'; @@ -54,7 +54,7 @@ export class CoreUserAvatarComponent implements OnInit, OnChanges, OnDestroy { this.currentUserId = CoreSites.getCurrentSiteUserId(); this.pictureObserver = CoreEvents.on( - CoreUserProvider.PROFILE_PICTURE_UPDATED, + USER_PROFILE_PICTURE_UPDATED, (data) => { if (data.userId == this.userId) { this.avatarUrl = data.picture; diff --git a/src/core/features/user/pages/about/about.page.ts b/src/core/features/user/pages/about/about.page.ts index 9fb9ae255..4a22aa789 100644 --- a/src/core/features/user/pages/about/about.page.ts +++ b/src/core/features/user/pages/about/about.page.ts @@ -21,7 +21,7 @@ import { CoreDomUtils } from '@services/utils/dom'; import { CoreTextUtils } from '@services/utils/text'; import { CoreUtils } from '@services/utils/utils'; import { CoreEventObserver, CoreEvents } from '@singletons/events'; -import { CoreUser, CoreUserProfile, CoreUserProvider } from '@features/user/services/user'; +import { CoreUser, CoreUserProfile, USER_PROFILE_PICTURE_UPDATED, USER_PROFILE_REFRESHED } from '@features/user/services/user'; import { CoreUserHelper } from '@features/user/services/user-helper'; import { CoreNavigator } from '@services/navigator'; import { CoreIonLoadingElement } from '@classes/ion-loading'; @@ -65,7 +65,7 @@ export class CoreUserAboutPage implements OnInit, OnDestroy { return; } - this.obsProfileRefreshed = CoreEvents.on(CoreUserProvider.PROFILE_REFRESHED, (data) => { + this.obsProfileRefreshed = CoreEvents.on(USER_PROFILE_REFRESHED, (data) => { if (!this.user || !data.user) { return; } @@ -147,7 +147,7 @@ export class CoreUserAboutPage implements OnInit, OnDestroy { await CoreSites.updateSiteInfo(this.site.getId()); } catch { // Cannot update site info. Assume the profile image is the right one. - CoreEvents.trigger(CoreUserProvider.PROFILE_PICTURE_UPDATED, { + CoreEvents.trigger(USER_PROFILE_PICTURE_UPDATED, { userId: this.userId, picture: this.user.profileimageurl, }, this.site.getId()); @@ -158,7 +158,7 @@ export class CoreUserAboutPage implements OnInit, OnDestroy { await this.refreshUser(); } else { // Now they're the same, send event to use the right avatar in the rest of the app. - CoreEvents.trigger(CoreUserProvider.PROFILE_PICTURE_UPDATED, { + CoreEvents.trigger(USER_PROFILE_PICTURE_UPDATED, { userId: this.userId, picture: this.user.profileimageurl, }, this.site.getId()); @@ -181,7 +181,7 @@ export class CoreUserAboutPage implements OnInit, OnDestroy { const profileImageURL = await CoreUser.changeProfilePicture(result.itemid, this.userId, this.site.getId()); - CoreEvents.trigger(CoreUserProvider.PROFILE_PICTURE_UPDATED, { + CoreEvents.trigger(USER_PROFILE_PICTURE_UPDATED, { userId: this.userId, picture: profileImageURL, }, this.site.getId()); @@ -210,7 +210,7 @@ export class CoreUserAboutPage implements OnInit, OnDestroy { event?.complete(); if (this.user) { - CoreEvents.trigger(CoreUserProvider.PROFILE_REFRESHED, { + CoreEvents.trigger(USER_PROFILE_REFRESHED, { courseId: this.courseId, userId: this.userId, user: this.user, diff --git a/src/core/features/user/pages/profile/profile.page.ts b/src/core/features/user/pages/profile/profile.page.ts index b22146d00..d9e7280a9 100644 --- a/src/core/features/user/pages/profile/profile.page.ts +++ b/src/core/features/user/pages/profile/profile.page.ts @@ -21,7 +21,7 @@ import { CoreSite } from '@classes/site'; import { CoreSites } from '@services/sites'; import { CoreDomUtils } from '@services/utils/dom'; import { CoreEventObserver, CoreEvents } from '@singletons/events'; -import { CoreUser, CoreUserProfile, CoreUserProvider } from '@features/user/services/user'; +import { CoreUser, CoreUserProfile, USER_PROFILE_REFRESHED } from '@features/user/services/user'; import { CoreUserHelper } from '@features/user/services/user-helper'; import { CoreUserDelegate, @@ -64,7 +64,7 @@ export class CoreUserProfilePage implements OnInit, OnDestroy { users?: CoreUserSwipeItemsManager; constructor(private route: ActivatedRoute) { - this.obsProfileRefreshed = CoreEvents.on(CoreUserProvider.PROFILE_REFRESHED, (data) => { + this.obsProfileRefreshed = CoreEvents.on(USER_PROFILE_REFRESHED, (data) => { if (!this.user || !data.user) { return; } @@ -187,7 +187,7 @@ export class CoreUserProfilePage implements OnInit, OnDestroy { event?.complete(); if (this.user) { - CoreEvents.trigger(CoreUserProvider.PROFILE_REFRESHED, { + CoreEvents.trigger(USER_PROFILE_REFRESHED, { courseId: this.courseId, userId: this.userId, user: this.user, diff --git a/src/core/features/user/services/user-delegate.ts b/src/core/features/user/services/user-delegate.ts index a4c2bf16c..cbb37dbf3 100644 --- a/src/core/features/user/services/user-delegate.ts +++ b/src/core/features/user/services/user-delegate.ts @@ -18,11 +18,24 @@ import { Subject, BehaviorSubject } from 'rxjs'; import { CoreDelegate, CoreDelegateHandler } from '@classes/delegate'; import { CoreUtils } from '@services/utils/utils'; import { CoreEvents } from '@singletons/events'; -import { CoreUserProfile, CoreUserProvider } from './user'; +import { CoreUserProfile, USER_PROFILE_REFRESHED } from './user'; import { makeSingleton } from '@singletons'; import { CoreCourses, CoreCourseUserAdminOrNavOptionIndexed } from '@features/courses/services/courses'; import { CoreSites } from '@services/sites'; +declare module '@singletons/events' { + + /** + * Augment CoreEventsData interface with events specific to this service. + * + * @see https://www.typescriptlang.org/docs/handbook/declaration-merging.html#module-augmentation + */ + export interface CoreEventsData { + [USER_DELEGATE_UPDATE_HANDLER_EVENT]: CoreUserUpdateHandlerData; + } + +} + /** * Interface that all user profile handlers must implement. */ @@ -168,6 +181,11 @@ export interface CoreUserProfileHandlerToDisplay { type: string; } +/** + * Delegate update handler event. + */ +export const USER_DELEGATE_UPDATE_HANDLER_EVENT = 'CoreUserDelegate_update_handler_event'; + /** * Service to interact with plugins to be shown in user profile. Provides functions to register a plugin * and notify an update in the data. @@ -188,11 +206,6 @@ export class CoreUserDelegateService extends CoreDelegate { + CoreEvents.on(USER_DELEGATE_UPDATE_HANDLER_EVENT, (data) => { const handlersData = this.getHandlersData(data.userId, data.context, data.contextId); // Search the handler. @@ -225,7 +238,7 @@ export class CoreUserDelegateService extends CoreDelegate { + CoreEvents.on(USER_PROFILE_REFRESHED, (data) => { const context = data.courseId ? CoreUserDelegateContext.COURSE : CoreUserDelegateContext.SITE; this.clearHandlerCache(data.userId, context, data.courseId); }); diff --git a/src/core/features/user/services/user.ts b/src/core/features/user/services/user.ts index ed0b956ee..44913b195 100644 --- a/src/core/features/user/services/user.ts +++ b/src/core/features/user/services/user.ts @@ -27,7 +27,6 @@ import { CoreStatusWithWarningsWSResponse, CoreWSExternalWarning } from '@servic import { CoreError } from '@classes/errors/error'; import { USERS_TABLE_NAME, CoreUserDBRecord } from './database/user'; import { CorePushNotifications } from '@features/pushnotifications/services/pushnotifications'; -import { CoreUserDelegateService, CoreUserUpdateHandlerData } from './user-delegate'; const ROOT_CACHE_KEY = 'mmUser:'; @@ -39,13 +38,22 @@ declare module '@singletons/events' { * @see https://www.typescriptlang.org/docs/handbook/declaration-merging.html#module-augmentation */ export interface CoreEventsData { - [CoreUserProvider.PROFILE_REFRESHED]: CoreUserProfileRefreshedData; - [CoreUserProvider.PROFILE_PICTURE_UPDATED]: CoreUserProfilePictureUpdatedData; - [CoreUserDelegateService.UPDATE_HANDLER_EVENT]: CoreUserUpdateHandlerData; + [USER_PROFILE_REFRESHED]: CoreUserProfileRefreshedData; + [USER_PROFILE_PICTURE_UPDATED]: CoreUserProfilePictureUpdatedData; } } +/** + * Profile picture updated event. + */ +export const USER_PROFILE_REFRESHED = 'CoreUserProfileRefreshed'; + +/** + * Profile picture updated event. + */ +export const USER_PROFILE_PICTURE_UPDATED = 'CoreUserProfilePictureUpdated'; + /** * Service to provide user functionalities. */ @@ -53,8 +61,6 @@ declare module '@singletons/events' { export class CoreUserProvider { static readonly PARTICIPANTS_LIST_LIMIT = 50; // Max of participants to retrieve in each WS call. - static readonly PROFILE_REFRESHED = 'CoreUserProfileRefreshed'; - static readonly PROFILE_PICTURE_UPDATED = 'CoreUserProfilePictureUpdated'; protected logger: CoreLogger; @@ -841,20 +847,6 @@ export class CoreUserProvider { } export const CoreUser = makeSingleton(CoreUserProvider); -declare module '@singletons/events' { - - /** - * Augment CoreEventsData interface with events specific to this service. - * - * @see https://www.typescriptlang.org/docs/handbook/declaration-merging.html#module-augmentation - */ - export interface CoreEventsData { - [CoreUserProvider.PROFILE_REFRESHED]: CoreUserProfileRefreshedData; - [CoreUserProvider.PROFILE_PICTURE_UPDATED]: CoreUserProfilePictureUpdatedData; - } - -} - /** * Data passed to PROFILE_REFRESHED event. */