MOBILE-4539 course: Stop calling isDownloadable in course page

main
Dani Palou 2024-03-07 11:54:14 +01:00
parent 9b9b569703
commit 054160f322
2 changed files with 92 additions and 7 deletions

View File

@ -115,7 +115,7 @@ export class CoreCourseModuleComponent implements OnInit, OnDestroy {
} }
if (this.showDownloadStatus && this.module.handlerData.showDownloadButton) { if (this.showDownloadStatus && this.module.handlerData.showDownloadButton) {
const status = await CoreCourseModulePrefetchDelegate.getModuleStatus(this.module, this.module.course); const status = await CoreCourseModulePrefetchDelegate.getDownloadedModuleStatus(this.module, this.module.course);
this.updateModuleStatus(status); this.updateModuleStatus(status);
// Listen for changes on this module status, even if download isn't enabled. // Listen for changes on this module status, even if download isn't enabled.
@ -146,7 +146,7 @@ export class CoreCourseModuleComponent implements OnInit, OnDestroy {
* *
* @param prefetchStatus Module status. * @param prefetchStatus Module status.
*/ */
protected updateModuleStatus(prefetchStatus: TDownloadStatus): void { protected updateModuleStatus(prefetchStatus: TDownloadStatus | null): void {
if (!prefetchStatus) { if (!prefetchStatus) {
return; return;
} }

View File

@ -25,7 +25,7 @@ import { CoreUtils } from '@services/utils/utils';
import { CoreCourse, CoreCourseAnyModuleData, CoreCourseModuleContentFile } from './course'; import { CoreCourse, CoreCourseAnyModuleData, CoreCourseModuleContentFile } from './course';
import { CoreCache } from '@classes/cache'; import { CoreCache } from '@classes/cache';
import { CoreSiteWSPreSets } from '@classes/sites/authenticated-site'; import { CoreSiteWSPreSets } from '@classes/sites/authenticated-site';
import { DownloadStatus, TDownloadStatus } from '@/core/constants'; import { DownloadStatus, TDownloadStatus, TDownloadedStatus } from '@/core/constants';
import { CoreDelegate, CoreDelegateHandler } from '@classes/delegate'; import { CoreDelegate, CoreDelegateHandler } from '@classes/delegate';
import { makeSingleton } from '@singletons'; import { makeSingleton } from '@singletons';
import { CoreEvents, CoreEventSectionStatusChangedData } from '@singletons/events'; import { CoreEvents, CoreEventSectionStatusChangedData } from '@singletons/events';
@ -554,6 +554,52 @@ export class CoreCourseModulePrefetchDelegateService extends CoreDelegate<CoreCo
return this.determineModuleStatus(module, result.status); return this.determineModuleStatus(module, result.status);
} }
/**
* If a module is downloaded or downloading, return its status.
* This function has a better performance than getModuleStatus, but it doesn't allow you to differentiate betweeen
* NOT_DOWNLOADABLE and DOWNLOADABLE_NOT_DOWNLOADED.
*
* @param module Module.
* @param courseId Course ID the module belongs to.
* @param updates Result of getCourseUpdates for all modules in the course. If not provided, it will be
* calculated (slower). If it's false it means the site doesn't support check updates.
* @param refresh True if it should ignore the memory cache, not the WS cache.
* @param sectionId ID of the section the module belongs to.
* @returns Promise resolved with the status, null if not downloaded.
*/
async getDownloadedModuleStatus(
module: CoreCourseAnyModuleData,
courseId: number,
updates?: CourseUpdates | false,
refresh?: boolean,
sectionId?: number,
): Promise<TDownloadedStatus | null> {
const handler = this.getPrefetchHandlerFor(module.modname);
if (!handler) {
// No handler found, module not downloadable.
return null;
}
// Check if the status is cached.
const packageId = CoreFilepool.getPackageId(handler.component, module.id);
const status = this.statusCache.getValue<TDownloadStatus>(packageId, 'status');
if (!refresh && status !== undefined) {
this.storeCourseAndSection(packageId, courseId, sectionId);
return this.filterDownloadedStatus(this.determineModuleStatus(module, status));
}
const result = await this.calculateDownloadedModuleStatus(handler, module, courseId, updates, sectionId);
if (result.updateStatus && result.status) {
this.updateStatusCache(result.status, handler.component, module.id, courseId, sectionId);
}
return this.filterDownloadedStatus(
this.determineModuleStatus(module, result.status ?? DownloadStatus.DOWNLOADABLE_NOT_DOWNLOADED),
);
}
/** /**
* Calculate a module status. * Calculate a module status.
* *
@ -581,14 +627,42 @@ export class CoreCourseModulePrefetchDelegateService extends CoreDelegate<CoreCo
}; };
} }
const result = await this.calculateDownloadedModuleStatus(handler, module, courseId, updates, sectionId);
return {
status: result.status ?? DownloadStatus.DOWNLOADABLE_NOT_DOWNLOADED,
updateStatus: result.updateStatus,
};
}
/**
* Calculate the status of a downloaded module.
* This function has a better performance than calculateModuleStatus, but it doesn't allow you to differentiate betweeen
* NOT_DOWNLOADABLE and DOWNLOADABLE_NOT_DOWNLOADED.
*
* @param handler Prefetch handler.
* @param module Module.
* @param courseId Course ID the module belongs to.
* @param updates Result of getCourseUpdates for all modules in the course. If not provided, it will be
* calculated (slower). If it's false it means the site doesn't support check updates.
* @param sectionId ID of the section the module belongs to.
* @returns Promise resolved with the status.
*/
protected async calculateDownloadedModuleStatus(
handler: CoreCourseModulePrefetchHandler,
module: CoreCourseAnyModuleData,
courseId: number,
updates?: CourseUpdates | false,
sectionId?: number,
): Promise<{status: TDownloadedStatus | null; updateStatus: boolean}> {
// Get the saved package status. // Get the saved package status.
const siteId = CoreSites.getCurrentSiteId(); const siteId = CoreSites.getCurrentSiteId();
const currentStatus = await CoreFilepool.getPackageStatus(siteId, handler.component, module.id); const currentStatus = await CoreFilepool.getPackageStatus(siteId, handler.component, module.id);
let status = handler.determineStatus ? handler.determineStatus(module, currentStatus, true) : currentStatus; let status = handler.determineStatus ? handler.determineStatus(module, currentStatus, true) : currentStatus;
if (status != DownloadStatus.DOWNLOADED || updates === false) { if (status !== DownloadStatus.DOWNLOADED || updates === false) {
return { return {
status, status: this.filterDownloadedStatus(status),
updateStatus: true, updateStatus: true,
}; };
} }
@ -604,7 +678,7 @@ export class CoreCourseModulePrefetchDelegateService extends CoreDelegate<CoreCo
this.storeCourseAndSection(packageId, courseId, sectionId); this.storeCourseAndSection(packageId, courseId, sectionId);
return { return {
status: currentStatus, status: this.filterDownloadedStatus(currentStatus),
updateStatus: false, updateStatus: false,
}; };
} }
@ -647,12 +721,23 @@ export class CoreCourseModulePrefetchDelegateService extends CoreDelegate<CoreCo
const status = this.statusCache.getValue<TDownloadStatus>(packageId, 'status', true); const status = this.statusCache.getValue<TDownloadStatus>(packageId, 'status', true);
return { return {
status: this.determineModuleStatus(module, status || DownloadStatus.DOWNLOADABLE_NOT_DOWNLOADED), status: status ? this.filterDownloadedStatus(status) : null,
updateStatus: true, updateStatus: true,
}; };
} }
} }
/**
* Given a download status, filter it to return only the downloaded statuses.
*
* @param status Status.
* @returns Filtered status, null for not downloaded statuses.
*/
protected filterDownloadedStatus(status: TDownloadStatus): TDownloadedStatus | null {
return status === DownloadStatus.NOT_DOWNLOADABLE || status === DownloadStatus.DOWNLOADABLE_NOT_DOWNLOADED ?
null : status;
}
/** /**
* Get the status of a list of modules, along with the lists of modules for each status. * Get the status of a list of modules, along with the lists of modules for each status.
* *