From d308e0c29fca8319704a336b7e16628c4088bca5 Mon Sep 17 00:00:00 2001 From: Carlos <2092019+CarlosEsco@users.noreply.github.com> Date: Sun, 9 Aug 2020 15:04:21 -0400 Subject: [PATCH] update nth chapter logic from stable (#504) Co-authored-by: Jays2Kings --- .../tachiyomi/ui/reader/ReaderPresenter.kt | 34 +++++++++++-------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderPresenter.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderPresenter.kt index be31699039..afc61aea30 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderPresenter.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderPresenter.kt @@ -23,7 +23,6 @@ import eu.kanade.tachiyomi.source.online.HttpSource import eu.kanade.tachiyomi.ui.base.presenter.BasePresenter import eu.kanade.tachiyomi.ui.reader.chapter.ReaderChapterItem import eu.kanade.tachiyomi.ui.reader.loader.ChapterLoader -import eu.kanade.tachiyomi.ui.reader.loader.DownloadPageLoader import eu.kanade.tachiyomi.ui.reader.model.ReaderChapter import eu.kanade.tachiyomi.ui.reader.model.ReaderPage import eu.kanade.tachiyomi.ui.reader.model.ViewerChapters @@ -370,7 +369,7 @@ class ReaderPresenter( if (selectedChapter.pages?.lastIndex == page.index) { selectedChapter.chapter.read = true updateTrackChapterRead(selectedChapter) - enqueueDeleteReadChapters(selectedChapter) + deleteChapterIfNeeded(selectedChapter) } if (selectedChapter != currentChapters.currChapter) { @@ -382,6 +381,22 @@ class ReaderPresenter( return false } + /** + * Determines if deleting option is enabled and nth to last chapter actually exists. + * If both conditions are satisfied enqueues chapter for delete + * @param currentChapter current chapter, which is going to be marked as read. + */ + private fun deleteChapterIfNeeded(currentChapter: ReaderChapter) { + // Determine which chapter should be deleted and enqueue + val currentChapterPosition = chapterList.indexOf(currentChapter) + val removeAfterReadSlots = preferences.removeAfterReadSlots() + val chapterToDelete = chapterList.getOrNull(currentChapterPosition - removeAfterReadSlots) + // Check if deleting option is enabled and chapter exists + if (removeAfterReadSlots != -1 && chapterToDelete != null) { + enqueueDeleteReadChapters(chapterToDelete) + } + } + /** * Called when a chapter changed from [fromChapter] to [toChapter]. It updates [fromChapter] * on the database. @@ -634,23 +649,12 @@ class ReaderPresenter( * manager handles persisting it across process deaths. */ private fun enqueueDeleteReadChapters(chapter: ReaderChapter) { - if (!chapter.chapter.read || chapter.pageLoader !is DownloadPageLoader) return + if (!chapter.chapter.read) return val manga = manga ?: return - // Return if the setting is disabled - val removeAfterReadSlots = preferences.removeAfterReadSlots() - if (removeAfterReadSlots == -1) return - Completable .fromCallable { - // Position of the read chapter - val position = chapterList.indexOf(chapter) - - // Retrieve chapter to delete according to preference - val chapterToDelete = chapterList.getOrNull(position - removeAfterReadSlots) - if (chapterToDelete != null) { - downloadManager.enqueueDeleteChapters(listOf(chapterToDelete.chapter), manga) - } + downloadManager.enqueueDeleteChapters(listOf(chapter.chapter), manga) } .onErrorComplete() .subscribeOn(Schedulers.io())