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 1f93754e8..c42831f5b 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 @@ -361,7 +361,7 @@ class ReaderPresenter( if (selectedChapter.pages?.lastIndex == page.index) { selectedChapter.chapter.read = true updateTrackChapterRead(selectedChapter) - enqueueDeleteReadChapters(selectedChapter) + deleteChapterIfNeeded(selectedChapter) } if (selectedChapter != currentChapters.currChapter) { @@ -371,6 +371,22 @@ class ReaderPresenter( } } + /** + * 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. @@ -653,19 +669,8 @@ class ReaderPresenter( if (!chapter.chapter.read || chapter.pageLoader !is DownloadPageLoader) return val manga = manga ?: return - // Return if the setting is disabled - val removeAfterReadSlots = preferences.removeAfterReadSlots() - if (removeAfterReadSlots == -1) return - launchIO { - // 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) } }