Skip to content

Commit

Permalink
MOBILE-4627 book: 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 8c566af commit d28f85a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 81 deletions.
7 changes: 0 additions & 7 deletions src/addons/mod/book/pages/contents/contents.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,6 @@ <h1>

<core-loading [hideUntil]="loaded">
<div class="safe-area-padding-horizontal core-swipe-slides-container">
<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>

<core-swipe-slides [manager]="manager" [options]="swiperOpts">
<ng-template let-chapter="item" let-active="active">
<div class="ion-padding">
Expand Down
109 changes: 35 additions & 74 deletions src/addons/mod/book/pages/contents/contents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,13 @@
// See the License for the specific language governing permissions and
// limitations under the License.

import { DownloadStatus } from '@/core/constants';
import { Component, OnDestroy, OnInit, ViewChild } from '@angular/core';
import { CoreError } from '@classes/errors/error';
import { CoreSwipeSlidesItemsManager } from '@classes/items-management/swipe-slides-items-manager';
import { CoreSwipeSlidesItemsManagerSource } from '@classes/items-management/swipe-slides-items-manager-source';
import { CoreNavigationBarItem } from '@components/navigation-bar/navigation-bar';
import { CoreSwipeSlidesComponent, CoreSwipeSlidesOptions } from '@components/swipe-slides/swipe-slides';
import { CoreCourseResourceDownloadResult } from '@features/course/classes/main-resource-component';
import { CoreCourse } from '@features/course/services/course';
import { CoreCourse, CoreCourseModuleContentFile } from '@features/course/services/course';
import { CoreCourseModuleData } from '@features/course/services/course-helper';
import { CoreCourseModulePrefetchDelegate } from '@features/course/services/module-prefetch-delegate';
import { CoreTag, CoreTagItem } from '@features/tag/services/tag';
Expand Down Expand Up @@ -59,7 +57,6 @@ export class AddonModBookContentsPage implements OnInit, OnDestroy {
initialChapterId?: number;
component = ADDON_MOD_BOOK_COMPONENT;
manager?: CoreSwipeSlidesItemsManager<LoadedChapter, AddonModBookSlidesItemsManagerSource>;
warning = '';
displayNavBar = true;
navigationItems: CoreNavigationBarItem<AddonModBookTocChapter>[] = [];
swiperOpts: CoreSwipeSlidesOptions = {
Expand Down Expand Up @@ -130,87 +127,21 @@ export class AddonModBookContentsPage implements OnInit, OnDestroy {
return;
}

const { module, book } = await source.loadBookData();

const downloadResult = await this.downloadResourceIfNeeded(module, refresh);
const { book } = await source.loadBookData();

this.displayNavBar = book.navstyle != AddonModBookNavStyle.TOC_ONLY;
this.title = book.name;

// Get contents. No need to refresh, it has been done in downloadResourceIfNeeded.
await source.loadContents();
await source.loadContents(refresh);

await source.load();

if (downloadResult?.failed) {
const error = CoreTextUtils.getErrorMessageFromError(downloadResult.error) || downloadResult.error;
this.warning = Translate.instant('core.errordownloadingsomefiles') + (error ? ' ' + error : '');
} else {
this.warning = '';
}
} catch (error) {
CoreDomUtils.showErrorModalDefault(error, 'core.course.errorgetmodule', true);
} finally {
this.loaded = true;
}
}

/**
* Download a resource if needed.
* If the download call fails the promise won't be rejected, but the error will be included in the returned object.
* If module.contents cannot be loaded then the Promise will be rejected.
*
* @param module Module to download.
* @param refresh Whether we're refreshing data.
* @returns Promise resolved when done.
*/
protected async downloadResourceIfNeeded(
module: CoreCourseModuleData,
refresh = false,
): Promise<CoreCourseResourceDownloadResult> {

const result: CoreCourseResourceDownloadResult = {
failed: false,
};
let contentsAlreadyLoaded = false;

// Get module status to determine if it needs to be downloaded.
const status = await CoreCourseModulePrefetchDelegate.getModuleStatus(module, this.courseId, undefined, refresh);

if (status !== DownloadStatus.DOWNLOADED) {
// Download content. This function also loads module contents if needed.
try {
await CoreCourseModulePrefetchDelegate.downloadModule(module, this.courseId);

// If we reach here it means the download process already loaded the contents, no need to do it again.
contentsAlreadyLoaded = true;
} catch (error) {
// Mark download as failed but go on since the main files could have been downloaded.
result.failed = true;
result.error = error;
}
}

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

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

return result;
}

/**
* Change the current chapter.
*
Expand Down Expand Up @@ -395,18 +326,48 @@ class AddonModBookSlidesItemsManagerSource extends CoreSwipeSlidesItemsManagerSo

/**
* Load module contents.
*
* @param refresh Whether we're refreshing data.
*/
async loadContents(): Promise<void> {
async loadContents(refresh = false): Promise<void> {
if (!this.module) {
return;
}

const contents = await CoreCourse.getModuleContents(this.module, this.COURSE_ID);
const contents = await this.getModuleContents(refresh);

this.contentsMap = AddonModBook.getContentsMap(contents);
this.chapters = AddonModBook.getTocList(contents);
}

/**
* Get module contents.
*
* @param refresh Whether we're refreshing data.
* @returns Module contents.
*/
protected async getModuleContents(refresh = false): Promise<CoreCourseModuleContentFile[]> {
if (!this.module) {
return [];
}

const ignoreCache = refresh && CoreNetwork.isOnline();

try {
return await CoreCourse.getModuleContents(this.module, this.COURSE_ID, 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;
}
}

/**
* @inheritdoc
*/
Expand Down

0 comments on commit d28f85a

Please sign in to comment.