Skip to content

Commit ed2c62e

Browse files
committed
Implement support for feed pagination.
1 parent dbcb02a commit ed2c62e

File tree

1 file changed

+33
-12
lines changed

1 file changed

+33
-12
lines changed

src/components/FeedPage.vue

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
<hr />
2626

27-
<LoadingIndicatorPage :show-content="videosStore != null" class="video-grid">
27+
<LoadingIndicatorPage :show-content="videos != null" class="video-grid">
2828
<template v-for="video in videos" :key="video.url">
2929
<VideoItem v-if="shouldShowVideo(video)" :is-feed="true" :item="video" />
3030
</template>
@@ -44,12 +44,11 @@ export default {
4444
},
4545
data() {
4646
return {
47-
currentVideoCount: 0,
48-
videoStep: 100,
49-
videosStore: null,
50-
videos: [],
47+
videos: null,
48+
videosCount: 0,
5149
availableFilters: ["all", "shorts", "videos"],
5250
selectedFilter: "all",
51+
loading: false,
5352
};
5453
},
5554
computed: {
@@ -60,16 +59,16 @@ export default {
6059
},
6160
mounted() {
6261
this.fetchFeed().then(videos => {
63-
this.videosStore = videos;
64-
this.loadMoreVideos();
62+
this.videos = videos;
63+
this.videosCount = videos.length;
6564
this.updateWatched(this.videos);
6665
});
6766
6867
this.selectedFilter = this.getPreferenceString("feedFilter") ?? "all";
6968
},
7069
activated() {
7170
document.title = this.$t("titles.feed") + " - Piped";
72-
if (this.videos.length > 0) this.updateWatched(this.videos);
71+
if (this.videos?.length > 0) this.updateWatched(this.videos);
7372
window.addEventListener("scroll", this.handleScroll);
7473
},
7574
deactivated() {
@@ -90,13 +89,35 @@ export default {
9089
});
9190
}
9291
},
93-
loadMoreVideos() {
94-
this.currentVideoCount = Math.min(this.currentVideoCount + this.videoStep, this.videosStore.length);
95-
if (this.videos.length != this.videosStore.length)
96-
this.videos = this.videosStore.slice(0, this.currentVideoCount);
92+
async loadMoreVideos() {
93+
const start = this.videos[this.videos.length - 1].uploaded;
94+
if (this.authenticated) {
95+
return await this.fetchJson(this.authApiUrl() + "/feed", {
96+
authToken: this.getAuthToken(),
97+
start,
98+
}).then(videos => {
99+
this.videos = this.videos.concat(videos);
100+
this.videosCount = videos.length > 0 ? this.videos.length : -1;
101+
this.loading = false;
102+
});
103+
} else {
104+
return await this.fetchJson(this.authApiUrl() + "/feed/unauthenticated", {
105+
channels: this.getUnauthenticatedChannels(),
106+
start,
107+
}).then(videos => {
108+
this.videos = this.videos.concat(videos);
109+
this.videosCount = videos.length > 0 ? this.videos.length : -1;
110+
this.loading = false;
111+
});
112+
}
97113
},
98114
handleScroll() {
99115
if (window.innerHeight + window.scrollY >= document.body.offsetHeight - window.innerHeight) {
116+
if (this.loading) return;
117+
if (this.videos == null) return;
118+
if (this.videosCount % 100 != 0) return;
119+
120+
this.loading = true;
100121
this.loadMoreVideos();
101122
}
102123
},

0 commit comments

Comments
 (0)