MOBILE-3833 chat: Remove deprecated list manager
This commit is contained in:
		
							parent
							
								
									4cdcb23d97
								
							
						
					
					
						commit
						a02bb045db
					
				
							
								
								
									
										142
									
								
								src/addons/mod/chat/classes/chat-sessions-source.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										142
									
								
								src/addons/mod/chat/classes/chat-sessions-source.ts
									
									
									
									
									
										Normal file
									
								
							| @ -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<AddonModChatSessionFormatted> { | ||||
| 
 | ||||
|     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<void> { | ||||
|         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<unknown>[] = []; | ||||
| 
 | ||||
|         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<void> { | ||||
|         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<AddonModChatSession, 'sessionusers'> & { | ||||
|     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.
 | ||||
| }; | ||||
| @ -19,7 +19,7 @@ | ||||
|                     <ng-container *ngIf="groupInfo.separateGroups">{{'core.groupsseparate' | translate }}</ng-container> | ||||
|                     <ng-container *ngIf="groupInfo.visibleGroups">{{'core.groupsvisible' | translate }}</ng-container> | ||||
|                 </ion-label> | ||||
|                 <ion-select [(ngModel)]="groupId" (ionChange)="fetchSessions(true)" aria-labelledby="addon-chat-groupslabel" | ||||
|                 <ion-select [(ngModel)]="groupId" (ionChange)="reloadSessions()" aria-labelledby="addon-chat-groupslabel" | ||||
|                     interface="action-sheet" [interfaceOptions]="{header: 'core.group' | translate}"> | ||||
|                     <ion-select-option *ngFor="let groupOpt of groupInfo.groups" [value]="groupOpt.id"> | ||||
|                         {{groupOpt.name}} | ||||
| @ -29,7 +29,7 @@ | ||||
| 
 | ||||
|             <ion-item> | ||||
|                 <ion-label>{{ 'addon.mod_chat.showincompletesessions' | translate }}</ion-label> | ||||
|                 <ion-toggle [(ngModel)]="showAll" (ionChange)="fetchSessions(true)"></ion-toggle> | ||||
|                 <ion-toggle [(ngModel)]="showAll" (ionChange)="reloadSessions()"></ion-toggle> | ||||
|             </ion-item> | ||||
| 
 | ||||
|             <ion-card *ngFor="let session of sessions.items" (click)="sessions.select(session)" button | ||||
|  | ||||
| @ -13,17 +13,14 @@ | ||||
| // limitations under the License.
 | ||||
| 
 | ||||
| import { AfterViewInit, Component, OnDestroy, ViewChild } from '@angular/core'; | ||||
| import { Params } from '@angular/router'; | ||||
| import { CorePageItemsListManager } from '@classes/page-items-list-manager'; | ||||
| import { CoreListItemsManager } from '@classes/items-management/list-items-manager'; | ||||
| import { CoreRoutedItemsManagerSourcesTracker } from '@classes/items-management/routed-items-manager-sources-tracker'; | ||||
| import { CoreSplitViewComponent } from '@components/split-view/split-view'; | ||||
| import { CoreUser } from '@features/user/services/user'; | ||||
| import { IonRefresher } from '@ionic/angular'; | ||||
| import { CoreGroupInfo, CoreGroups } from '@services/groups'; | ||||
| import { CoreGroupInfo } from '@services/groups'; | ||||
| import { CoreNavigator } from '@services/navigator'; | ||||
| import { CoreDomUtils } from '@services/utils/dom'; | ||||
| import { CoreUtils } from '@services/utils/utils'; | ||||
| import { Translate } from '@singletons'; | ||||
| import { AddonModChat, AddonModChatSession, AddonModChatSessionUser } from '../../services/chat'; | ||||
| import { AddonModChatSessionFormatted, AddonModChatSessionsSource } from '../../classes/chat-sessions-source'; | ||||
| 
 | ||||
| /** | ||||
|  * Page that displays list of chat sessions. | ||||
| @ -36,28 +33,19 @@ export class AddonModChatSessionsPage implements AfterViewInit, OnDestroy { | ||||
| 
 | ||||
|     @ViewChild(CoreSplitViewComponent) splitView!: CoreSplitViewComponent; | ||||
| 
 | ||||
|     sessions!: AddonChatSessionsManager; | ||||
|     showAll = false; | ||||
|     groupId = 0; | ||||
|     groupInfo?: CoreGroupInfo; | ||||
| 
 | ||||
|     protected courseId!: number; | ||||
|     protected cmId!: number; | ||||
|     protected chatId!: number; | ||||
|     sessions!: CoreListItemsManager<AddonModChatSessionFormatted, AddonModChatSessionsSource>; | ||||
| 
 | ||||
|     constructor() { | ||||
|         this.sessions = new AddonChatSessionsManager(AddonModChatSessionsPage); | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * @inheritdoc | ||||
|      */ | ||||
|     async ngAfterViewInit(): Promise<void> { | ||||
|         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<void> { | ||||
|         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<void> { | ||||
|         const modal = showLoading ? await CoreDomUtils.showModalLoading() : null; | ||||
| 
 | ||||
|     async fetchSessions(): Promise<void> { | ||||
|         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<unknown>[] = []; | ||||
| 
 | ||||
|             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<void> { | ||||
|         if (sessionUser.userfullname) { | ||||
|             return; | ||||
|         } | ||||
|     async reloadSessions(): Promise<void> { | ||||
|         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<void> { | ||||
|         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<AddonModChatSessionFormatted> { | ||||
| 
 | ||||
|     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<AddonModChatSession, 'sessionusers'> & { | ||||
|     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.
 | ||||
| }; | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user