Browse Source

Load next/prev chapter depending on the sorting method

len 9 years ago
parent
commit
af0cf9e52d

+ 25 - 1
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<Chapter> {
         // 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()

+ 12 - 5
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<ReaderActivity>() {
     }
 
     private fun getAdjacentChaptersObservable(): Observable<Pair<Chapter, Chapter>> {
-        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<ReaderActivity>() {
                 .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<Page> {
         return source.getCachedPageListOrPullFromNetwork(nextChapter!!.url)