Skip to content

Commit

Permalink
changes
Browse files Browse the repository at this point in the history
  • Loading branch information
crazyserver committed Oct 3, 2024
1 parent c007e82 commit a5598e4
Show file tree
Hide file tree
Showing 7 changed files with 207 additions and 202 deletions.
56 changes: 51 additions & 5 deletions src/addons/mod/subsection/services/handlers/prefetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@

import { Injectable } from '@angular/core';
import { CoreCourseResourcePrefetchHandlerBase } from '@features/course/classes/resource-prefetch-handler';
import { CoreCourse } from '@features/course/services/course';
import { CoreCourse, CoreCourseAnyModuleData, CoreCourseWSSection } from '@features/course/services/course';
import { makeSingleton } from '@singletons';
import { ADDON_MOD_SUBSECTION_COMPONENT } from '../../constants';
import { CoreCourseHelper, CoreCourseModuleData } from '@features/course/services/course-helper';
import { CoreFileSizeSum } from '@services/plugin-file-delegate';
import { CoreCourseModulePrefetchDelegate } from '@features/course/services/module-prefetch-delegate';
import { CoreSites } from '@services/sites';

/**
* Handler to prefetch subsections.
Expand All @@ -37,16 +40,59 @@ export class AddonModSubsectionPrefetchHandlerService extends CoreCourseResource
module: CoreCourseModuleData,
courseId: number,
): Promise<void> {
const sections = await CoreCourse.getSections(courseId, false, true, undefined, siteId);
const section = sections.find((section) =>
section.component === 'mod_subsection' && section.itemid === module.instance);

const section = await this.getSection(module, courseId, siteId);
if (!section) {
return;
}

await CoreCourseHelper.prefetchSections([section], courseId);
}

/**
* @inheritdoc
*/
async getDownloadSize(module: CoreCourseAnyModuleData, courseId: number): Promise<CoreFileSizeSum> {
const section = await this.getSection(module, courseId);
if (!section) {
return { size: 0, total: true };
}

return await CoreCourseModulePrefetchDelegate.getDownloadSize(section.modules, courseId);
}

/**
* @inheritdoc
*/
async getDownloadedSize(module: CoreCourseAnyModuleData, courseId: number): Promise<number> {
const section = await this.getSection(module, courseId);
if (!section) {
return 0;
}

return CoreCourseHelper.getModulesDownloadedSize(section.modules, courseId);

}

/**
* Get the section of a module.
*
* @param module Module.
* @param courseId Course ID.
* @param siteId Site ID. If not defined, current site.
* @returns Promise resolved with the section if found.
*/
protected async getSection(
module: CoreCourseAnyModuleData,
courseId: number,
siteId?: string,
): Promise<CoreCourseWSSection | undefined> {
siteId = siteId ?? CoreSites.getCurrentSiteId();

const sections = await CoreCourse.getSections(courseId, false, true, undefined, siteId);

return sections.find((section) =>
section.component === 'mod_subsection' && section.itemid === module.instance);
}

}
export const AddonModSubsectionPrefetchHandler = makeSingleton(AddonModSubsectionPrefetchHandlerService);
Loading

0 comments on commit a5598e4

Please sign in to comment.