diff --git a/src/addons/mod/lesson/components/index/index.ts b/src/addons/mod/lesson/components/index/index.ts index 8c3a34a4a..c449a9fe9 100644 --- a/src/addons/mod/lesson/components/index/index.ts +++ b/src/addons/mod/lesson/components/index/index.ts @@ -558,13 +558,9 @@ export class AddonModLessonIndexComponent extends CoreCourseModuleMainActivityCo } /** - * Displays some data based on the current status. - * - * @param status The current status. - * @param previousStatus The previous status. If not defined, there is no previous status. + * @inheritdoc */ - // eslint-disable-next-line @typescript-eslint/no-unused-vars - protected showStatus(status: string, previousStatus?: string): void { + protected showStatus(status: string): void { this.showSpinner = status == CoreConstants.DOWNLOADING; } diff --git a/src/addons/mod/resource/components/index/addon-mod-resource-index.html b/src/addons/mod/resource/components/index/addon-mod-resource-index.html index c209b8031..a299d9a31 100644 --- a/src/addons/mod/resource/components/index/addon-mod-resource-index.html +++ b/src/addons/mod/resource/components/index/addon-mod-resource-index.html @@ -63,7 +63,7 @@

{{ 'core.lastdownloaded' | translate }}

{{ downloadTimeReadable }}

- + diff --git a/src/addons/mod/resource/components/index/index.ts b/src/addons/mod/resource/components/index/index.ts index 203b2349e..6c26ec16e 100644 --- a/src/addons/mod/resource/components/index/index.ts +++ b/src/addons/mod/resource/components/index/index.ts @@ -125,6 +125,8 @@ export class AddonModResourceIndexComponent extends CoreCourseModuleMainResource this.displayDescription = options.printintro === undefined || !!options.printintro; this.dataRetrieved.emit(resource); + this.setStatusListener(); + if (AddonModResourceHelper.isDisplayedInIframe(this.module)) { const downloadResult = await this.downloadResourceIfNeeded(refresh, true); @@ -202,7 +204,7 @@ export class AddonModResourceIndexComponent extends CoreCourseModuleMainResource downloadable = await AddonModResourceHelper.isMainFileDownloadable(this.module); if (downloadable) { - if (this.prefetchStatus === CoreConstants.OUTDATED && !this.isOnline) { + if (this.currentStatus === CoreConstants.OUTDATED && !this.isOnline) { // Warn the user that the file isn't updated. const alert = await CoreDomUtils.showAlert( undefined, diff --git a/src/core/features/course/classes/main-resource-component.ts b/src/core/features/course/classes/main-resource-component.ts index d59380505..d94f853dd 100644 --- a/src/core/features/course/classes/main-resource-component.ts +++ b/src/core/features/course/classes/main-resource-component.ts @@ -58,21 +58,20 @@ export class CoreCourseModuleMainResourceComponent implements OnInit, OnDestroy, hasOffline = false; // Resources don't have any data to sync. description?: string; // Module description. - prefetchStatus?: string; - downloadTimeReadable?: string; // Last download time in a readable format. isDestroyed = false; // Whether the component is destroyed. protected fetchContentDefaultError = 'core.course.errorgetmodule'; // Default error to show when loading contents. protected isCurrentView = false; // Whether the component is in the current view. protected siteId?: string; // Current Site ID. protected statusObserver?: CoreEventObserver; // Observer of package status. Only if setStatusListener is called. - protected currentStatus?: string; // The current status of the module. Only if setStatusListener is called. + currentStatus?: string; // The current status of the module. Only if setStatusListener is called. + downloadTimeReadable?: string; // Last download time in a readable format. Only if setStatusListener is called. + protected completionObserver?: CoreEventObserver; protected logger: CoreLogger; protected debouncedUpdateModule?: () => void; // Update the module after a certain time. protected showCompletion = false; // Whether to show completion inside the activity. protected displayDescription = true; // Wether to show Module description on module page, and not on summary or the contrary. - protected packageStatusObserver?: CoreEventObserver; // Observer of package status. constructor( @Optional() @Inject('') loggerName: string = 'CoreCourseModuleMainResourceComponent', @@ -106,16 +105,6 @@ export class CoreCourseModuleMainResourceComponent implements OnInit, OnDestroy, this.fetchModule(); }, 10000); } - - this.packageStatusObserver = CoreEvents.on( - CoreEvents.PACKAGE_STATUS_CHANGED, - (data) => { - if (data.componentId == module.id && data.component == this.component) { - this.getPackageStatus(); - } - }, - this.siteId, - ); } /** @@ -203,7 +192,6 @@ export class CoreCourseModuleMainResourceComponent implements OnInit, OnDestroy, try { await this.fetchContent(refresh); - await this.getPackageStatus(refresh); } catch (error) { if (!refresh && !CoreSites.getCurrentSite()?.isOfflineDisabled() && this.isNotFoundError(error)) { // Module not found, retry without using cache. @@ -227,28 +215,25 @@ export class CoreCourseModuleMainResourceComponent implements OnInit, OnDestroy, } /** - * Updage package status. - * - * @param refresh If prefetch info has to be refreshed. + * Updage package last downloaded. */ - async getPackageStatus(refresh = false): Promise { + protected async getPackageLastDownloaded(): Promise { if (!this.module) { return; } - const moduleInfo = - await CoreCourseHelper.getModulePrefetchInfo(this.module, this.courseId, refresh, this.component); + const lastDownloaded = + await CoreCourseHelper.getModulePackageLastDownloaded(this.module, this.component); - this.downloadTimeReadable = CoreTextUtils.ucFirst(moduleInfo.downloadTimeReadable); - this.prefetchStatus = moduleInfo.status; + this.downloadTimeReadable = lastDownloaded.downloadTimeReadable; } /** - * Check if the module is prefetched or being prefetched. To make it faster, just use the data calculated by fillContextMenu. - * This means that you need to call fillContextMenu to make this work. + * Check if the module is prefetched or being prefetched. + * To make it faster, just use the data calculated by setStatusListener. */ protected isPrefetched(): boolean { - return this.prefetchStatus != CoreConstants.NOT_DOWNLOADABLE && this.prefetchStatus != CoreConstants.NOT_DOWNLOADED; + return this.currentStatus != CoreConstants.NOT_DOWNLOADABLE && this.currentStatus != CoreConstants.NOT_DOWNLOADED; } /** @@ -308,6 +293,8 @@ export class CoreCourseModuleMainResourceComponent implements OnInit, OnDestroy, const previousStatus = this.currentStatus; this.currentStatus = data.status; + this.getPackageLastDownloaded(); + this.showStatus(this.currentStatus, previousStatus); }, this.siteId); } else if (!refresh) { @@ -322,6 +309,9 @@ export class CoreCourseModuleMainResourceComponent implements OnInit, OnDestroy, const status = await CoreCourseModulePrefetchDelegate.getModuleStatus(this.module, this.courseId, undefined, refresh); this.currentStatus = status; + + this.getPackageLastDownloaded(); + this.showStatus(status); } @@ -451,7 +441,6 @@ export class CoreCourseModuleMainResourceComponent implements OnInit, OnDestroy, this.isDestroyed = true; this.statusObserver?.off(); this.completionObserver?.off(); - this.packageStatusObserver?.off(); } /** diff --git a/src/core/features/course/components/module-navigation/module-navigation.ts b/src/core/features/course/components/module-navigation/module-navigation.ts index 2b3573738..3a566c701 100644 --- a/src/core/features/course/components/module-navigation/module-navigation.ts +++ b/src/core/features/course/components/module-navigation/module-navigation.ts @@ -110,7 +110,7 @@ export class CoreCourseModuleNavigationComponent implements OnInit, OnDestroy { return; } // Set a minimum height value. - this.initialHeight = this.initialHeight || 56; + this.initialHeight = this.initialHeight || 48; this.previousHeight = this.initialHeight; this.content = this.element.closest('ion-content'); diff --git a/src/core/features/course/services/course-helper.ts b/src/core/features/course/services/course-helper.ts index 39e917109..77aab7baa 100644 --- a/src/core/features/course/services/course-helper.ts +++ b/src/core/features/course/services/course-helper.ts @@ -75,36 +75,19 @@ import { CoreStatusWithWarningsWSResponse } from '@services/ws'; /** * Prefetch info of a module. */ -export type CoreCourseModulePrefetchInfo = { - /** - * Downloaded size. - */ - size: number; +export type CoreCourseModulePrefetchInfo = CoreCourseModulePackageLastDownloaded & { + size: number; // Downloaded size. + sizeReadable: string; // Downloadable size in a readable format. + status: string; // Module status. + statusIcon?: string; // Icon's name of the module status. +}; - /** - * Downloadable size in a readable format. - */ - sizeReadable: string; - - /** - * Module status. - */ - status: string; - - /** - * Icon's name of the module status. - */ - statusIcon?: string; - - /** - * Time when the module was last downloaded. - */ - downloadTime: number; - - /** - * Download time in a readable format. - */ - downloadTimeReadable: string; +/** + * Prefetch info of a module. + */ +export type CoreCourseModulePackageLastDownloaded = { + downloadTime: number; // Time when the module was last downloaded. + downloadTimeReadable: string; // Download time in a readable format. }; /** @@ -1484,12 +1467,9 @@ export class CoreCourseHelperProvider { async getModulePrefetchInfo( module: CoreCourseModuleData, courseId: number, - invalidateCache?: boolean, - component?: string, + invalidateCache = false, + component = '', ): Promise { - - 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. @@ -1501,7 +1481,7 @@ export class CoreCourseHelperProvider { const results = await Promise.all([ CoreCourseModulePrefetchDelegate.getModuleStoredSize(module, courseId), CoreCourseModulePrefetchDelegate.getModuleStatus(module, courseId), - CoreUtils.ignoreErrors(CoreFilepool.getPackageData(siteId, component || '', module.id)), + this.getModulePackageLastDownloaded(module, component), ]); // Treat stored size. @@ -1528,33 +1508,53 @@ export class CoreCourseHelperProvider { break; } - // Treat download time. - if (!results[2] || !results[2].downloadTime || !CoreFileHelper.isStateDownloaded(results[2].status || '')) { - // Not downloaded. - return { - size, - sizeReadable, - status, - statusIcon, - downloadTime: 0, - downloadTimeReadable: '', - }; - } - - const now = CoreTimeUtils.timestamp(); - const downloadTime = results[2].downloadTime; - let downloadTimeReadable = ''; - if (now - results[2].downloadTime < 7 * 86400) { - downloadTimeReadable = moment(results[2].downloadTime * 1000).fromNow(); - } else { - downloadTimeReadable = moment(results[2].downloadTime * 1000).calendar(); - } + const packageData = results[2]; return { size, sizeReadable, status, statusIcon, + downloadTime: packageData.downloadTime, + downloadTimeReadable: packageData.downloadTimeReadable, + }; + } + + /** + * Get prefetch info for a module. + * + * @param module Module to get the info from. + * @param component Component of the module. + * @return Promise resolved with the info. + */ + async getModulePackageLastDownloaded( + module: CoreCourseModuleData, + component = '', + ): Promise { + const siteId = CoreSites.getCurrentSiteId(); + const packageData = await CoreUtils.ignoreErrors(CoreFilepool.getPackageData(siteId, component, module.id)); + + // Treat download time. + if (!packageData || !packageData.downloadTime || !CoreFileHelper.isStateDownloaded(packageData.status || '')) { + // Not downloaded. + return { + downloadTime: 0, + downloadTimeReadable: '', + }; + } + + const now = CoreTimeUtils.timestamp(); + const downloadTime = packageData.downloadTime; + let downloadTimeReadable = ''; + if (now - downloadTime < 7 * 86400) { + downloadTimeReadable = moment(downloadTime * 1000).fromNow(); + } else { + downloadTimeReadable = moment(downloadTime * 1000).calendar(); + } + + downloadTimeReadable = CoreTextUtils.ucFirst(downloadTimeReadable); + + return { downloadTime, downloadTimeReadable, }; diff --git a/src/theme/theme.light.scss b/src/theme/theme.light.scss index 6f2e2b837..edcdad2f9 100644 --- a/src/theme/theme.light.scss +++ b/src/theme/theme.light.scss @@ -233,6 +233,9 @@ } --core-loading-spinner: var(--brand); + ion-loading { + --spinner-color: var(--core-loading-spinner); + } ion-spinner, ion-refresher { --ion-color-base: var(--core-loading-spinner); --ion-color-primary: var(--core-loading-spinner);