From 8a3ae5e081c0651ebf48d6b0f2fb6e6646aefa3f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pau=20Ferrer=20Oca=C3=B1a?= Date: Tue, 16 Nov 2021 14:10:43 +0100 Subject: [PATCH] MOBILE-3810 module: Fix getIconSrc on course module handler --- .../mod/resource/services/handlers/module.ts | 28 ++++++++++++++++++- src/core/components/mod-icon/mod-icon.ts | 4 +-- .../course/services/module-delegate.ts | 8 ++++-- 3 files changed, 34 insertions(+), 6 deletions(-) diff --git a/src/addons/mod/resource/services/handlers/module.ts b/src/addons/mod/resource/services/handlers/module.ts index 69a3d2ce8..275ea61f7 100644 --- a/src/addons/mod/resource/services/handlers/module.ts +++ b/src/addons/mod/resource/services/handlers/module.ts @@ -15,7 +15,7 @@ import { CoreConstants } from '@/core/constants'; import { Injectable, Type } from '@angular/core'; import { CoreModuleHandlerBase } from '@features/course/classes/module-base-handler'; -import { CoreCourse } from '@features/course/services/course'; +import { CoreCourse, CoreCourseWSModule } from '@features/course/services/course'; import { CoreCourseModule } from '@features/course/services/course-helper'; import { CoreCourseModuleHandler, CoreCourseModuleHandlerData } from '@features/course/services/module-delegate'; import { CoreCourseModulePrefetchDelegate } from '@features/course/services/module-prefetch-delegate'; @@ -226,6 +226,32 @@ export class AddonModResourceModuleHandlerService extends CoreModuleHandlerBase }; } + /** + * @inheritdoc + */ + async getIconSrc(module?: CoreCourseWSModule): Promise { + if (!module) { + return; + } + let mimetypeIcon = ''; + + if ('contentsinfo' in module && 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/components/mod-icon/mod-icon.ts b/src/core/components/mod-icon/mod-icon.ts index eba805215..c9c6b70ac 100644 --- a/src/core/components/mod-icon/mod-icon.ts +++ b/src/core/components/mod-icon/mod-icon.ts @@ -15,7 +15,7 @@ import { Component, Input, OnChanges, OnInit, SimpleChange } from '@angular/core'; import { CoreCourse } from '@features/course/services/course'; -const assetsPath = 'assets/img/mod/'; +const assetsPath = 'assets/img/'; const fallbackModName = 'external-tool'; /** @@ -51,7 +51,7 @@ export class CoreModIconComponent implements OnInit, OnChanges { * @inheritdoc */ ngOnChanges(changes: { [name: string]: SimpleChange }): void { - if (changes && changes.modicon && changes.modicon.previousValue) { + if (changes && changes.modicon && changes.modicon.previousValue !== undefined) { this.setIcon(); } } diff --git a/src/core/features/course/services/module-delegate.ts b/src/core/features/course/services/module-delegate.ts index f967c89a5..e5f918d1f 100644 --- a/src/core/features/course/services/module-delegate.ts +++ b/src/core/features/course/services/module-delegate.ts @@ -80,9 +80,10 @@ export interface CoreCourseModuleHandler extends CoreDelegateHandler { /** * Get the icon src for the module. * + * @param module: Module to get the icon from. * @return The icon src. */ - getIconSrc?(module: CoreCourseWSModule): Promise | string | undefined; + getIconSrc?(module?: CoreCourseWSModule): Promise | string | undefined; /** * Check if this type of module supports a certain feature. @@ -343,10 +344,11 @@ export class CoreCourseModuleDelegateService extends CoreDelegate { - const icon = await this.executeFunctionOnEnabled>(modname, 'getIconSrc'); + async getModuleIconSrc(modname: string, modicon?: string, module?: CoreCourseWSModule): Promise { + const icon = await this.executeFunctionOnEnabled>(modname, 'getIconSrc', [module]); return icon || await CoreCourse.getModuleIconSrc(modname, modicon) || ''; }