From 8e05c669db817ee0cd4dd881979c61f46779baf6 Mon Sep 17 00:00:00 2001 From: X4Lo Date: Mon, 27 Jan 2025 07:04:48 +0100 Subject: [PATCH] Implemented load next on scroll for grid view --- .../library-page/library-page.component.html | 12 ++-- .../library-page/library-page.component.ts | 62 ++++++++++++++++--- 2 files changed, 60 insertions(+), 14 deletions(-) diff --git a/frontend/src/app/features/library/library-page/library-page.component.html b/frontend/src/app/features/library/library-page/library-page.component.html index 4eb44c9..d92f361 100644 --- a/frontend/src/app/features/library/library-page/library-page.component.html +++ b/frontend/src/app/features/library/library-page/library-page.component.html @@ -108,12 +108,12 @@
Total entries {{entriesCount}}
-
+
  • Previous
  • -
  • {{ page }}
  • @@ -127,20 +127,20 @@ - - -
    +
    • Previous
    • -
    • {{ page }}
    • diff --git a/frontend/src/app/features/library/library-page/library-page.component.ts b/frontend/src/app/features/library/library-page/library-page.component.ts index 34f4af1..62b624f 100644 --- a/frontend/src/app/features/library/library-page/library-page.component.ts +++ b/frontend/src/app/features/library/library-page/library-page.component.ts @@ -5,6 +5,8 @@ import { ActivatedRoute, Event, Router } from '@angular/router'; import { LibraryQueryParams } from '../../../shared/models/library-query-params'; import { SettingsService } from '../../../core/services/settings.service'; import { Settings } from '../../../shared/models/settings.model'; +import { ScrollService } from '../../../core/services/scroll.service'; +import { Subscription } from 'rxjs'; declare var $: any; @Component({ @@ -14,7 +16,8 @@ declare var $: any; }) export class LibraryPageComponent implements OnInit, AfterViewInit { entries: Entry[] = []; - entriesToShow: Entry[] = []; + entriesList: Entry[] = []; + entriesToDisplay: Entry[] = []; // ? pagination entriesCount: number = 0; @@ -76,17 +79,20 @@ export class LibraryPageComponent implements OnInit, AfterViewInit { matchAll: true, }; + private scrollSubscription!: Subscription; + constructor( private router: Router, private route: ActivatedRoute, private libraryService: LibraryService, - private settingsService: SettingsService + private settingsService: SettingsService, + private scrollService: ScrollService ) {} async ngOnInit() { this.getDefaultViewMode(); - this.pageSize = await this.getDefaultPageSize(); + this.pageSize = await this.getDefaultTableSize(); await this.getEntries(true); // this.router.events.subscribe((event) => { // if (event instanceof NavigationEnd && event.url === '/library') { @@ -94,6 +100,12 @@ export class LibraryPageComponent implements OnInit, AfterViewInit { // this.getEntries(true); // } // }); + + this.scrollSubscription = this.scrollService.scrollEnd$.subscribe(() => { + if (this.viewMode == 'grid') { + this.loadNext(); + } + }); } ngAfterViewInit() { @@ -119,6 +131,13 @@ export class LibraryPageComponent implements OnInit, AfterViewInit { // $('#genre-select').val(this.filterCriteria.genres) } + ngOnDestroy(): void { + // Unsubscribe to avoid memory leaks + if (this.scrollSubscription) { + this.scrollSubscription.unsubscribe(); + } + } + // ? uses libraryService to get library all entries getEntries(forceFetch: boolean = false) { this.libraryService.newGetAllEntries().subscribe({ @@ -325,18 +344,40 @@ export class LibraryPageComponent implements OnInit, AfterViewInit { // console.log(temp); // ? sort filter - temp = await this.sortEntries(temp); + this.entriesList = await this.sortEntries(temp); - this.entriesCount = temp.length; + this.entriesCount = this.entriesList.length; this.maxPage = Math.ceil(this.entriesCount / this.pageSize); if (this.currentPage > this.maxPage) this.currentPage = this.maxPage; // fix for when 1 entry in the last page is removed + this.entriesToDisplay = []; + + if (this.viewMode == 'table') { + this.getPageContent(); + } else if (this.viewMode == 'grid') { + this.loadNext(); + } + } + // + FILTER END + + private loadNext(): void { + const nextItems = this.entriesList.slice( + this.entriesToDisplay.length, + Math.min(this.entriesToDisplay.length + 30, this.entriesList.length) + ); + if (nextItems.length > 0) { + this.entriesToDisplay = [...this.entriesToDisplay, ...nextItems]; + } + } + + private getPageContent(): void { const startIndex = (this.currentPage - 1) * this.pageSize; const endIndex = startIndex + this.pageSize; - this.entriesToShow = temp.slice(startIndex, endIndex); + console.log(this.pageSize, startIndex, endIndex); + + this.entriesToDisplay = this.entriesList.slice(startIndex, endIndex); } - // + FILTER END // ! sorting sortEntries(entries: Entry[]) { @@ -438,9 +479,14 @@ export class LibraryPageComponent implements OnInit, AfterViewInit { return 54; } + getDefaultTableSize() { + // TODO get default page size from settings service + return 10; + } + goToPage(page: number) { this.currentPage = page; - this.filterEntries(); + this.getPageContent(); this.setQueryParameters(); // * update url query }