From 1089c25b8fc9daabf1891633825349fc39c287b6 Mon Sep 17 00:00:00 2001 From: Carlos Date: Wed, 31 May 2017 15:50:21 -0400 Subject: [PATCH] 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. --- .../tachiyomi/data/database/queries/MangaQueries.kt | 3 +++ .../kanade/tachiyomi/data/database/queries/RawQueries.kt | 9 +++++++++ .../kanade/tachiyomi/ui/library/LibraryNavigationView.kt | 6 +++++- .../eu/kanade/tachiyomi/ui/library/LibraryPresenter.kt | 9 +++++++++ .../java/eu/kanade/tachiyomi/ui/library/LibrarySort.kt | 2 +- app/src/main/res/values/strings.xml | 1 + 6 files changed, 28 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/database/queries/MangaQueries.kt b/app/src/main/java/eu/kanade/tachiyomi/data/database/queries/MangaQueries.kt index 33eb06558d..ed5ce5e8ba 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/database/queries/MangaQueries.kt +++ b/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(); } \ No newline at end of file diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/database/queries/RawQueries.kt b/app/src/main/java/eu/kanade/tachiyomi/data/database/queries/RawQueries.kt index cac21db3f9..daa5c48fd0 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/database/queries/RawQueries.kt +++ b/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. */ diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryNavigationView.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryNavigationView.kt index 8c3cb176c0..2edd7a6346 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryNavigationView.kt +++ b/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) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryPresenter.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryPresenter.kt index a7c32b2e13..e09e837cc4 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryPresenter.kt +++ b/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") } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibrarySort.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibrarySort.kt index 35cff3a980..677eeb244d 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibrarySort.kt +++ b/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 } \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index e0d4b14992..718d9a3452 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -30,6 +30,7 @@ Read Remove filter Alphabetically + Total chapters Last read Last updated Search