MOBILE-3931 module: Merge duplicated prefetch statuses

main
Pau Ferrer Ocaña 2022-02-15 17:12:37 +01:00
parent 344ee6d57e
commit 16cee9df14
7 changed files with 82 additions and 92 deletions

View File

@ -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;
}

View File

@ -63,7 +63,7 @@
<ion-label>
<h3>{{ 'core.lastdownloaded' | translate }}</h3>
<p>{{ downloadTimeReadable }}</p>
<ion-grid *ngIf="prefetchStatus === outdatedStatus" class="addon-mod_resource-outdated">
<ion-grid *ngIf="currentStatus === outdatedStatus" class="addon-mod_resource-outdated">
<ion-row class="ion-align-items-center">
<ion-col size="auto">
<ion-icon color="warning" name="fas-exclamation-triangle" aria-hidden="true"></ion-icon>

View File

@ -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,

View File

@ -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<void> {
protected async getPackageLastDownloaded(): Promise<void> {
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();
}
/**

View File

@ -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');

View File

@ -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<CoreCourseModulePrefetchInfo> {
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<CoreCourseModulePackageLastDownloaded> {
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,
};

View File

@ -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);