Merge pull request #4144 from dpalou/MOBILE-4627

Mobile 4627
main
Pau Ferrer Ocaña 2024-08-13 13:43:35 +02:00 committed by GitHub
commit 40b96b9e63
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 72 additions and 116 deletions

View File

@ -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

View File

@ -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

View File

@ -22,13 +22,6 @@
<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">

View File

@ -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<LoadedChapter, AddonModBookSlidesItemsManagerSource>;
warning = '';
displayNavBar = true;
navigationItems: CoreNavigationBarItem<AddonModBookTocChapter>[] = [];
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<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.
*
@ -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
*/

View File

@ -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" />

View File

@ -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';
@ -38,7 +37,6 @@ export class AddonModPageIndexComponent extends CoreCourseModuleMainResourceComp
displayTimemodified = true;
timemodified?: number;
page?: AddonModPagePage;
warning?: string;
protected fetchContentDefaultError = 'addon.mod_page.errorwhileloadingthepage';
@ -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);
}
/**

View File

@ -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';
@ -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.
*