From a02bb045db561bc201f18ca10e2e0bc8d38cfac1 Mon Sep 17 00:00:00 2001 From: Noel De Martin Date: Wed, 12 Jan 2022 14:04:18 +0100 Subject: [PATCH] MOBILE-3833 chat: Remove deprecated list manager --- .../mod/chat/classes/chat-sessions-source.ts | 142 +++++++++++++ .../mod/chat/pages/sessions/sessions.html | 4 +- .../mod/chat/pages/sessions/sessions.ts | 200 +++++------------- 3 files changed, 199 insertions(+), 147 deletions(-) create mode 100644 src/addons/mod/chat/classes/chat-sessions-source.ts diff --git a/src/addons/mod/chat/classes/chat-sessions-source.ts b/src/addons/mod/chat/classes/chat-sessions-source.ts new file mode 100644 index 000000000..e53b4be01 --- /dev/null +++ b/src/addons/mod/chat/classes/chat-sessions-source.ts @@ -0,0 +1,142 @@ +// (C) Copyright 2015 Moodle Pty Ltd. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +import { Params } from '@angular/router'; +import { CoreRoutedItemsManagerSource } from '@classes/items-management/routed-items-manager-source'; +import { CoreUser } from '@features/user/services/user'; +import { CoreGroupInfo, CoreGroups } from '@services/groups'; +import { CoreUtils } from '@services/utils/utils'; +import { Translate } from '@singletons'; +import { AddonModChat, AddonModChatSession, AddonModChatSessionUser } from '../services/chat'; + +/** + * Provides a collection of sessions. + */ +export class AddonModChatSessionsSource extends CoreRoutedItemsManagerSource { + + readonly COURSE_ID: number; + readonly CHAT_ID: number; + readonly CM_ID: number; + + showAll = false; + groupId = 0; + groupInfo?: CoreGroupInfo; + + constructor(courseId: number, chatId: number, cmId: number) { + super(); + + this.COURSE_ID = courseId; + this.CHAT_ID = chatId; + this.CM_ID = cmId; + } + + /** + * Invalidate chat cache. + */ + async invalidateCache(): Promise { + await CoreUtils.ignoreErrors(CoreUtils.allPromises([ + CoreGroups.invalidateActivityGroupInfo(this.CM_ID), + AddonModChat.invalidateSessions(this.CHAT_ID, this.groupId, this.showAll), + ])); + } + + /** + * @inheritdoc + */ + protected async loadPageItems(): Promise<{ items: AddonModChatSessionFormatted[] }> { + this.groupInfo = await CoreGroups.getActivityGroupInfo(this.CM_ID, false); + + this.groupId = CoreGroups.validateGroupId(this.groupId, this.groupInfo); + + const sessions = await AddonModChat.getSessions(this.CHAT_ID, this.groupId, this.showAll, { cmId: this.CM_ID }); + + // Fetch user profiles. + const promises: Promise[] = []; + + const formattedSessions = sessions.map((session: AddonModChatSessionFormatted) => { + session.duration = session.sessionend - session.sessionstart; + session.sessionusers.forEach((sessionUser) => { + // The WS does not return the user name, fetch user profile. + promises.push(this.loadUserFullname(sessionUser)); + }); + + // If session has more than 4 users we display a "Show more" link. + session.allsessionusers = session.sessionusers; + if (session.sessionusers.length > 4) { + session.sessionusers = session.allsessionusers.slice(0, 3); + } + + return session; + }); + + await Promise.all(promises); + + return { items: formattedSessions }; + } + + /** + * @inheritdoc + */ + getItemPath(session: AddonModChatSessionFormatted): string { + return `${session.sessionstart}/${session.sessionend}`; + } + + /** + * @inheritdoc + */ + getItemQueryParams(): Params { + return { + chatId: this.CHAT_ID, + groupId: this.groupId, + }; + } + + /** + * Load the fullname of a user. + * + * @param id User ID. + * @return Promise resolved when done. + */ + protected async loadUserFullname(sessionUser: AddonModChatUserSessionFormatted): Promise { + if (sessionUser.userfullname) { + return; + } + + try { + const user = await CoreUser.getProfile(sessionUser.userid, this.COURSE_ID, true); + + sessionUser.userfullname = user.fullname; + } catch { + // Error getting profile, most probably the user is deleted. + sessionUser.userfullname = Translate.instant('core.deleteduser') + ' ' + sessionUser.userid; + } + } + +} + +/** + * Fields added to chat session in this view. + */ +export type AddonModChatSessionFormatted = Omit & { + duration?: number; // Session duration. + sessionusers: AddonModChatUserSessionFormatted[]; + allsessionusers?: AddonModChatUserSessionFormatted[]; // All session users. +}; + +/** + * Fields added to user session in this view. + */ +export type AddonModChatUserSessionFormatted = AddonModChatSessionUser & { + userfullname?: string; // User full name. +}; diff --git a/src/addons/mod/chat/pages/sessions/sessions.html b/src/addons/mod/chat/pages/sessions/sessions.html index d4f7bf45e..53115aac3 100644 --- a/src/addons/mod/chat/pages/sessions/sessions.html +++ b/src/addons/mod/chat/pages/sessions/sessions.html @@ -19,7 +19,7 @@ {{'core.groupsseparate' | translate }} {{'core.groupsvisible' | translate }} - {{groupOpt.name}} @@ -29,7 +29,7 @@ {{ 'addon.mod_chat.showincompletesessions' | translate }} - + ; constructor() { - this.sessions = new AddonChatSessionsManager(AddonModChatSessionsPage); - } - - /** - * @inheritdoc - */ - async ngAfterViewInit(): Promise { try { - this.courseId = CoreNavigator.getRequiredRouteNumberParam('courseId'); - this.cmId = CoreNavigator.getRequiredRouteNumberParam('cmId'); - this.chatId = CoreNavigator.getRequiredRouteNumberParam('chatId'); - this.sessions.setChatId(this.chatId); + const courseId = CoreNavigator.getRequiredRouteNumberParam('courseId'); + const chatId = CoreNavigator.getRequiredRouteNumberParam('chatId'); + const cmId = CoreNavigator.getRequiredRouteNumberParam('cmId'); + const source = CoreRoutedItemsManagerSourcesTracker.getOrCreateSource( + AddonModChatSessionsSource, + [courseId, chatId, cmId], + ); + + this.sessions = new CoreListItemsManager(source, AddonModChatSessionsPage); } catch (error) { CoreDomUtils.showErrorModal(error); @@ -65,7 +53,32 @@ export class AddonModChatSessionsPage implements AfterViewInit, OnDestroy { return; } + } + get groupId(): number { + return this.sessions.getSource().groupId; + } + + set groupId(value: number) { + this.sessions.getSource().groupId = value; + } + + get showAll(): boolean { + return this.sessions.getSource().showAll; + } + + set showAll(value: boolean) { + this.sessions.getSource().showAll = value; + } + + get groupInfo(): CoreGroupInfo | undefined { + return this.sessions.getSource().groupInfo; + } + + /** + * @inheritdoc + */ + async ngAfterViewInit(): Promise { await this.fetchSessions(); this.sessions.start(this.splitView); @@ -75,66 +88,27 @@ export class AddonModChatSessionsPage implements AfterViewInit, OnDestroy { * Fetch chat sessions. * * @param showLoading Display a loading modal. - * @return Promise resolved when done. */ - async fetchSessions(showLoading?: boolean): Promise { - const modal = showLoading ? await CoreDomUtils.showModalLoading() : null; - + async fetchSessions(): Promise { try { - this.groupInfo = await CoreGroups.getActivityGroupInfo(this.cmId, false); - - this.groupId = CoreGroups.validateGroupId(this.groupId, this.groupInfo); - this.sessions.setGroupId(this.groupId); - - const sessions = await AddonModChat.getSessions(this.chatId, this.groupId, this.showAll, { cmId: this.cmId }); - - // Fetch user profiles. - const promises: Promise[] = []; - - const formattedSessions = sessions.map((session: AddonModChatSessionFormatted) => { - session.duration = session.sessionend - session.sessionstart; - session.sessionusers.forEach((sessionUser) => { - // The WS does not return the user name, fetch user profile. - promises.push(this.loadUserFullname(sessionUser)); - }); - - // If session has more than 4 users we display a "Show more" link. - session.allsessionusers = session.sessionusers; - if (session.sessionusers.length > 4) { - session.sessionusers = session.allsessionusers.slice(0, 3); - } - - return session; - }); - - await Promise.all(promises); - - this.sessions.setItems(formattedSessions); + await this.sessions.load(); } catch (error) { CoreDomUtils.showErrorModalDefault(error, 'core.errorloadingcontent', true); - } finally { - modal?.dismiss(); } } /** - * Load the fullname of a user. - * - * @param id User ID. - * @return Promise resolved when done. + * Reload chat sessions. */ - protected async loadUserFullname(sessionUser: AddonModChatUserSessionFormatted): Promise { - if (sessionUser.userfullname) { - return; - } + async reloadSessions(): Promise { + const modal = await CoreDomUtils.showModalLoading(); try { - const user = await CoreUser.getProfile(sessionUser.userid, this.courseId, true); - - sessionUser.userfullname = user.fullname; - } catch { - // Error getting profile, most probably the user is deleted. - sessionUser.userfullname = Translate.instant('core.deleteduser') + ' ' + sessionUser.userid; + await this.sessions.reload(); + } catch (error) { + CoreDomUtils.showErrorModalDefault(error, 'core.errorloadingcontent', true); + } finally { + modal.dismiss(); } } @@ -145,11 +119,9 @@ export class AddonModChatSessionsPage implements AfterViewInit, OnDestroy { */ async refreshSessions(refresher: IonRefresher): Promise { try { - await CoreUtils.ignoreErrors(CoreUtils.allPromises([ - CoreGroups.invalidateActivityGroupInfo(this.cmId), - AddonModChat.invalidateSessions(this.chatId, this.groupId, this.showAll), - ])); + this.sessions.getSource().setDirty(true); + await this.sessions.getSource().invalidateCache(); await this.fetchSessions(); } finally { refresher.complete(); @@ -163,7 +135,10 @@ export class AddonModChatSessionsPage implements AfterViewInit, OnDestroy { * @param event The event. */ showMoreUsers(session: AddonModChatSessionFormatted, event: Event): void { - session.sessionusers = session.allsessionusers!; + if (session.allsessionusers) { + session.sessionusers = session.allsessionusers; + } + event.stopPropagation(); } @@ -175,68 +150,3 @@ export class AddonModChatSessionsPage implements AfterViewInit, OnDestroy { } } - -/** - * Helper class to manage sessions. - */ -class AddonChatSessionsManager extends CorePageItemsListManager { - - chatId = -1; - groupId = 0; - - constructor(pageComponent: unknown) { - super(pageComponent); - } - - /** - * Set chat ID. - * - * @param chatId Chat ID. - */ - setChatId(chatId: number): void { - this.chatId = chatId; - } - - /** - * Set group ID. - * - * @param groupId Group ID. - */ - setGroupId(groupId: number): void { - this.groupId = groupId; - } - - /** - * @inheritdoc - */ - protected getItemPath(session: AddonModChatSessionFormatted): string { - return `${session.sessionstart}/${session.sessionend}`; - } - - /** - * @inheritdoc - */ - protected getItemQueryParams(): Params { - return { - chatId: this.chatId, - groupId: this.groupId, - }; - } - -} - -/** - * Fields added to chat session in this view. - */ -type AddonModChatSessionFormatted = Omit & { - duration?: number; // Session duration. - sessionusers: AddonModChatUserSessionFormatted[]; - allsessionusers?: AddonModChatUserSessionFormatted[]; // All session users. -}; - -/** - * Fields added to user session in this view. - */ -type AddonModChatUserSessionFormatted = AddonModChatSessionUser & { - userfullname?: string; // User full name. -};