Skip to content

Commit

Permalink
Merge pull request #4059 from dpalou/MOBILE-4470
Browse files Browse the repository at this point in the history
Mobile 4470
  • Loading branch information
crazyserver authored May 22, 2024
2 parents 24c8c63 + 40032e7 commit f7a3b65
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,7 @@ export class AddonStorageManagerCoursesStoragePage implements OnInit, OnDestroy
}
}

this.spaceUsage = await CoreSettingsHelper.getSiteSpaceUsage(this.siteId);

this.setDownloadedCourses(downloadedCourses);
await this.setDownloadedCourses(downloadedCourses);

this.loaded = true;
}
Expand Down Expand Up @@ -126,7 +124,7 @@ export class AddonStorageManagerCoursesStoragePage implements OnInit, OnDestroy
try {
await Promise.all(deletedCourseIds.map((courseId) => CoreCourseHelper.deleteCourseFiles(courseId)));

this.setDownloadedCourses(this.downloadedCourses.filter((course) => !deletedCourseIds.includes(course.id)));
await this.setDownloadedCourses(this.downloadedCourses.filter((course) => !deletedCourseIds.includes(course.id)));
} catch (error) {
CoreDomUtils.showErrorModalDefault(error, Translate.instant('core.errordeletefile'));
} finally {
Expand Down Expand Up @@ -162,7 +160,7 @@ export class AddonStorageManagerCoursesStoragePage implements OnInit, OnDestroy
try {
await CoreCourseHelper.deleteCourseFiles(course.id);

this.setDownloadedCourses(CoreArray.withoutItem(this.downloadedCourses, course));
await this.setDownloadedCourses(CoreArray.withoutItem(this.downloadedCourses, course));
} catch (error) {
CoreDomUtils.showErrorModalDefault(error, Translate.instant('core.errordeletefile'));
} finally {
Expand All @@ -177,7 +175,7 @@ export class AddonStorageManagerCoursesStoragePage implements OnInit, OnDestroy
*/
private async onCourseUpdated(courseId: number, status: DownloadStatus): Promise<void> {
if (courseId == CoreCourseProvider.ALL_COURSES_CLEARED) {
this.setDownloadedCourses([]);
await this.setDownloadedCourses([]);

return;
}
Expand All @@ -191,15 +189,18 @@ export class AddonStorageManagerCoursesStoragePage implements OnInit, OnDestroy
course.isDownloading = status === DownloadStatus.DOWNLOADING;
course.totalSize = await this.calculateDownloadedCourseSize(course.id);

this.setDownloadedCourses(this.downloadedCourses);
await this.setDownloadedCourses(this.downloadedCourses);
}

/**
* Set downloaded courses data.
*
* @param courses Courses info.
*/
private setDownloadedCourses(courses: DownloadedCourse[]): void {
private async setDownloadedCourses(courses: DownloadedCourse[]): Promise<void> {
// Downloaded courses changed, update site usage too.
this.spaceUsage = await CoreSettingsHelper.getSiteSpaceUsage(this.siteId);

this.downloadedCourses = courses.sort((a, b) => b.totalSize - a.totalSize);
this.completelyDownloadedCourses = courses.filter((course) => !course.isDownloading);
this.totalSize = this.downloadedCourses.reduce((totalSize, course) => totalSize + course.totalSize, 0);
Expand Down
8 changes: 8 additions & 0 deletions src/core/directives/format-text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ export class CoreFormatTextDirective implements OnChanges, OnDestroy, AsyncDirec
protected emptyText = '';
protected domPromises: CoreCancellablePromise<void>[] = [];
protected domElementPromise?: CoreCancellablePromise<void>;
protected externalContentInstances: CoreExternalContentDirective[] = [];

constructor(
element: ElementRef,
Expand Down Expand Up @@ -145,6 +146,7 @@ export class CoreFormatTextDirective implements OnChanges, OnDestroy, AsyncDirec
this.domElementPromise?.cancel();
this.domPromises.forEach((promise) => { promise.cancel();});
this.elementControllers.forEach(controller => controller.destroy());
this.externalContentInstances.forEach(extContent => extContent.ngOnDestroy());
}

/**
Expand Down Expand Up @@ -191,6 +193,8 @@ export class CoreFormatTextDirective implements OnChanges, OnDestroy, AsyncDirec

extContent.ngAfterViewInit();

this.externalContentInstances.push(extContent);

const changeDetectorRef = this.viewContainerRef.injector.get(ChangeDetectorRef);
changeDetectorRef.markForCheck();

Expand Down Expand Up @@ -342,6 +346,10 @@ export class CoreFormatTextDirective implements OnChanges, OnDestroy, AsyncDirec
* Format contents and render.
*/
protected async formatAndRenderContents(): Promise<void> {
// Destroy previous instances of external-content.
this.externalContentInstances.forEach(extContent => extContent.ngOnDestroy());
this.externalContentInstances = [];

if (!this.text) {
this.element.innerHTML = this.emptyText; // Remove current contents.

Expand Down
8 changes: 5 additions & 3 deletions src/core/features/course/services/course-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1912,12 +1912,14 @@ export class CoreCourseHelperProvider {
* @returns Promise to be resolved once the course files are deleted.
*/
async deleteCourseFiles(courseId: number): Promise<void> {
const siteId = CoreSites.getCurrentSiteId();
const sections = await CoreCourse.getSections(courseId);
const modules = sections.map((section) => section.modules).flat();

await Promise.all(
modules.map((module) => this.removeModuleStoredData(module, courseId)),
);
await Promise.all([
...modules.map((module) => this.removeModuleStoredData(module, courseId)),
siteId && CoreFilepool.removeFilesByComponent(siteId, CoreCourseProvider.COMPONENT, courseId),
]);

await CoreCourse.setCourseStatus(courseId, DownloadStatus.DOWNLOADABLE_NOT_DOWNLOADED);
}
Expand Down

0 comments on commit f7a3b65

Please sign in to comment.