diff --git a/src/addons/storagemanager/pages/course-storage/course-storage.html b/src/addons/storagemanager/pages/course-storage/course-storage.html index b36d1e85d..57b45f33c 100644 --- a/src/addons/storagemanager/pages/course-storage/course-storage.html +++ b/src/addons/storagemanager/pages/course-storage/course-storage.html @@ -25,8 +25,8 @@

{{ 'addon.storagemanager.totaldownloads' | translate }}

- {{ totalSize | coreBytesToSize }} - {{ 'core.calculating' | translate }} + {{ totalSize | coreBytesToSize }} + {{ 'core.calculating' | translate }} {{ prefetchCourseData.statusTranslatable | translate }} - @@ -55,12 +55,12 @@

+ *ngIf="!section.calculatingSize && section.totalSize > 0"> {{ section.totalSize | coreBytesToSize }} - + {{ 'core.calculating' | translate }} @@ -69,7 +69,8 @@

-
+
- @@ -95,7 +96,7 @@ + *ngIf="downloadEnabled || (!module.calculatingSize && module.totalSize > 0)"> @@ -106,12 +107,12 @@ + *ngIf="!module.calculatingSize && module.totalSize > 0"> {{ module.totalSize | coreBytesToSize }} - + {{ 'core.calculating' | translate }} @@ -123,7 +124,7 @@ (action)="prefetchModule(module, section)"> + *ngIf="!module.calculatingSize && module.totalSize > 0" color="danger"> diff --git a/src/addons/storagemanager/pages/course-storage/course-storage.ts b/src/addons/storagemanager/pages/course-storage/course-storage.ts index 5c3f371cb..39c3d42d0 100644 --- a/src/addons/storagemanager/pages/course-storage/course-storage.ts +++ b/src/addons/storagemanager/pages/course-storage/course-storage.ts @@ -48,7 +48,7 @@ export class AddonStorageManagerCourseStoragePage implements OnInit, OnDestroy { loaded = false; sections: AddonStorageManagerCourseSection[] = []; totalSize = 0; - sizeLoaded = false; + calculatingSize = true; downloadEnabled = false; downloadCourseEnabled = false; @@ -106,12 +106,20 @@ export class AddonStorageManagerCourseStoragePage implements OnInit, OnDestroy { const sections = await CoreCourse.getSections(this.courseId, false, true); this.sections = (await CoreCourseHelper.addHandlerDataForModules(sections, this.courseId)).sections - .map((section) => ({ ...section, totalSize: 0 })); + .map(section => ({ + ...section, + totalSize: 0, + calculatingSize: true, + modules: section.modules.map(module => ({ + ...module, + calculatingSize: true, + })), + })); this.loaded = true; await Promise.all([ - this.loadSizes(), + this.initSizes(), this.initCoursePrefetch(), this.initModulePrefetch(), ]); @@ -240,18 +248,9 @@ export class AddonStorageManagerCourseStoragePage implements OnInit, OnDestroy { /** * Init section, course and modules sizes. */ - protected async loadSizes(): Promise { - this.totalSize = 0; - this.sizeLoaded = false; - + protected async initSizes(): Promise { await Promise.all(this.sections.map(async (section) => { - section.totalSize = 0; - section.sizeLoaded = false; - await Promise.all(section.modules.map(async (module) => { - module.totalSize = 0; - module.sizeLoaded = false; - // Note: This function only gets the size for modules which are downloadable. // For other modules it always returns 0, even if they have downloaded some files. // However there is no 100% reliable way to actually track the files in this case. @@ -268,13 +267,13 @@ export class AddonStorageManagerCourseStoragePage implements OnInit, OnDestroy { this.totalSize += size; } - module.sizeLoaded = true; + module.calculatingSize = false; })); - section.sizeLoaded = true; + section.calculatingSize = false; })); - this.sizeLoaded = true; + this.calculatingSize = false; // Mark course as not downloaded if course size is 0. if (this.totalSize == 0) { @@ -282,6 +281,56 @@ export class AddonStorageManagerCourseStoragePage implements OnInit, OnDestroy { } } + /** + * Update the sizes of some modules. + * + * @param modules Modules. + * @param section Section the modules belong to. + * @return Promise resolved when done. + */ + protected async updateModulesSizes( + modules: AddonStorageManagerModule[], + section?: AddonStorageManagerCourseSection, + ): Promise { + this.calculatingSize = true; + + await Promise.all(modules.map(async (module) => { + if (module.calculatingSize) { + return; + } + + module.calculatingSize = true; + + if (!section) { + section = this.sections.find((section) => section.modules.some((mod) => mod.id === module.id)); + if (section) { + section.calculatingSize = true; + } + } + + try { + const size = await CoreCourseModulePrefetchDelegate.getModuleStoredSize(module, this.courseId); + + const diff = (isNaN(size) ? 0 : size) - (module.totalSize ?? 0); + + module.totalSize = Number(size); + this.totalSize += diff; + if (section) { + section.totalSize += diff; + } + } catch { + // Ignore errors, it shouldn't happen. + } finally { + module.calculatingSize = false; + } + })); + + this.calculatingSize = false; + if (section) { + section.calculatingSize = false; + } + } + /** * The user has requested a delete for the whole course data. * @@ -406,7 +455,7 @@ export class AddonStorageManagerCourseStoragePage implements OnInit, OnDestroy { } finally { modal.dismiss(); - await this.loadSizes(); + await this.updateModulesSizes(modules, section); CoreCourseHelper.calculateSectionsStatus(this.sections, this.courseId, false, false); } } @@ -455,7 +504,7 @@ export class AddonStorageManagerCourseStoragePage implements OnInit, OnDestroy { CoreDomUtils.showErrorModalDefault(error, 'core.course.errordownloadingsection', true); } } finally { - await this.loadSizes(); + await this.updateModulesSizes(section.modules, section); } } catch (error) { // User cancelled or there was an error calculating the size. @@ -501,7 +550,7 @@ export class AddonStorageManagerCourseStoragePage implements OnInit, OnDestroy { } finally { module.spinner = false; - await this.loadSizes(); + await this.updateModulesSizes([module]); } } @@ -613,13 +662,13 @@ export class AddonStorageManagerCourseStoragePage implements OnInit, OnDestroy { type AddonStorageManagerCourseSection = Omit & { totalSize: number; - sizeLoaded?: boolean; + calculatingSize: boolean; modules: AddonStorageManagerModule[]; }; type AddonStorageManagerModule = CoreCourseModuleData & { totalSize?: number; - sizeLoaded?: boolean; + calculatingSize: boolean; prefetchHandler?: CoreCourseModulePrefetchHandler; spinner?: boolean; downloadStatus?: string;