From 09533757a482c17e06047dce4db0d9e6c49e014c Mon Sep 17 00:00:00 2001 From: Dani Palou Date: Thu, 25 Jul 2019 13:07:34 +0200 Subject: [PATCH] MOBILE-3064 course: Don't call check_updates after prefetch --- src/core/course/components/module/module.ts | 2 +- src/core/course/pages/section/section.ts | 10 +------- src/core/course/providers/helper.ts | 16 ++++++++----- .../providers/module-prefetch-delegate.ts | 24 +++++++++++++------ 4 files changed, 29 insertions(+), 23 deletions(-) diff --git a/src/core/course/components/module/module.ts b/src/core/course/components/module/module.ts index 758784848..2cde98503 100644 --- a/src/core/course/components/module/module.ts +++ b/src/core/course/components/module/module.ts @@ -149,7 +149,7 @@ export class CoreCourseModuleComponent implements OnInit, OnDestroy { this.prefetchHandler.getDownloadSize(this.module, this.courseId, true).then((size) => { return this.courseHelper.prefetchModule(this.prefetchHandler, this.module, size, this.courseId, refresh); }).then(() => { - this.courseHelper.calculateSectionStatus(this.section, this.courseId); + this.courseHelper.calculateSectionStatus(this.section, this.courseId, false, false); }).catch((error) => { // Error, hide spinner. this.spinner = false; diff --git a/src/core/course/pages/section/section.ts b/src/core/course/pages/section/section.ts index efbd0d748..58327d93c 100644 --- a/src/core/course/pages/section/section.ts +++ b/src/core/course/pages/section/section.ts @@ -442,15 +442,7 @@ export class CoreCourseSectionPage implements OnDestroy { */ prefetchCourse(): void { this.courseHelper.confirmAndPrefetchCourse(this.prefetchCourseData, this.course, this.sections, - this.courseHandlers, this.courseMenuHandlers) - .then(() => { - if (this.downloadEnabled) { - // Recalculate the status. - this.courseHelper.calculateSectionsStatus(this.sections, this.course.id).catch(() => { - // Ignore errors (shouldn't happen). - }); - } - }).catch((error) => { + this.courseHandlers, this.courseMenuHandlers).catch((error) => { if (!this.isDestroyed) { this.domUtils.showErrorModalDefault(error, 'core.course.errordownloadingcourse', true); } diff --git a/src/core/course/providers/helper.ts b/src/core/course/providers/helper.ts index c458b999c..36738cff8 100644 --- a/src/core/course/providers/helper.ts +++ b/src/core/course/providers/helper.ts @@ -187,16 +187,19 @@ export class CoreCourseHelperProvider { * @param {any} section Section to calculate its status. It can't be "All sections". * @param {number} courseId Course ID the section belongs to. * @param {boolean} [refresh] True if it shouldn't use module status cache (slower). + * @param {boolean} [checkUpdates=true] Whether to use the WS to check updates. Defaults to true. * @return {Promise} Promise resolved when the status is calculated. */ - calculateSectionStatus(section: any, courseId: number, refresh?: boolean): Promise { + calculateSectionStatus(section: any, courseId: number, refresh?: boolean, checkUpdates: boolean = true): Promise { if (section.id == CoreCourseProvider.ALL_SECTIONS_ID) { return Promise.reject(null); } // Get the status of this section. - return this.prefetchDelegate.getModulesStatus(section.modules, courseId, section.id, refresh, true).then((result) => { + return this.prefetchDelegate.getModulesStatus(section.modules, courseId, section.id, refresh, true, checkUpdates) + .then((result) => { + // Check if it's being downloaded. const downloadId = this.getSectionDownloadId(section); if (this.prefetchDelegate.isBeingDownloaded(downloadId)) { @@ -228,9 +231,10 @@ export class CoreCourseHelperProvider { * @param {any[]} sections Sections to calculate their status. * @param {number} courseId Course ID the sections belong to. * @param {boolean} [refresh] True if it shouldn't use module status cache (slower). + * @param {boolean} [checkUpdates=true] Whether to use the WS to check updates. Defaults to true. * @return {Promise} Promise resolved when the states are calculated. */ - calculateSectionsStatus(sections: any[], courseId: number, refresh?: boolean): Promise { + calculateSectionsStatus(sections: any[], courseId: number, refresh?: boolean, checkUpdates: boolean = true): Promise { const promises = []; let allSectionsSection, allSectionsStatus; @@ -242,7 +246,7 @@ export class CoreCourseHelperProvider { section.isCalculating = true; } else { section.isCalculating = true; - promises.push(this.calculateSectionStatus(section, courseId, refresh).then((result) => { + promises.push(this.calculateSectionStatus(section, courseId, refresh, checkUpdates).then((result) => { // Calculate "All sections" status. allSectionsStatus = this.filepoolProvider.determinePackagesStatus(allSectionsStatus, result.status); }).finally(() => { @@ -1331,7 +1335,7 @@ export class CoreCourseHelperProvider { // Download only this section. return this.prefetchSingleSectionIfNeeded(section, courseId).finally(() => { // Calculate the status of the section that finished. - return this.calculateSectionStatus(section, courseId); + return this.calculateSectionStatus(section, courseId, false, false); }); } else { // Download all the sections except "All sections". @@ -1343,7 +1347,7 @@ export class CoreCourseHelperProvider { if (section.id != CoreCourseProvider.ALL_SECTIONS_ID) { promises.push(this.prefetchSingleSectionIfNeeded(section, courseId).finally(() => { // Calculate the status of the section that finished. - return this.calculateSectionStatus(section, courseId).then((result) => { + return this.calculateSectionStatus(section, courseId, false, false).then((result) => { // Calculate "All sections" status. allSectionsStatus = this.filepoolProvider.determinePackagesStatus(allSectionsStatus, result.status); }); diff --git a/src/core/course/providers/module-prefetch-delegate.ts b/src/core/course/providers/module-prefetch-delegate.ts index efb256400..267027d84 100644 --- a/src/core/course/providers/module-prefetch-delegate.ts +++ b/src/core/course/providers/module-prefetch-delegate.ts @@ -795,6 +795,7 @@ export class CoreCourseModulePrefetchDelegate extends CoreDelegate { * @param {number} [sectionId] ID of the section the modules belong to. * @param {boolean} [refresh] True if it should always check the DB (slower). * @param {boolean} [onlyToDisplay] True if the status will only be used to determine which button should be displayed. + * @param {boolean} [checkUpdates=true] Whether to use the WS to check updates. Defaults to true. * @return {Promise} Promise resolved with an object with the following properties: * - status (string) Status of the module. * - total (number) Number of modules. @@ -803,12 +804,15 @@ export class CoreCourseModulePrefetchDelegate extends CoreDelegate { * - CoreConstants.DOWNLOADING (any[]) Modules with state DOWNLOADING. * - CoreConstants.OUTDATED (any[]) Modules with state OUTDATED. */ - getModulesStatus(modules: any[], courseId: number, sectionId?: number, refresh?: boolean, onlyToDisplay?: boolean): any { + getModulesStatus(modules: any[], courseId: number, sectionId?: number, refresh?: boolean, onlyToDisplay?: boolean, + checkUpdates: boolean = true): any { + const promises = [], result: any = { total: 0 }; - let status = CoreConstants.NOT_DOWNLOADABLE; + let status = CoreConstants.NOT_DOWNLOADABLE, + promise; // Init result. result[CoreConstants.NOT_DOWNLOADED] = []; @@ -816,11 +820,17 @@ export class CoreCourseModulePrefetchDelegate extends CoreDelegate { result[CoreConstants.DOWNLOADING] = []; result[CoreConstants.OUTDATED] = []; - // Check updates in course. Don't use getCourseUpdates because the list of modules might not be the whole course list. - return this.getCourseUpdatesByCourseId(courseId).catch(() => { - // Cannot get updates. - return false; - }).then((updates) => { + if (checkUpdates) { + // Check updates in course. Don't use getCourseUpdates because the list of modules might not be the whole course list. + promise = this.getCourseUpdatesByCourseId(courseId).catch(() => { + // Cannot get updates. + return false; + }); + } else { + promise = Promise.resolve(false); + } + + return promise.then((updates) => { modules.forEach((module) => { // Check if the module has a prefetch handler.