chapter deletion logic fixed (#3320)

This commit is contained in:
Dimitri Rogava 2020-06-16 07:03:56 +04:00 committed by GitHub
parent c041410f61
commit f642f23366
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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)
}
}