Skip to content

Commit

Permalink
Merge pull request #4144 from dpalou/MOBILE-4627
Browse files Browse the repository at this point in the history
Mobile 4627
  • Loading branch information
crazyserver authored Aug 13, 2024
2 parents 79ba031 + dbfbe20 commit 40b96b9
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 116 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/acceptance.yml
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,10 @@ jobs:
echo $(cd ci/bin; pwd) >> $GITHUB_PATH
echo $(cd ci/vendor/bin; pwd) >> $GITHUB_PATH
sudo locale-gen en_AU.UTF-8
echo "NVM_DIR=$HOME/.nvm" >> $GITHUB_ENV
# Install nvm v0.39.7 as a temporary workaround for issue:
# https://github.com/moodlehq/moodle-plugin-ci/issues/309
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
- name: Install Behat Snapshots plugin
run: moodle-plugin-ci add-plugin moodlemobile/moodle-local_behatsnapshots
Expand Down
5 changes: 4 additions & 1 deletion .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,10 @@ jobs:
echo $(cd ci/bin; pwd) >> $GITHUB_PATH
echo $(cd ci/vendor/bin; pwd) >> $GITHUB_PATH
sudo locale-gen en_AU.UTF-8
echo "NVM_DIR=$HOME/.nvm" >> $GITHUB_ENV
# Install nvm v0.39.7 as a temporary workaround for issue:
# https://github.com/moodlehq/moodle-plugin-ci/issues/309
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
- name: Install Behat Snapshots plugin
run: moodle-plugin-ci add-plugin moodlemobile/moodle-local_behatsnapshots
Expand Down
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
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 40b96b9

Please sign in to comment.