From 82419c3f5051b087209c169ffa82f25c1f2bcd25 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pau=20Ferrer=20Oca=C3=B1a?= Date: Wed, 23 Feb 2022 09:49:06 +0100 Subject: [PATCH] MOBILE-3811 resource: Remove mimetype icons from resource only on 4.0+ This partially reverts commit 96b172c42ca216aacc986502eb628e5a911d0671. --- .../mod/forum/services/handlers/module.ts | 9 +++-- .../mod/resource/services/handlers/module.ts | 40 ++++++++++++++++++- .../course/services/module-delegate.ts | 3 +- 3 files changed, 46 insertions(+), 6 deletions(-) diff --git a/src/addons/mod/forum/services/handlers/module.ts b/src/addons/mod/forum/services/handlers/module.ts index a750b83b6..780f432b1 100644 --- a/src/addons/mod/forum/services/handlers/module.ts +++ b/src/addons/mod/forum/services/handlers/module.ts @@ -23,6 +23,7 @@ import { CoreConstants, ModPurpose } from '@/core/constants'; import { AddonModForumIndexComponent } from '../../components/index'; import { CoreModuleHandlerBase } from '@features/course/classes/module-base-handler'; import { CoreCourseModuleData } from '@features/course/services/course-helper'; +import { CoreIonicColorNames } from '@singletons/colors'; /** * Handler to support forum modules. @@ -58,7 +59,7 @@ export class AddonModForumModuleHandlerService extends CoreModuleHandlerBase imp const data = await super.getData(module, courseId); if ('afterlink' in module && !!module.afterlink) { - data.extraBadgeColor = ''; + data.extraBadgeColor = undefined; const match = />(\d+)[^<]+/.exec(module.afterlink); data.extraBadge = match ? Translate.instant('addon.mod_forum.unreadpostsnumber', { $a : match[1] }) : ''; } else { @@ -112,7 +113,7 @@ export class AddonModForumModuleHandlerService extends CoreModuleHandlerBase imp } data.extraBadge = Translate.instant('core.loading'); - data.extraBadgeColor = 'light'; + data.extraBadgeColor = CoreIonicColorNames.DARK; await CoreUtils.ignoreErrors(AddonModForum.invalidateForumData(courseId)); @@ -120,7 +121,7 @@ export class AddonModForumModuleHandlerService extends CoreModuleHandlerBase imp // Handle unread posts. const forum = await AddonModForum.getForum(courseId, moduleId, { siteId }); - data.extraBadgeColor = ''; + data.extraBadgeColor = undefined; data.extraBadge = forum.unreadpostscount ? Translate.instant( 'addon.mod_forum.unreadpostsnumber', @@ -129,7 +130,7 @@ export class AddonModForumModuleHandlerService extends CoreModuleHandlerBase imp : ''; } catch { // Ignore errors. - data.extraBadgeColor = ''; + data.extraBadgeColor = undefined; data.extraBadge = ''; } } diff --git a/src/addons/mod/resource/services/handlers/module.ts b/src/addons/mod/resource/services/handlers/module.ts index 0eaab6861..3304a1b8c 100644 --- a/src/addons/mod/resource/services/handlers/module.ts +++ b/src/addons/mod/resource/services/handlers/module.ts @@ -20,6 +20,7 @@ import { CoreCourseModuleData } from '@features/course/services/course-helper'; import { CoreCourseModuleHandler, CoreCourseModuleHandlerData } from '@features/course/services/module-delegate'; import { CoreCourseModulePrefetchDelegate } from '@features/course/services/module-prefetch-delegate'; import { CoreFileHelper } from '@services/file-helper'; +import { CoreSites } from '@services/sites'; import { CoreMimetypeUtils } from '@services/utils/mimetype'; import { CoreTextUtils } from '@services/utils/text'; import { CoreTimeUtils } from '@services/utils/time'; @@ -95,7 +96,14 @@ export class AddonModResourceModuleHandlerService extends CoreModuleHandlerBase this.getResourceData(module, courseId, handlerData).then((extra) => { handlerData.extraBadge = extra; - handlerData.extraBadgeColor = ''; + + return; + }).catch(() => { + // Ignore errors. + }); + + this.getIconSrc(module).then((icon) => { + handlerData.icon = icon; return; }).catch(() => { @@ -214,6 +222,36 @@ export class AddonModResourceModuleHandlerService extends CoreModuleHandlerBase return extra.join(' '); } + /** + * @inheritdoc + */ + async getIconSrc(module?: CoreCourseModuleData): Promise { + if (!module) { + return; + } + + if (CoreSites.getCurrentSite()?.isVersionGreaterEqualThan('4.0')) { + return await CoreCourse.getModuleIconSrc(module.modname, module.modicon); + } + let mimetypeIcon = ''; + + if (module.contentsinfo) { + // No need to use the list of files. + const mimetype = module.contentsinfo.mimetypes[0]; + if (mimetype) { + mimetypeIcon = CoreMimetypeUtils.getMimetypeIcon(mimetype); + } + + } else if (module.contents && module.contents[0]) { + const files = module.contents; + const file = files[0]; + + mimetypeIcon = CoreMimetypeUtils.getFileIcon(file.filename || ''); + } + + return await CoreCourse.getModuleIconSrc(module.modname, module.modicon, mimetypeIcon); + } + /** * @inheritdoc */ diff --git a/src/core/features/course/services/module-delegate.ts b/src/core/features/course/services/module-delegate.ts index 6c9a73055..ba49c4c72 100644 --- a/src/core/features/course/services/module-delegate.ts +++ b/src/core/features/course/services/module-delegate.ts @@ -25,6 +25,7 @@ import { CoreSites } from '@services/sites'; import { makeSingleton } from '@singletons'; import { CoreCourseModuleData } from './course-helper'; import { CoreNavigationOptions } from '@services/navigator'; +import { CoreIonicColorNames } from '@singletons/colors'; /** * Interface that all course module handlers must implement. @@ -146,7 +147,7 @@ export interface CoreCourseModuleHandlerData { /** * The color of the extra badge. Default: primary. */ - extraBadgeColor?: string; + extraBadgeColor?: CoreIonicColorNames; /** * Whether to display a button to download/refresh the module if it's downloadable.