Skip to content

Commit

Permalink
MOBILE-4627 page: Don't download all files when opening
Browse files Browse the repository at this point in the history
  • Loading branch information
dpalou committed Aug 7, 2024
1 parent 330b702 commit 8c566af
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,6 @@
<core-course-module-info [module]="module" [description]="displayDescription && description" [component]="component"
[componentId]="componentId" [courseId]="courseId" (completionChanged)="onCompletionChange()" />

<ion-card class="core-warning-card" *ngIf="warning">
<ion-item>
<ion-icon name="fas-triangle-exclamation" slot="start" aria-hidden="true" />
<ion-label><span [innerHTML]="warning"></span></ion-label>
</ion-item>
</ion-card>

<div class="ion-padding">
<core-format-text [component]="component" [componentId]="componentId" [text]="contents" contextLevel="module"
[contextInstanceId]="module.id" [courseId]="courseId" />
Expand Down
15 changes: 3 additions & 12 deletions src/addons/mod/page/components/index/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import { Component, OnInit, Optional } from '@angular/core';
import { CoreCourseModuleMainResourceComponent } from '@features/course/classes/main-resource-component';
import { CoreCourseContentsPage } from '@features/course/pages/contents/contents';
import { CoreCourse } from '@features/course/services/course';
import { CoreTextUtils } from '@services/utils/text';
import { CoreUtils } from '@services/utils/utils';
import { AddonModPagePage, AddonModPage } from '../../services/page';
Expand All @@ -38,7 +37,6 @@ export class AddonModPageIndexComponent extends CoreCourseModuleMainResourceComp
displayTimemodified = true;
timemodified?: number;
page?: AddonModPagePage;
warning?: string;

protected fetchContentDefaultError = 'addon.mod_page.errorwhileloadingthepage';

Expand Down Expand Up @@ -68,19 +66,12 @@ export class AddonModPageIndexComponent extends CoreCourseModuleMainResourceComp
* @inheritdoc
*/
protected async fetchContent(refresh?: boolean): Promise<void> {
// Download the resource if it needs to be downloaded.
const downloadResult = await this.downloadResourceIfNeeded(refresh);

// Get contents. No need to refresh, it has been done in downloadResourceIfNeeded.
const contents = await CoreCourse.getModuleContents(this.module);

const results = await Promise.all([
const [contents] = await Promise.all([
this.getModuleContents(refresh),
this.loadPageData(),
AddonModPageHelper.getPageHtml(contents, this.module.id),
]);

this.contents = results[1];
this.warning = downloadResult?.failed ? this.getErrorDownloadingSomeFilesMessage(downloadResult.error!) : '';
this.contents = await AddonModPageHelper.getPageHtml(contents, this.module.id);
}

/**
Expand Down
40 changes: 26 additions & 14 deletions src/core/features/course/classes/main-resource-component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import { CoreEventObserver, CoreEvents } from '@singletons/events';
import { CoreLogger } from '@singletons/logger';
import { CoreCourseModuleSummaryResult } from '../components/module-summary/module-summary';
import { CoreCourseContentsPage } from '../pages/contents/contents';
import { CoreCourse } from '../services/course';
import { CoreCourse, CoreCourseModuleContentFile } from '../services/course';
import { CoreCourseHelper, CoreCourseModuleData } from '../services/course-helper';
import { CoreCourseModuleDelegate, CoreCourseModuleMainComponent } from '../services/module-delegate';
import { CoreCourseModulePrefetchDelegate } from '../services/module-prefetch-delegate';
Expand Down Expand Up @@ -364,24 +364,36 @@ export class CoreCourseModuleMainResourceComponent implements OnInit, OnDestroy,

if (!this.module.contents?.length || (refresh && !contentsAlreadyLoaded)) {
// Try to load the contents.
const ignoreCache = refresh && CoreNetwork.isOnline();

try {
await CoreCourse.loadModuleContents(this.module, undefined, undefined, false, ignoreCache);
} catch (error) {
// Error loading contents. If we ignored cache, try to get the cached value.
if (ignoreCache && !this.module.contents) {
await CoreCourse.loadModuleContents(this.module);
} else if (!this.module.contents) {
// Not able to load contents, throw the error.
throw error;
}
}
await this.getModuleContents(refresh);
}

return result;
}

/**
* Get module contents.
*
* @param refresh Whether we're refreshing data.
* @returns Module contents.
*/
protected async getModuleContents(refresh?: boolean): Promise<CoreCourseModuleContentFile[]> {
const ignoreCache = refresh && CoreNetwork.isOnline();

try {
return await CoreCourse.getModuleContents(this.module, undefined, undefined, false, ignoreCache);
} catch (error) {
// Error loading contents. If we ignored cache, try to get the cached value.
if (ignoreCache && !this.module.contents) {
return await CoreCourse.getModuleContents(this.module);
} else if (!this.module.contents) {
// Not able to load contents, throw the error.
throw error;
}

return this.module.contents;
}
}

/**
* The completion of the modules has changed.
*
Expand Down

0 comments on commit 8c566af

Please sign in to comment.