From d28f85a0c9a12b1263afa5139a787b9b1c3486dd Mon Sep 17 00:00:00 2001 From: Dani Palou Date: Wed, 7 Aug 2024 15:09:34 +0200 Subject: [PATCH] MOBILE-4627 book: Don't download all files when opening --- .../mod/book/pages/contents/contents.html | 7 -- .../mod/book/pages/contents/contents.ts | 109 ++++++------------ 2 files changed, 35 insertions(+), 81 deletions(-) diff --git a/src/addons/mod/book/pages/contents/contents.html b/src/addons/mod/book/pages/contents/contents.html index d540f7b94..22501f112 100644 --- a/src/addons/mod/book/pages/contents/contents.html +++ b/src/addons/mod/book/pages/contents/contents.html @@ -22,13 +22,6 @@
- - - - -
diff --git a/src/addons/mod/book/pages/contents/contents.ts b/src/addons/mod/book/pages/contents/contents.ts index 39b975103..141d51dc6 100644 --- a/src/addons/mod/book/pages/contents/contents.ts +++ b/src/addons/mod/book/pages/contents/contents.ts @@ -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'; @@ -59,7 +57,6 @@ export class AddonModBookContentsPage implements OnInit, OnDestroy { initialChapterId?: number; component = ADDON_MOD_BOOK_COMPONENT; manager?: CoreSwipeSlidesItemsManager; - warning = ''; displayNavBar = true; navigationItems: CoreNavigationBarItem[] = []; swiperOpts: CoreSwipeSlidesOptions = { @@ -130,24 +127,14 @@ 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 { @@ -155,62 +142,6 @@ export class AddonModBookContentsPage implements OnInit, OnDestroy { } } - /** - * 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 { - - 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. * @@ -395,18 +326,48 @@ class AddonModBookSlidesItemsManagerSource extends CoreSwipeSlidesItemsManagerSo /** * Load module contents. + * + * @param refresh Whether we're refreshing data. */ - async loadContents(): Promise { + async loadContents(refresh = false): Promise { 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 { + 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 */