diff --git a/src/addons/mod/book/components/index/index.ts b/src/addons/mod/book/components/index/index.ts index 76c994b86..9473a677c 100644 --- a/src/addons/mod/book/components/index/index.ts +++ b/src/addons/mod/book/components/index/index.ts @@ -186,7 +186,8 @@ export class AddonModBookIndexComponent extends CoreCourseModuleMainResourceComp // Ignore errors, they're handled inside the loadChapter function. } } finally { - this.fillContextMenu(refresh); + // Pass false because downloadResourceIfNeeded already invalidates and refresh data if refresh=true. + this.fillContextMenu(false); } } diff --git a/src/addons/mod/imscp/components/index/index.ts b/src/addons/mod/imscp/components/index/index.ts index 83c8e976f..b6c6e301b 100644 --- a/src/addons/mod/imscp/components/index/index.ts +++ b/src/addons/mod/imscp/components/index/index.ts @@ -117,7 +117,8 @@ export class AddonModImscpIndexComponent extends CoreCourseModuleMainResourceCom this.warning = downloadResult!.failed ? this.getErrorDownloadingSomeFilesMessage(downloadResult!.error!) : ''; } finally { - this.fillContextMenu(refresh); + // Pass false because downloadResourceIfNeeded already invalidates and refresh data if refresh=true. + this.fillContextMenu(false); } } diff --git a/src/addons/mod/page/components/index/index.ts b/src/addons/mod/page/components/index/index.ts index d8d3cdb61..acfedd74b 100644 --- a/src/addons/mod/page/components/index/index.ts +++ b/src/addons/mod/page/components/index/index.ts @@ -143,7 +143,8 @@ export class AddonModPageIndexComponent extends CoreCourseModuleMainResourceComp await Promise.all(promises); } finally { - this.fillContextMenu(refresh); + // Pass false because downloadResourceIfNeeded already invalidates and refresh data if refresh=true. + this.fillContextMenu(false); } } diff --git a/src/addons/mod/resource/components/index/index.ts b/src/addons/mod/resource/components/index/index.ts index aca96abae..11f4241c2 100644 --- a/src/addons/mod/resource/components/index/index.ts +++ b/src/addons/mod/resource/components/index/index.ts @@ -114,6 +114,7 @@ export class AddonModResourceIndexComponent extends CoreCourseModuleMainResource let resource: AddonModResourceResource | CoreCourseWSModule | undefined; let options: AddonModResourceCustomData = {}; + let hasCalledDownloadResource = false; // Get the resource instance to get the latest name/description and to know if it's embedded. if (this.canGetResource) { @@ -133,6 +134,8 @@ export class AddonModResourceIndexComponent extends CoreCourseModuleMainResource } if (AddonModResourceHelper.isDisplayedInIframe(this.module)) { + hasCalledDownloadResource = true; + const downloadResult = await this.downloadResourceIfNeeded(refresh, true); const src = await AddonModResourceHelper.getIframeSrc(this.module); this.mode = 'iframe'; @@ -174,7 +177,8 @@ export class AddonModResourceIndexComponent extends CoreCourseModuleMainResource this.isStreamedFile = CoreMimetypeUtils.isStreamedMimetype(mimetype); } } finally { - this.fillContextMenu(refresh); + // Pass false in some cases because downloadResourceIfNeeded already invalidates and refresh data if refresh=true. + this.fillContextMenu(hasCalledDownloadResource ? false : refresh); } } diff --git a/src/core/features/course/classes/main-resource-component.ts b/src/core/features/course/classes/main-resource-component.ts index 287435b87..3fb59518a 100644 --- a/src/core/features/course/classes/main-resource-component.ts +++ b/src/core/features/course/classes/main-resource-component.ts @@ -321,28 +321,33 @@ export class CoreCourseModuleMainResourceComponent implements OnInit, OnDestroy, /** * Watch for changes on the status. * + * @param refresh Whether we're refreshing data. * @return Promise resolved when done. */ - protected async setStatusListener(): Promise { - if (typeof this.statusObserver != 'undefined') { + protected async setStatusListener(refresh?: boolean): Promise { + if (this.statusObserver === undefined) { + // Listen for changes on this module status. + this.statusObserver = CoreEvents.on(CoreEvents.PACKAGE_STATUS_CHANGED, (data) => { + if (data.componentId != this.module.id || data.component != this.component) { + return; + } + + // The status has changed, update it. + const previousStatus = this.currentStatus; + this.currentStatus = data.status; + + this.showStatus(this.currentStatus, previousStatus); + }, this.siteId); + } else if (!refresh) { return; } - // Listen for changes on this module status. - this.statusObserver = CoreEvents.on(CoreEvents.PACKAGE_STATUS_CHANGED, (data) => { - if (data.componentId != this.module.id || data.component != this.component) { - return; - } - - // The status has changed, update it. - const previousStatus = this.currentStatus; - this.currentStatus = data.status; - - this.showStatus(this.currentStatus, previousStatus); - }, this.siteId); + if (refresh) { + await CoreUtils.ignoreErrors(CoreCourseModulePrefetchDelegate.invalidateCourseUpdates(this.courseId)); + } // Also, get the current status. - const status = await CoreCourseModulePrefetchDelegate.getModuleStatus(this.module, this.courseId); + const status = await CoreCourseModulePrefetchDelegate.getModuleStatus(this.module, this.courseId, undefined, refresh); this.currentStatus = status; this.showStatus(status); @@ -366,7 +371,7 @@ export class CoreCourseModuleMainResourceComponent implements OnInit, OnDestroy, }; // Get module status to determine if it needs to be downloaded. - await this.setStatusListener(); + await this.setStatusListener(refresh); if (this.currentStatus != CoreConstants.DOWNLOADED) { // Download content. This function also loads module contents if needed. diff --git a/src/core/features/course/services/course-helper.ts b/src/core/features/course/services/course-helper.ts index 61f2c9df7..be69290b1 100644 --- a/src/core/features/course/services/course-helper.ts +++ b/src/core/features/course/services/course-helper.ts @@ -1455,6 +1455,8 @@ export class CoreCourseHelperProvider { const siteId = CoreSites.getCurrentSiteId(); if (invalidateCache) { + // Currently, some modules pass invalidateCache=false because they already invalidate data in downloadResourceIfNeeded. + // If this function is changed to do more actions if invalidateCache=true, please review those modules. CoreCourseModulePrefetchDelegate.invalidateModuleStatusCache(module); } diff --git a/src/core/features/course/services/module-prefetch-delegate.ts b/src/core/features/course/services/module-prefetch-delegate.ts index 04d7fbc4c..8ee32560d 100644 --- a/src/core/features/course/services/module-prefetch-delegate.ts +++ b/src/core/features/course/services/module-prefetch-delegate.ts @@ -546,7 +546,7 @@ export class CoreCourseModulePrefetchDelegateService extends CoreDelegate