Skip to content

Commit

Permalink
Merge pull request #3962 from dpalou/MOBILE-4528
Browse files Browse the repository at this point in the history
MOBILE-4528 chat: Decouple from initial bundle
  • Loading branch information
NoelDeMartin authored Mar 11, 2024
2 parents 162a618 + 181c4d9 commit 02edd58
Show file tree
Hide file tree
Showing 10 changed files with 280 additions and 191 deletions.
26 changes: 18 additions & 8 deletions src/addons/mod/chat/chat.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,11 @@ import { CoreCourseHelper } from '@features/course/services/course-helper';
import { CoreCourseModuleDelegate } from '@features/course/services/module-delegate';
import { CoreCourseModulePrefetchDelegate } from '@features/course/services/module-prefetch-delegate';
import { CoreMainMenuTabRoutingModule } from '@features/mainmenu/mainmenu-tab-routing.module';
import { AddonModChatComponentsModule } from './components/components.module';
import { AddonModChatProvider } from './services/chat';
import { AddonModChatIndexLinkHandler } from './services/handlers/index-link';
import { AddonModChatListLinkHandler } from './services/handlers/list-link';
import { AddonModChatModuleHandler, AddonModChatModuleHandlerService } from './services/handlers/module';
import { AddonModChatPrefetchHandler } from './services/handlers/prefetch';
import { AddonModChatModuleHandler } from './services/handlers/module';
import { getPrefetchHandlerInstance } from './services/handlers/prefetch';
import { ADDON_MOD_CHAT_COMPONENT, ADDON_MOD_CHAT_PAGE_NAME } from './constants';

/**
* Get mod chat services.
Expand All @@ -41,29 +40,40 @@ export async function getModChatServices(): Promise<Type<unknown>[]> {
];
}

/**
* Get mod chat component modules.
*
* @returns Chat component modules.
*/
export async function getModChatComponentModules(): Promise<unknown[]> {
const { AddonModChatComponentsModule } = await import('@addons/mod/chat/components/components.module');

return [AddonModChatComponentsModule];
}

const routes: Routes = [
{
path: AddonModChatModuleHandlerService.PAGE_NAME,
path: ADDON_MOD_CHAT_PAGE_NAME,
loadChildren: () => import('./chat-lazy.module').then(m => m.AddonModChatLazyModule),
},
];

@NgModule({
imports: [
CoreMainMenuTabRoutingModule.forChild(routes),
AddonModChatComponentsModule,
],
providers: [
{
provide: APP_INITIALIZER,
multi: true,
useValue: () => {
CoreCourseModulePrefetchDelegate.registerHandler(getPrefetchHandlerInstance());

CoreCourseModuleDelegate.registerHandler(AddonModChatModuleHandler.instance);
CoreContentLinksDelegate.registerHandler(AddonModChatIndexLinkHandler.instance);
CoreContentLinksDelegate.registerHandler(AddonModChatListLinkHandler.instance);
CoreCourseModulePrefetchDelegate.registerHandler(AddonModChatPrefetchHandler.instance);

CoreCourseHelper.registerModuleReminderClick(AddonModChatProvider.COMPONENT);
CoreCourseHelper.registerModuleReminderClick(ADDON_MOD_CHAT_COMPONENT);
},
},
],
Expand Down
10 changes: 5 additions & 5 deletions src/addons/mod/chat/components/index/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ import { IonContent } from '@ionic/angular';
import { CoreNavigator } from '@services/navigator';
import { CoreTimeUtils } from '@services/utils/time';
import { CoreTime } from '@singletons/time';
import { AddonModChat, AddonModChatChat, AddonModChatProvider } from '../../services/chat';
import { AddonModChatModuleHandlerService } from '../../services/handlers/module';
import { AddonModChat, AddonModChatChat } from '../../services/chat';
import { ADDON_MOD_CHAT_COMPONENT, ADDON_MOD_CHAT_PAGE_NAME } from '../../constants';

/**
* Component that displays a chat.
Expand All @@ -31,7 +31,7 @@ import { AddonModChatModuleHandlerService } from '../../services/handlers/module
})
export class AddonModChatIndexComponent extends CoreCourseModuleMainActivityComponent implements OnInit {

component = AddonModChatProvider.COMPONENT;
component = ADDON_MOD_CHAT_COMPONENT;
pluginName = 'chat';
chat?: AddonModChatChat;
chatTime?: string;
Expand Down Expand Up @@ -88,7 +88,7 @@ export class AddonModChatIndexComponent extends CoreCourseModuleMainActivityComp
const title = this.chat?.name || this.moduleName;

CoreNavigator.navigateToSitePath(
AddonModChatModuleHandlerService.PAGE_NAME + `/${this.courseId}/${this.module.id}/chat`,
`${ADDON_MOD_CHAT_PAGE_NAME}/${this.courseId}/${this.module.id}/chat`,
{
params: {
title,
Expand All @@ -103,7 +103,7 @@ export class AddonModChatIndexComponent extends CoreCourseModuleMainActivityComp
*/
viewSessions(): void {
CoreNavigator.navigateToSitePath(
AddonModChatModuleHandlerService.PAGE_NAME + `/${this.courseId}/${this.module.id}/sessions`,
`${ADDON_MOD_CHAT_PAGE_NAME}/${this.courseId}/${this.module.id}/sessions`,
{
params: {
chatId: this.chat!.id,
Expand Down
23 changes: 23 additions & 0 deletions src/addons/mod/chat/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// (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.

export const ADDON_MOD_CHAT_COMPONENT = 'mmaModChat';

// Routing.
export const ADDON_MOD_CHAT_PAGE_NAME = 'mod_chat';

// Handlers.
export const ADDON_MOD_CHAT_PREFETCH_NAME = 'AddonModChat';
export const ADDON_MOD_CHAT_PREFETCH_MODNAME = 'chat';
export const ADDON_MOD_CHAT_PREFETCH_COMPONENT = ADDON_MOD_CHAT_PAGE_NAME;
6 changes: 4 additions & 2 deletions src/addons/mod/chat/pages/chat/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import { NgZone, Translate } from '@singletons';
import { CoreEvents } from '@singletons/events';
import { Subscription } from 'rxjs';
import { AddonModChatUsersModalComponent, AddonModChatUsersModalResult } from '../../components/users-modal/users-modal';
import { AddonModChat, AddonModChatProvider, AddonModChatUser } from '../../services/chat';
import { AddonModChat, AddonModChatUser } from '../../services/chat';
import { AddonModChatFormattedMessage, AddonModChatHelper } from '../../services/chat-helper';
import { CoreTime } from '@singletons/time';
import { CoreAnalytics, CoreAnalyticsEventType } from '@services/analytics';
Expand All @@ -41,6 +41,8 @@ import { CoreAnalytics, CoreAnalyticsEventType } from '@services/analytics';
})
export class AddonModChatChatPage implements OnInit, OnDestroy, CanLeave {

protected static readonly POLL_INTERVAL = 4000;

@ViewChild(IonContent) content?: IonContent;
@ViewChild(CoreSendMessageFormComponent) sendMessageForm?: CoreSendMessageFormComponent;

Expand Down Expand Up @@ -252,7 +254,7 @@ export class AddonModChatChatPage implements OnInit, OnDestroy, CanLeave {
// Start polling.
this.polling = window.setInterval(() => {
CoreUtils.ignoreErrors(this.fetchMessagesInterval());
}, AddonModChatProvider.POLL_INTERVAL);
}, AddonModChatChatPage.POLL_INTERVAL);
}

/**
Expand Down
24 changes: 11 additions & 13 deletions src/addons/mod/chat/services/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,15 @@ import { CoreUser } from '@features/user/services/user';
import { CoreSites, CoreSitesCommonWSOptions, CoreSitesReadingStrategy } from '@services/sites';
import { CoreWSExternalFile, CoreWSExternalWarning } from '@services/ws';
import { makeSingleton, Translate } from '@singletons';

const ROOT_CACHE_KEY = 'AddonModChat:';
import { ADDON_MOD_CHAT_COMPONENT } from '../constants';

/**
* Service that provides some features for chats.
*/
@Injectable({ providedIn: 'root' })
export class AddonModChatProvider {

static readonly COMPONENT = 'mmaModChat';
static readonly POLL_INTERVAL = 4000;
protected static readonly ROOT_CACHE_KEY = 'AddonModChat:';

/**
* Get a chat.
Expand All @@ -51,7 +49,7 @@ export class AddonModChatProvider {
const preSets: CoreSiteWSPreSets = {
cacheKey: this.getChatsCacheKey(courseId),
updateFrequency: CoreSite.FREQUENCY_RARELY,
component: AddonModChatProvider.COMPONENT,
component: ADDON_MOD_CHAT_COMPONENT,
...CoreSites.getReadingStrategyPreSets(options.readingStrategy), // Include reading strategy preSets.
};

Expand Down Expand Up @@ -99,7 +97,7 @@ export class AddonModChatProvider {
await CoreCourseLogHelper.log(
'mod_chat_view_chat',
params,
AddonModChatProvider.COMPONENT,
ADDON_MOD_CHAT_COMPONENT,
id,
siteId,
);
Expand All @@ -126,7 +124,7 @@ export class AddonModChatProvider {
await CoreCourseLogHelper.log(
'mod_chat_view_sessions',
params,
AddonModChatProvider.COMPONENT,
ADDON_MOD_CHAT_COMPONENT,
id,
);
}
Expand Down Expand Up @@ -232,7 +230,7 @@ export class AddonModChatProvider {
chatsid: sessionId,
};
const preSets: CoreSiteWSPreSets = {
component: AddonModChatProvider.COMPONENT,
component: ADDON_MOD_CHAT_COMPONENT,
componentId: options.cmId,
...CoreSites.getReadingStrategyPreSets(options.readingStrategy), // Include reading strategy preSets.
};
Expand Down Expand Up @@ -265,7 +263,7 @@ export class AddonModChatProvider {
const preSets: CoreSiteWSPreSets = {
cacheKey: this.getSessionsCacheKey(chatId, groupId, showAll),
updateFrequency: CoreSite.FREQUENCY_SOMETIMES,
component: AddonModChatProvider.COMPONENT,
component: ADDON_MOD_CHAT_COMPONENT,
componentId: options.cmId,
...CoreSites.getReadingStrategyPreSets(options.readingStrategy), // Include reading strategy preSets.
};
Expand Down Expand Up @@ -303,7 +301,7 @@ export class AddonModChatProvider {
const preSets: CoreSiteWSPreSets = {
cacheKey: this.getSessionMessagesCacheKey(chatId, sessionStart, groupId),
updateFrequency: CoreSite.FREQUENCY_RARELY,
component: AddonModChatProvider.COMPONENT,
component: ADDON_MOD_CHAT_COMPONENT,
componentId: options.cmId,
...CoreSites.getReadingStrategyPreSets(options.readingStrategy), // Include reading strategy preSets.
};
Expand Down Expand Up @@ -393,7 +391,7 @@ export class AddonModChatProvider {
* @returns Cache key.
*/
protected getChatsCacheKey(courseId: number): string {
return ROOT_CACHE_KEY + 'chats:' + courseId;
return AddonModChatProvider.ROOT_CACHE_KEY + 'chats:' + courseId;
}

/**
Expand All @@ -415,7 +413,7 @@ export class AddonModChatProvider {
* @returns Cache key prefix.
*/
protected getSessionsCacheKeyPrefix(chatId: number): string {
return ROOT_CACHE_KEY + 'sessions:' + chatId + ':';
return AddonModChatProvider.ROOT_CACHE_KEY + 'sessions:' + chatId + ':';
}

/**
Expand All @@ -437,7 +435,7 @@ export class AddonModChatProvider {
* @returns Cache key prefix.
*/
protected getSessionMessagesCacheKeyPrefix(chatId: number): string {
return ROOT_CACHE_KEY + 'sessionsMessages:' + chatId + ':';
return AddonModChatProvider.ROOT_CACHE_KEY + 'sessionsMessages:' + chatId + ':';
}

}
Expand Down
8 changes: 4 additions & 4 deletions src/addons/mod/chat/services/handlers/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,17 @@ import { Injectable, Type } from '@angular/core';
import { CoreModuleHandlerBase } from '@features/course/classes/module-base-handler';
import { CoreCourseModuleHandler } from '@features/course/services/module-delegate';
import { makeSingleton } from '@singletons';
import { AddonModChatIndexComponent } from '../../components/index';
import { ADDON_MOD_CHAT_PAGE_NAME } from '../../constants';

/**
* Handler to support chat modules.
*/
@Injectable({ providedIn: 'root' })
export class AddonModChatModuleHandlerService extends CoreModuleHandlerBase implements CoreCourseModuleHandler {

static readonly PAGE_NAME = 'mod_chat';

name = 'AddonModChat';
modName = 'chat';
protected pageName = AddonModChatModuleHandlerService.PAGE_NAME;
protected pageName = ADDON_MOD_CHAT_PAGE_NAME;

supportedFeatures = {
[CoreConstants.FEATURE_GROUPS]: true,
Expand All @@ -47,6 +45,8 @@ export class AddonModChatModuleHandlerService extends CoreModuleHandlerBase impl
* @inheritdoc
*/
async getMainComponent(): Promise<Type<unknown>> {
const { AddonModChatIndexComponent } = await import('@addons/mod/chat/components/index');

return AddonModChatIndexComponent;
}

Expand Down
Loading

0 comments on commit 02edd58

Please sign in to comment.