From 7caea4e5262ecbd42a06a37de57623a2cafa9b61 Mon Sep 17 00:00:00 2001 From: schroda <50052685+schroda@users.noreply.github.com> Date: Mon, 12 Feb 2024 20:53:53 +0100 Subject: [PATCH] Download ahead only in case current and next chapter are downloaded --- src/screens/Reader.tsx | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/src/screens/Reader.tsx b/src/screens/Reader.tsx index 25a79aae..f0c4a003 100644 --- a/src/screens/Reader.tsx +++ b/src/screens/Reader.tsx @@ -188,6 +188,16 @@ export function Reader() { const { settings: metadataSettings } = useMetadataServerSettings(); const updateChapter = (patch: UpdateChapterPatchInput) => { + const getChapterFromCache = (id: number) => + requestManager.graphQLClient.client.cache.readFragment({ + id: requestManager.graphQLClient.client.cache.identify({ + __typename: 'ChapterType', + id, + }), + fragment: FULL_CHAPTER_FIELDS, + fragmentName: 'FULL_CHAPTER_FIELDS', + }); + const isAutoDeletionEnabled = !!patch.isRead && !!metadataSettings.deleteChaptersWhileReading; const getChapterIdToDelete = () => { @@ -204,14 +214,7 @@ export function Reader() { return -1; } - const chapterToDeleteUpToDateData = requestManager.graphQLClient.client.cache.readFragment({ - id: requestManager.graphQLClient.client.cache.identify({ - __typename: 'ChapterType', - id: chapterToDelete.id, - }), - fragment: FULL_CHAPTER_FIELDS, - fragmentName: 'FULL_CHAPTER_FIELDS', - }); + const chapterToDeleteUpToDateData = getChapterFromCache(chapterToDelete.id); const shouldDeleteChapter = chapterToDeleteUpToDateData?.isDownloaded && @@ -223,8 +226,17 @@ export function Reader() { return chapterToDelete.id; }; + const currentChapter = getChapterFromCache(chapter.id); + const nextChapterId = mangaChapters?.[Number(chapterIndex) + 1]?.id; + const nextChapter = nextChapterId ? getChapterFromCache(nextChapterId) : null; + const shouldDownloadAhead = - chapter.manga.inLibrary && !chapter.isRead && !!patch.isRead && isDownloadAheadEnabled; + isDownloadAheadEnabled && + chapter.manga.inLibrary && + !chapter.isRead && + !!patch.isRead && + !!currentChapter?.isDownloaded && + !!nextChapter?.isDownloaded; requestManager .updateChapter(chapter.id, {