From b93dcfb87fd7b47dac076f5b9187e56ef7cce283 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pau=20Ferrer=20Oca=C3=B1a?= Date: Thu, 11 Jul 2024 22:44:06 +0200 Subject: [PATCH] MOBILE-4616 resource: Move resource constants to a file --- .../mod/resource/components/index/index.ts | 4 ++-- src/addons/mod/resource/constants.ts | 17 +++++++++++++++++ src/addons/mod/resource/resource-lazy.module.ts | 2 +- src/addons/mod/resource/resource.module.ts | 7 ++++--- .../mod/resource/services/handlers/module.ts | 5 ++--- .../mod/resource/services/handlers/prefetch.ts | 5 +++-- .../mod/resource/services/resource-helper.ts | 7 ++++--- src/addons/mod/resource/services/resource.ts | 13 ++++++------- 8 files changed, 39 insertions(+), 21 deletions(-) create mode 100644 src/addons/mod/resource/constants.ts diff --git a/src/addons/mod/resource/components/index/index.ts b/src/addons/mod/resource/components/index/index.ts index 48a589d75..46e30f194 100644 --- a/src/addons/mod/resource/components/index/index.ts +++ b/src/addons/mod/resource/components/index/index.ts @@ -31,10 +31,10 @@ import { Subscription } from 'rxjs'; import { AddonModResource, AddonModResourceCustomData, - AddonModResourceProvider, } from '../../services/resource'; import { AddonModResourceHelper } from '../../services/resource-helper'; import { CorePlatform } from '@services/platform'; +import { ADDON_MOD_RESOURCE_COMPONENT } from '../../constants'; /** * Component that displays a resource. @@ -46,7 +46,7 @@ import { CorePlatform } from '@services/platform'; }) export class AddonModResourceIndexComponent extends CoreCourseModuleMainResourceComponent implements OnInit, OnDestroy { - component = AddonModResourceProvider.COMPONENT; + component = ADDON_MOD_RESOURCE_COMPONENT; pluginName = 'resource'; mode = ''; diff --git a/src/addons/mod/resource/constants.ts b/src/addons/mod/resource/constants.ts new file mode 100644 index 000000000..336a35fea --- /dev/null +++ b/src/addons/mod/resource/constants.ts @@ -0,0 +1,17 @@ +// (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_RESOURCE_COMPONENT = 'mmaModResource'; + +export const ADDON_MOD_RESOURCE_PAGE_NAME = 'mod_resource'; diff --git a/src/addons/mod/resource/resource-lazy.module.ts b/src/addons/mod/resource/resource-lazy.module.ts index d04baeef9..6da403f7e 100644 --- a/src/addons/mod/resource/resource-lazy.module.ts +++ b/src/addons/mod/resource/resource-lazy.module.ts @@ -35,4 +35,4 @@ const routes: Routes = [ AddonModResourceIndexPage, ], }) -export class AddonModResourceLazyModule {} +export default class AddonModResourceLazyModule {} diff --git a/src/addons/mod/resource/resource.module.ts b/src/addons/mod/resource/resource.module.ts index d27d47f95..c0eebd30b 100644 --- a/src/addons/mod/resource/resource.module.ts +++ b/src/addons/mod/resource/resource.module.ts @@ -21,14 +21,15 @@ import { CoreMainMenuTabRoutingModule } from '@features/mainmenu/mainmenu-tab-ro import { CorePluginFileDelegate } from '@services/plugin-file-delegate'; import { AddonModResourceIndexLinkHandler } from './services/handlers/index-link'; import { AddonModResourceListLinkHandler } from './services/handlers/list-link'; -import { AddonModResourceModuleHandlerService, AddonModResourceModuleHandler } from './services/handlers/module'; +import { AddonModResourceModuleHandler } from './services/handlers/module'; import { AddonModResourcePluginFileHandler } from './services/handlers/pluginfile'; import { AddonModResourcePrefetchHandler } from './services/handlers/prefetch'; +import { ADDON_MOD_RESOURCE_PAGE_NAME } from './constants'; const routes: Routes = [ { - path: AddonModResourceModuleHandlerService.PAGE_NAME, - loadChildren: () => import('./resource-lazy.module').then(m => m.AddonModResourceLazyModule), + path: ADDON_MOD_RESOURCE_PAGE_NAME, + loadChildren: () => import('./resource-lazy.module'), }, ]; diff --git a/src/addons/mod/resource/services/handlers/module.ts b/src/addons/mod/resource/services/handlers/module.ts index 5c2c2a27b..f94293609 100644 --- a/src/addons/mod/resource/services/handlers/module.ts +++ b/src/addons/mod/resource/services/handlers/module.ts @@ -25,6 +25,7 @@ import { makeSingleton, Translate } from '@singletons'; import { AddonModResource } from '../resource'; import { AddonModResourceHelper } from '../resource-helper'; import { CoreUtils } from '@services/utils/utils'; +import { ADDON_MOD_RESOURCE_PAGE_NAME } from '../../constants'; /** * Handler to support resource modules. @@ -32,11 +33,9 @@ import { CoreUtils } from '@services/utils/utils'; @Injectable({ providedIn: 'root' }) export class AddonModResourceModuleHandlerService extends CoreModuleHandlerBase implements CoreCourseModuleHandler { - static readonly PAGE_NAME = 'mod_resource'; - name = 'AddonModResource'; modName = 'resource'; - protected pageName = AddonModResourceModuleHandlerService.PAGE_NAME; + protected pageName = ADDON_MOD_RESOURCE_PAGE_NAME; supportedFeatures = { [CoreConstants.FEATURE_MOD_ARCHETYPE]: CoreConstants.MOD_ARCHETYPE_RESOURCE, diff --git a/src/addons/mod/resource/services/handlers/prefetch.ts b/src/addons/mod/resource/services/handlers/prefetch.ts index 86c588bdf..502b4fcfb 100644 --- a/src/addons/mod/resource/services/handlers/prefetch.ts +++ b/src/addons/mod/resource/services/handlers/prefetch.ts @@ -20,8 +20,9 @@ import { CoreCourseModuleData } from '@features/course/services/course-helper'; import { CoreFilepool } from '@services/filepool'; import { CoreSites } from '@services/sites'; import { makeSingleton } from '@singletons'; -import { AddonModResource, AddonModResourceProvider } from '../resource'; +import { AddonModResource } from '../resource'; import { AddonModResourceHelper } from '../resource-helper'; +import { ADDON_MOD_RESOURCE_COMPONENT } from '../../constants'; /** * Handler to prefetch resources. @@ -31,7 +32,7 @@ export class AddonModResourcePrefetchHandlerService extends CoreCourseResourcePr name = 'AddonModResource'; modName = 'resource'; - component = AddonModResourceProvider.COMPONENT; + component = ADDON_MOD_RESOURCE_COMPONENT; /** * @inheritdoc diff --git a/src/addons/mod/resource/services/resource-helper.ts b/src/addons/mod/resource/services/resource-helper.ts index c66f0cb16..539616973 100644 --- a/src/addons/mod/resource/services/resource-helper.ts +++ b/src/addons/mod/resource/services/resource-helper.ts @@ -27,10 +27,11 @@ import { CoreMimetypeUtils } from '@services/utils/mimetype'; import { CoreUtilsOpenFileOptions } from '@services/utils/utils'; import { makeSingleton, Translate } from '@singletons'; import { CorePath } from '@singletons/path'; -import { AddonModResource, AddonModResourceCustomData, AddonModResourceProvider } from './resource'; +import { AddonModResource, AddonModResourceCustomData } from './resource'; import { CoreAnalytics, CoreAnalyticsEventType } from '@services/analytics'; import { CoreTextUtils } from '@services/utils/text'; import { CoreTimeUtils } from '@services/utils/time'; +import { ADDON_MOD_RESOURCE_COMPONENT } from '../constants'; /** * Service that provides helper functions for resources. @@ -50,7 +51,7 @@ export class AddonModResourceHelperProvider { const result = await CoreCourseHelper.downloadModuleWithMainFileIfNeeded( module, module.course, - AddonModResourceProvider.COMPONENT, + ADDON_MOD_RESOURCE_COMPONENT, module.id, contents, ); @@ -196,7 +197,7 @@ export class AddonModResourceHelperProvider { await CoreCourseHelper.downloadModuleAndOpenFile( module, courseId, - AddonModResourceProvider.COMPONENT, + ADDON_MOD_RESOURCE_COMPONENT, module.id, module.contents, undefined, diff --git a/src/addons/mod/resource/services/resource.ts b/src/addons/mod/resource/services/resource.ts index 6d9c451a7..3dfaa62dc 100644 --- a/src/addons/mod/resource/services/resource.ts +++ b/src/addons/mod/resource/services/resource.ts @@ -23,8 +23,7 @@ import { CoreSites, CoreSitesCommonWSOptions } from '@services/sites'; import { CoreUtils } from '@services/utils/utils'; import { CoreWSExternalFile, CoreWSExternalWarning } from '@services/ws'; import { makeSingleton, Translate } from '@singletons'; - -const ROOT_CACHE_KEY = 'mmaModResource:'; +import { ADDON_MOD_RESOURCE_COMPONENT } from '../constants'; /** * Service that provides some features for resources. @@ -32,7 +31,7 @@ const ROOT_CACHE_KEY = 'mmaModResource:'; @Injectable({ providedIn: 'root' }) export class AddonModResourceProvider { - static readonly COMPONENT = 'mmaModResource'; + protected static readonly ROOT_CACHE_KEY = 'mmaModResource:'; /** * Get cache key for resource data WS calls. @@ -41,7 +40,7 @@ export class AddonModResourceProvider { * @returns Cache key. */ protected getResourceCacheKey(courseId: number): string { - return ROOT_CACHE_KEY + 'resource:' + courseId; + return AddonModResourceProvider.ROOT_CACHE_KEY + 'resource:' + courseId; } /** @@ -68,7 +67,7 @@ export class AddonModResourceProvider { const preSets: CoreSiteWSPreSets = { cacheKey: this.getResourceCacheKey(courseId), updateFrequency: CoreSite.FREQUENCY_RARELY, - component: AddonModResourceProvider.COMPONENT, + component: ADDON_MOD_RESOURCE_COMPONENT, ...CoreSites.getReadingStrategyPreSets(options.readingStrategy), }; @@ -112,7 +111,7 @@ export class AddonModResourceProvider { const promises: Promise[] = []; promises.push(this.invalidateResourceData(courseId, siteId)); - promises.push(CoreFilepool.invalidateFilesByComponent(siteId, AddonModResourceProvider.COMPONENT, moduleId)); + promises.push(CoreFilepool.invalidateFilesByComponent(siteId, ADDON_MOD_RESOURCE_COMPONENT, moduleId)); promises.push(CoreCourse.invalidateModule(moduleId, siteId, 'resource')); await CoreUtils.allPromises(promises); @@ -158,7 +157,7 @@ export class AddonModResourceProvider { await CoreCourseLogHelper.log( 'mod_resource_view_resource', params, - AddonModResourceProvider.COMPONENT, + ADDON_MOD_RESOURCE_COMPONENT, id, siteId, );