From af0cf9e52d47f4c14e8ccf3864fc4b322876673f Mon Sep 17 00:00:00 2001 From: len Date: Fri, 20 May 2016 19:34:15 +0200 Subject: [PATCH] Load next/prev chapter depending on the sorting method --- .../data/database/queries/ChapterQueries.kt | 26 ++++++++++++++++++- .../tachiyomi/ui/reader/ReaderPresenter.kt | 17 ++++++++---- 2 files changed, 37 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/database/queries/ChapterQueries.kt b/app/src/main/java/eu/kanade/tachiyomi/data/database/queries/ChapterQueries.kt index 1f340dceca..8251c04bb2 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/database/queries/ChapterQueries.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/database/queries/ChapterQueries.kt @@ -53,6 +53,18 @@ interface ChapterQueries : DbProvider { .prepare() } + fun getNextChapterBySource(chapter: Chapter) = db.get() + .`object`(Chapter::class.java) + .withQuery(Query.builder() + .table(ChapterTable.TABLE) + .where("""${ChapterTable.COL_MANGA_ID} = ? AND + ${ChapterTable.COL_SOURCE_ORDER} < ?""") + .whereArgs(chapter.manga_id, chapter.source_order) + .orderBy("${ChapterTable.COL_SOURCE_ORDER} DESC") + .limit(1) + .build()) + .prepare() + fun getPreviousChapter(chapter: Chapter): PreparedGetObject { // Add a delta to the chapter number, because binary decimal representation // can retrieve the same chapter again @@ -65,12 +77,24 @@ interface ChapterQueries : DbProvider { "${ChapterTable.COL_CHAPTER_NUMBER} < ? AND " + "${ChapterTable.COL_CHAPTER_NUMBER} >= ?") .whereArgs(chapter.manga_id, chapterNumber, chapterNumber - 1) - .orderBy(ChapterTable.COL_CHAPTER_NUMBER + " DESC") + .orderBy("${ChapterTable.COL_CHAPTER_NUMBER} DESC") .limit(1) .build()) .prepare() } + fun getPreviousChapterBySource(chapter: Chapter) = db.get() + .`object`(Chapter::class.java) + .withQuery(Query.builder() + .table(ChapterTable.TABLE) + .where("""${ChapterTable.COL_MANGA_ID} = ? AND + ${ChapterTable.COL_SOURCE_ORDER} > ?""") + .whereArgs(chapter.manga_id, chapter.source_order) + .orderBy(ChapterTable.COL_SOURCE_ORDER) + .limit(1) + .build()) + .prepare() + fun getNextUnreadChapter(manga: Manga) = db.get() .`object`(Chapter::class.java) .withQuery(Query.builder() 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 92aa7b6721..1e93f2200b 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 @@ -1,7 +1,6 @@ package eu.kanade.tachiyomi.ui.reader import android.os.Bundle -import android.util.Pair import eu.kanade.tachiyomi.data.cache.ChapterCache import eu.kanade.tachiyomi.data.database.DatabaseHelper import eu.kanade.tachiyomi.data.database.models.Chapter @@ -180,10 +179,8 @@ class ReaderPresenter : BasePresenter() { } private fun getAdjacentChaptersObservable(): Observable> { - return Observable.zip( - db.getPreviousChapter(chapter).asRxObservable().take(1), - db.getNextChapter(chapter).asRxObservable().take(1), - { a, b -> Pair.create(a, b) }) + val strategy = getAdjacentChaptersStrategy() + return Observable.zip(strategy.first, strategy.second) { prev, next -> Pair(prev, next) } .doOnNext { pair -> previousChapter = pair.first nextChapter = pair.second @@ -191,6 +188,16 @@ class ReaderPresenter : BasePresenter() { .observeOn(AndroidSchedulers.mainThread()) } + private fun getAdjacentChaptersStrategy() = when (manga.sorting) { + Manga.SORTING_NUMBER -> Pair( + db.getPreviousChapter(chapter).asRxObservable().take(1), + db.getNextChapter(chapter).asRxObservable().take(1)) + Manga.SORTING_SOURCE -> Pair( + db.getPreviousChapterBySource(chapter).asRxObservable().take(1), + db.getNextChapterBySource(chapter).asRxObservable().take(1)) + else -> throw AssertionError("Unknown sorting method") + } + // Preload the first pages of the next chapter. Only for non seamless mode private fun getPreloadNextChapterObservable(): Observable { return source.getCachedPageListOrPullFromNetwork(nextChapter!!.url)