MOBILE-4362 activity: Infinite loading on list mod type page

This commit is contained in:
Pau Ferrer Ocaña 2023-10-17 11:40:40 +02:00
parent f986937424
commit 0e31b8214a
4 changed files with 44 additions and 19 deletions

View File

@ -22,7 +22,7 @@ const THRESHOLD = .15; // % of the scroll element height that must be close to t
* Component to show a infinite loading trigger and spinner while more data is being loaded. * Component to show a infinite loading trigger and spinner while more data is being loaded.
* *
* Usage: * Usage:
* <core-infinite-loading [action]="loadingAction" [enabled]="dataLoaded"></core-inifinite-loading> * <core-infinite-loading [action]="loadingAction" [enabled]="dataLoaded"></core-infinite-loading>
*/ */
@Component({ @Component({
selector: 'core-infinite-loading', selector: 'core-infinite-loading',

View File

@ -298,7 +298,7 @@ export class CoreCourseContentsPage implements OnInit, OnDestroy, CoreRefreshCon
await this.loadData(true, true); await this.loadData(true, true);
} finally { } finally {
// Do not call doRefresh on the format component if the refresher is defined in the format component // Do not call doRefresh on the format component if the refresher is defined in the format component
// to prevent an inifinite loop. // to prevent an infinite loop.
if (this.displayRefresher && this.formatComponent) { if (this.displayRefresher && this.formatComponent) {
await CoreUtils.ignoreErrors(this.formatComponent.doRefresh(refresher)); await CoreUtils.ignoreErrors(this.formatComponent.doRefresh(refresher));
} }

View File

@ -17,7 +17,8 @@
</core-empty-box> </core-empty-box>
<ion-list class="core-course-module-list-wrapper"> <ion-list class="core-course-module-list-wrapper">
<ng-container *ngFor="let section of sections"> <ng-container *ngFor="let section of sections; index as i">
<ng-container *ngIf="i <= lastShownSectionIndex">
<ion-item-divider class="course-section ion-text-wrap" *ngIf="section.name"> <ion-item-divider class="course-section ion-text-wrap" *ngIf="section.name">
<ion-label> <ion-label>
<h2> <h2>
@ -32,6 +33,8 @@
</core-course-module> </core-course-module>
</ng-container> </ng-container>
</ng-container> </ng-container>
</ng-container>
</ion-list> </ion-list>
<core-infinite-loading [enabled]="canLoadMore" (action)="showMoreActivities($event)"></core-infinite-loading>
</core-loading> </core-loading>
</ion-content> </ion-content>

View File

@ -34,10 +34,14 @@ import { CoreAnalytics, CoreAnalyticsEventType } from '@services/analytics';
}) })
export class CoreCourseListModTypePage implements OnInit { export class CoreCourseListModTypePage implements OnInit {
private static readonly PAGE_LENGTH = 10; // How many activities should load each time showMoreActivities is called.
sections: CoreCourseSection[] = []; sections: CoreCourseSection[] = [];
title = ''; title = '';
loaded = false; loaded = false;
courseId = 0; courseId = 0;
canLoadMore = false;
lastShownSectionIndex = -1;
protected modName?: string; protected modName?: string;
protected archetypes: Record<string, number> = {}; // To speed up the check of modules. protected archetypes: Record<string, number> = {}; // To speed up the check of modules.
@ -84,8 +88,6 @@ export class CoreCourseListModTypePage implements OnInit {
/** /**
* Fetches the data. * Fetches the data.
*
* @returns Resolved when done.
*/ */
protected async fetchData(): Promise<void> { protected async fetchData(): Promise<void> {
if (!this.courseId) { if (!this.courseId) {
@ -97,7 +99,7 @@ export class CoreCourseListModTypePage implements OnInit {
let sections = await CoreCourse.getSections(this.courseId, false, true); let sections = await CoreCourse.getSections(this.courseId, false, true);
sections = sections.filter((section) => { sections = sections.filter((section) => {
if (!section.modules) { if (!section.modules.length || section.hiddenbynumsections) {
return false; return false;
} }
@ -134,16 +136,36 @@ export class CoreCourseListModTypePage implements OnInit {
const result = await CoreCourseHelper.addHandlerDataForModules(sections, this.courseId); const result = await CoreCourseHelper.addHandlerDataForModules(sections, this.courseId);
this.sections = result.sections; this.sections = result.sections;
this.lastShownSectionIndex = -1;
this.showMoreActivities();
} catch (error) { } catch (error) {
CoreDomUtils.showErrorModalDefault(error, 'Error getting data'); CoreDomUtils.showErrorModalDefault(error, 'Error getting data');
} }
} }
/**
* Show more activities.
*
* @param infiniteComplete Infinite scroll complete function. Only used from core-infinite-loading.
*/
showMoreActivities(infiniteComplete?: () => void): void {
let modulesLoaded = 0;
while (this.lastShownSectionIndex < this.sections.length - 1 && modulesLoaded < CoreCourseListModTypePage.PAGE_LENGTH) {
this.lastShownSectionIndex++;
modulesLoaded += this.sections[this.lastShownSectionIndex].modules.length;
}
this.canLoadMore = this.lastShownSectionIndex < this.sections.length - 1;
infiniteComplete?.();
}
/** /**
* Refresh the data. * Refresh the data.
* *
* @param refresher Refresher. * @param refresher Refresher.
* @returns Promise resolved when done.
*/ */
async refreshData(refresher: IonRefresher): Promise<void> { async refreshData(refresher: IonRefresher): Promise<void> {
await CoreUtils.ignoreErrors(CoreCourse.invalidateSections(this.courseId)); await CoreUtils.ignoreErrors(CoreCourse.invalidateSections(this.courseId));