Browse Source

allow sorting by total chapters for library view (#811)

* allow sorting by total chapters for library view

* allow sorting by total chapters for library view

* Changed to remove query per manga.
Carlos 7 years ago
parent
commit
1089c25b8f

+ 3 - 0
app/src/main/java/eu/kanade/tachiyomi/data/database/queries/MangaQueries.kt

@@ -98,4 +98,7 @@ interface MangaQueries : DbProvider {
                     .observesTables(MangaTable.TABLE)
                     .build())
             .prepare()
+
+    fun getTotalChapterManga() = db.get().listOfObjects(Manga::class.java)
+            .withQuery(RawQuery.builder().query(getTotalChapterMangaQuery()).observesTables(MangaTable.TABLE).build()).prepare();
 }

+ 9 - 0
app/src/main/java/eu/kanade/tachiyomi/data/database/queries/RawQueries.kt

@@ -93,6 +93,15 @@ fun getLastReadMangaQuery() = """
     ORDER BY max DESC
 """
 
+fun getTotalChapterMangaQuery()= """
+    SELECT ${Manga.TABLE}.*
+    FROM ${Manga.TABLE}
+    JOIN ${Chapter.TABLE}
+    ON ${Manga.TABLE}.${Manga.COL_ID} = ${Chapter.TABLE}.${Chapter.COL_MANGA_ID}
+    GROUP BY ${Manga.TABLE}.${Manga.COL_ID}
+    ORDER by COUNT(*)
+"""
+
 /**
  * Query to get the categories for a manga.
  */

+ 5 - 1
app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryNavigationView.kt

@@ -105,13 +105,15 @@ class LibraryNavigationView @JvmOverloads constructor(context: Context, attrs: A
 
         private val alphabetically = Item.MultiSort(R.string.action_sort_alpha, this)
 
+        private val total = Item.MultiSort(R.string.action_sort_total, this)
+
         private val lastRead = Item.MultiSort(R.string.action_sort_last_read, this)
 
         private val lastUpdated = Item.MultiSort(R.string.action_sort_last_updated, this)
 
         private val unread = Item.MultiSort(R.string.action_filter_unread, this)
 
-        override val items = listOf(alphabetically, lastRead, lastUpdated, unread)
+        override val items = listOf(alphabetically, lastRead, lastUpdated, unread, total)
 
         override val header = Item.Header(R.string.action_sort)
 
@@ -126,6 +128,7 @@ class LibraryNavigationView @JvmOverloads constructor(context: Context, attrs: A
             lastRead.state = if (sorting == LibrarySort.LAST_READ) order else SORT_NONE
             lastUpdated.state = if (sorting == LibrarySort.LAST_UPDATED) order else SORT_NONE
             unread.state = if (sorting == LibrarySort.UNREAD) order else SORT_NONE
+            total.state = if (sorting == LibrarySort.TOTAL) order else SORT_NONE
         }
 
         override fun onItemClicked(item: Item) {
@@ -145,6 +148,7 @@ class LibraryNavigationView @JvmOverloads constructor(context: Context, attrs: A
                 lastRead -> LibrarySort.LAST_READ
                 lastUpdated -> LibrarySort.LAST_UPDATED
                 unread -> LibrarySort.UNREAD
+                total -> LibrarySort.TOTAL
                 else -> throw Exception("Unknown sorting")
             })
             preferences.librarySortingAscending().set(if (item.state == SORT_ASC) true else false)

+ 9 - 0
app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryPresenter.kt

@@ -146,6 +146,10 @@ class LibraryPresenter(
             var counter = 0
             db.getLastReadManga().executeAsBlocking().associate { it.id!! to counter++ }
         }
+        val totalChapterManga by lazy {
+            var counter = 0
+            db.getTotalChapterManga().executeAsBlocking().associate { it.id!! to counter++ }
+        }
 
         val sortFn: (Manga, Manga) -> Int = { manga1, manga2 ->
             when (sortingMode) {
@@ -158,6 +162,11 @@ class LibraryPresenter(
                 }
                 LibrarySort.LAST_UPDATED -> manga2.last_update.compareTo(manga1.last_update)
                 LibrarySort.UNREAD -> manga1.unread.compareTo(manga2.unread)
+                LibrarySort.TOTAL -> {
+                    val manga1TotalChapter = totalChapterManga[manga1.id!!] ?: 0
+                    val mange2TotalChapter = totalChapterManga[manga2.id!!] ?: 0
+                    manga1TotalChapter.compareTo(mange2TotalChapter)
+                }
                 else -> throw Exception("Unknown sorting mode")
             }
         }

+ 1 - 1
app/src/main/java/eu/kanade/tachiyomi/ui/library/LibrarySort.kt

@@ -6,5 +6,5 @@ object LibrarySort {
     const val LAST_READ = 1
     const val LAST_UPDATED = 2
     const val UNREAD = 3
-
+    const val TOTAL = 4
 }

+ 1 - 0
app/src/main/res/values/strings.xml

@@ -30,6 +30,7 @@
     <string name="action_filter_read">Read</string>
     <string name="action_filter_empty">Remove filter</string>
     <string name="action_sort_alpha">Alphabetically</string>
+    <string name="action_sort_total">Total chapters</string>
     <string name="action_sort_last_read">Last read</string>
     <string name="action_sort_last_updated">Last updated</string>
     <string name="action_search">Search</string>