Skip to content

Commit

Permalink
Fetch chapter pages everytime unless chapter is downloaded (#439)
Browse files Browse the repository at this point in the history
In case the chapter is not downloaded, the chapters pages have to be always fetched, since there is a possibility, that the chapter data is so old, that the available pages data is outdated.
  • Loading branch information
schroda authored Nov 4, 2023
1 parent a6cc757 commit 2c35808
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions src/screens/Reader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ export function Reader() {
const { data: chapterData, loading: isChapterLoading } = requestManager.useGetMangaChapter(mangaId, chapterIndex, {
skip: isChapterLoaded,
});
const [arePagesUpdated, setArePagesUpdated] = useState(false);

const getLoadedChapter = () => {
const isAChapterLoaded = loadedChapter.current;
Expand All @@ -127,6 +128,10 @@ export function Reader() {
return loadedChapter.current;
}

if (arePagesUpdated) {
setArePagesUpdated(false);
}

if (chapterData?.chapter) {
return chapterData.chapter;
}
Expand All @@ -136,15 +141,21 @@ export function Reader() {
loadedChapter.current = getLoadedChapter();

const chapter = loadedChapter.current ?? initialChapter;
const [fetchPages, { loading: areChapterPagesLoading }] = requestManager.useGetChapterPagesFetch(chapter.id);
const [fetchPages] = requestManager.useGetChapterPagesFetch(chapter.id);

useEffect(() => {
if (!isChapterLoading && chapter.pageCount === -1) {
fetchPages();
const reCheckPages = !chapter.isDownloaded || chapter.pageCount === -1;
const shouldFetchPages = !isChapterLoading && reCheckPages;
if (shouldFetchPages) {
fetchPages().then(() => setArePagesUpdated(true));
}

if (!reCheckPages && !arePagesUpdated) {
setArePagesUpdated(true);
}
}, [chapter.id]);

const isLoading = isChapterLoading || areChapterPagesLoading || chapter.pageCount === -1;
const isLoading = isChapterLoading || !arePagesUpdated;
const [wasLastPageReadSet, setWasLastPageReadSet] = useState(false);
const [curPage, setCurPage] = useState<number>(0);
const [pageToScrollTo, setPageToScrollTo] = useState<number | undefined>(undefined);
Expand Down Expand Up @@ -276,8 +287,7 @@ export function Reader() {
}
}, [chapter.sourceOrder, manga.id, settings.skipDupChapters]);

// return spinner while chpater data is loading
if (chapter.pageCount === -1) {
if (isLoading) {
return (
<Box
sx={{
Expand Down

0 comments on commit 2c35808

Please sign in to comment.