From fd8d433f0588c452685dd3da7148f36b3abaca97 Mon Sep 17 00:00:00 2001 From: Dani Palou Date: Wed, 29 Sep 2021 11:32:09 +0200 Subject: [PATCH] MOBILE-3873 bbb: Allow both inapp support and site plugin support --- .../services/handlers/module.ts | 27 ++++++++++++++- .../components/module-index/module-index.ts | 6 +++- .../services/siteplugins-helper.ts | 8 +++-- .../siteplugins/services/siteplugins.ts | 34 ++++++++++++++++--- 4 files changed, 66 insertions(+), 9 deletions(-) diff --git a/src/addons/mod/bigbluebuttonbn/services/handlers/module.ts b/src/addons/mod/bigbluebuttonbn/services/handlers/module.ts index b3febceb6..4863be94e 100644 --- a/src/addons/mod/bigbluebuttonbn/services/handlers/module.ts +++ b/src/addons/mod/bigbluebuttonbn/services/handlers/module.ts @@ -17,6 +17,8 @@ import { Injectable, Type } from '@angular/core'; import { CoreModuleHandlerBase } from '@features/course/classes/module-base-handler'; import { CoreCourseAnyModuleData } from '@features/course/services/course'; import { CoreCourseModuleHandler, CoreCourseModuleHandlerData } from '@features/course/services/module-delegate'; +import { CoreSitePluginsModuleHandler } from '@features/siteplugins/classes/handlers/module-handler'; +import { CoreSitePlugins } from '@features/siteplugins/services/siteplugins'; import { makeSingleton } from '@singletons'; import { AddonModBBBIndexComponent } from '../../components/index'; import { AddonModBBB } from '../bigbluebuttonbn'; @@ -32,6 +34,7 @@ export class AddonModBBBModuleHandlerService extends CoreModuleHandlerBase imple name = 'AddonModBBB'; modName = 'bigbluebuttonbn'; protected pageName = ADDON_MOD_BBB_MAIN_MENU_PAGE_NAME; + protected sitePluginHandler?: CoreSitePluginsModuleHandler; supportedFeatures = { [CoreConstants.FEATURE_GROUPS]: true, @@ -48,7 +51,21 @@ export class AddonModBBBModuleHandlerService extends CoreModuleHandlerBase imple * @inheritdoc */ async isEnabled(): Promise { - return AddonModBBB.isPluginEnabled(); + const enabled = await AddonModBBB.isPluginEnabled(); + + if (enabled) { + delete this.sitePluginHandler; + this.name = 'AddonModBBB'; + + return true; + } + + // Native support not available in this site. Check if it's supported by site plugin. + this.sitePluginHandler = CoreSitePlugins.getModuleHandlerInstance(this.modName); + // Change the handler name to be able to retrieve the plugin data in component. + this.name = this.sitePluginHandler?.name || this.name; + + return !!this.sitePluginHandler; } /** @@ -60,6 +77,10 @@ export class AddonModBBBModuleHandlerService extends CoreModuleHandlerBase imple sectionId?: number, forCoursePage?: boolean, ): CoreCourseModuleHandlerData { + if (this.sitePluginHandler) { + return this.sitePluginHandler.getData(module, courseId, sectionId, forCoursePage); + } + const data = super.getData(module, courseId, sectionId, forCoursePage); data.showDownloadButton = false; @@ -71,6 +92,10 @@ export class AddonModBBBModuleHandlerService extends CoreModuleHandlerBase imple * @inheritdoc */ async getMainComponent(): Promise> { + if (this.sitePluginHandler) { + return this.sitePluginHandler.getMainComponent(); + } + return AddonModBBBIndexComponent; } diff --git a/src/core/features/siteplugins/components/module-index/module-index.ts b/src/core/features/siteplugins/components/module-index/module-index.ts index 5e83cbc9d..499458d36 100644 --- a/src/core/features/siteplugins/components/module-index/module-index.ts +++ b/src/core/features/siteplugins/components/module-index/module-index.ts @@ -161,7 +161,11 @@ export class CoreSitePluginsModuleIndexComponent implements OnInit, OnDestroy, C * Expand the description. */ expandDescription(): void { - CoreTextUtils.viewText(Translate.instant('core.description'), this.description!, { + if (!this.description) { + return; + } + + CoreTextUtils.viewText(Translate.instant('core.description'), this.description, { component: this.component, componentId: this.module.id, filter: true, diff --git a/src/core/features/siteplugins/services/siteplugins-helper.ts b/src/core/features/siteplugins/services/siteplugins-helper.ts index 3ce97bba9..2d674dac2 100644 --- a/src/core/features/siteplugins/services/siteplugins-helper.ts +++ b/src/core/features/siteplugins/services/siteplugins-helper.ts @@ -205,7 +205,7 @@ export class CoreSitePluginsHelperProvider { undefined, undefined, undefined, - handlerSchema.styles!.version, + handlerSchema.styles?.version, ); // File is downloaded, get the contents. @@ -378,8 +378,9 @@ export class CoreSitePluginsHelperProvider { if (plugin.parsedHandlers) { // Register all the handlers. - await CoreUtils.allPromises(Object.keys(plugin.parsedHandlers).map(async (name) => { - await this.registerHandler(plugin, name, plugin.parsedHandlers![name]); + const parsedHandlers = plugin.parsedHandlers; + await CoreUtils.allPromises(Object.keys(parsedHandlers).map(async (name) => { + await this.registerHandler(plugin, name, parsedHandlers[name]); })); } } @@ -891,6 +892,7 @@ export class CoreSitePluginsHelperProvider { const moduleHandler = new CoreSitePluginsModuleHandler(uniqueName, modName, plugin, handlerSchema, initResult); CoreCourseModuleDelegate.registerHandler(moduleHandler); + CoreSitePlugins.setModuleHandlerInstance(modName, moduleHandler); if (handlerSchema.offlinefunctions && Object.keys(handlerSchema.offlinefunctions).length) { // Register the prefetch handler. diff --git a/src/core/features/siteplugins/services/siteplugins.ts b/src/core/features/siteplugins/services/siteplugins.ts index 6d7fdb702..c03ffbe91 100644 --- a/src/core/features/siteplugins/services/siteplugins.ts +++ b/src/core/features/siteplugins/services/siteplugins.ts @@ -28,6 +28,7 @@ import { CoreWSExternalFile, CoreWSExternalWarning } from '@services/ws'; import { makeSingleton } from '@singletons'; import { CoreEvents } from '@singletons/events'; import { CoreLogger } from '@singletons/logger'; +import { CoreSitePluginsModuleHandler } from '../classes/handlers/module-handler'; const ROOT_CACHE_KEY = 'CoreSitePlugins:'; @@ -44,6 +45,8 @@ export class CoreSitePluginsProvider { protected sitePlugins: {[name: string]: CoreSitePluginsHandler} = {}; // Site plugins registered. protected sitePluginPromises: {[name: string]: Promise} = {}; // Promises of loading plugins. protected fetchPluginsDeferred: PromiseDefer; + protected moduleHandlerInstances: Record = {}; + hasSitePluginsLoaded = false; sitePluginsFinishedLoading = false; @@ -262,7 +265,7 @@ export class CoreSitePluginsProvider { switch (paramName) { case 'courseids': // The WS needs the list of course IDs. Create the list. - return [courseId!]; + return [courseId || 0]; case component + 'id': // The WS needs the instance id. @@ -540,10 +543,13 @@ export class CoreSitePluginsProvider { return; } + const siteInstance = site; + const offlineFunctions = handlerSchema.offlinefunctions; + await Promise.all(Object.keys(handlerSchema.offlinefunctions).map(async(method) => { - if (site!.wsAvailable(method)) { + if (siteInstance.wsAvailable(method)) { // The method is a WS. - const paramsList = handlerSchema.offlinefunctions![method]; + const paramsList = offlineFunctions[method]; const cacheKey = this.getCallWSCacheKey(method, args); let params: Record = {}; @@ -584,7 +590,7 @@ export class CoreSitePluginsProvider { // Prefetch the files in the content. if (result.files.length) { await CoreFilepool.downloadOrPrefetchFiles( - site!.getId(), + siteInstance.getId(), result.files, !!prefetch, false, @@ -657,6 +663,26 @@ export class CoreSitePluginsProvider { return this.fetchPluginsDeferred.promise; } + /** + * Get a module hander instance, if present. + * + * @param modName Mod name without "mod_". + * @return Handler instance, undefined if not found. + */ + getModuleHandlerInstance(modName: string): CoreSitePluginsModuleHandler | undefined { + return this.moduleHandlerInstances[modName]; + } + + /** + * Set a module hander instance. + * + * @param modName Mod name. + * @param handler Handler instance. + */ + setModuleHandlerInstance(modName: string, handler: CoreSitePluginsModuleHandler): void { + this.moduleHandlerInstances[modName] = handler; + } + } export const CoreSitePlugins = makeSingleton(CoreSitePluginsProvider);