chapter deletion logic fixed (#3320)

(cherry picked from commit f642f23366ef352c7d107c8fd90a049033391504)
This commit is contained in:
Dimitri Rogava 2020-06-16 07:03:56 +04:00 committed by Jobobby04
parent 9cff728d13
commit 7e5bbb2cc1

View File

@ -362,7 +362,7 @@ class ReaderPresenter(
if (selectedChapter.pages?.lastIndex == page.index) {
selectedChapter.chapter.read = true
updateTrackChapterRead(selectedChapter)
enqueueDeleteReadChapters(selectedChapter)
deleteChapterIfNeeded(selectedChapter)
}
if (selectedChapter != currentChapters.currChapter) {
@ -372,6 +372,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.
@ -660,19 +676,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)
}
}