update nth chapter logic from stable (#504)

Co-authored-by: Jays2Kings <jays@outlook.com>
This commit is contained in:
Carlos 2020-08-09 15:04:21 -04:00 committed by GitHub
parent 90ca4ecf53
commit d308e0c29f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -23,7 +23,6 @@ import eu.kanade.tachiyomi.source.online.HttpSource
import eu.kanade.tachiyomi.ui.base.presenter.BasePresenter import eu.kanade.tachiyomi.ui.base.presenter.BasePresenter
import eu.kanade.tachiyomi.ui.reader.chapter.ReaderChapterItem import eu.kanade.tachiyomi.ui.reader.chapter.ReaderChapterItem
import eu.kanade.tachiyomi.ui.reader.loader.ChapterLoader 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.ReaderChapter
import eu.kanade.tachiyomi.ui.reader.model.ReaderPage import eu.kanade.tachiyomi.ui.reader.model.ReaderPage
import eu.kanade.tachiyomi.ui.reader.model.ViewerChapters import eu.kanade.tachiyomi.ui.reader.model.ViewerChapters
@ -370,7 +369,7 @@ class ReaderPresenter(
if (selectedChapter.pages?.lastIndex == page.index) { if (selectedChapter.pages?.lastIndex == page.index) {
selectedChapter.chapter.read = true selectedChapter.chapter.read = true
updateTrackChapterRead(selectedChapter) updateTrackChapterRead(selectedChapter)
enqueueDeleteReadChapters(selectedChapter) deleteChapterIfNeeded(selectedChapter)
} }
if (selectedChapter != currentChapters.currChapter) { if (selectedChapter != currentChapters.currChapter) {
@ -382,6 +381,22 @@ class ReaderPresenter(
return false 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] * Called when a chapter changed from [fromChapter] to [toChapter]. It updates [fromChapter]
* on the database. * on the database.
@ -634,23 +649,12 @@ class ReaderPresenter(
* manager handles persisting it across process deaths. * manager handles persisting it across process deaths.
*/ */
private fun enqueueDeleteReadChapters(chapter: ReaderChapter) { private fun enqueueDeleteReadChapters(chapter: ReaderChapter) {
if (!chapter.chapter.read || chapter.pageLoader !is DownloadPageLoader) return if (!chapter.chapter.read) return
val manga = manga ?: return val manga = manga ?: return
// Return if the setting is disabled
val removeAfterReadSlots = preferences.removeAfterReadSlots()
if (removeAfterReadSlots == -1) return
Completable Completable
.fromCallable { .fromCallable {
// Position of the read chapter downloadManager.enqueueDeleteChapters(listOf(chapter.chapter), manga)
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)
}
} }
.onErrorComplete() .onErrorComplete()
.subscribeOn(Schedulers.io()) .subscribeOn(Schedulers.io())