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.
This commit is contained in:
Carlos 2017-05-31 15:50:21 -04:00 committed by inorichi
parent e85841784c
commit 1089c25b8f
6 changed files with 28 additions and 2 deletions

View File

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

View File

@ -93,6 +93,15 @@ fun getLastReadMangaQuery() = """
ORDER BY max DESC 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. * Query to get the categories for a manga.
*/ */

View File

@ -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 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 lastRead = Item.MultiSort(R.string.action_sort_last_read, this)
private val lastUpdated = Item.MultiSort(R.string.action_sort_last_updated, this) private val lastUpdated = Item.MultiSort(R.string.action_sort_last_updated, this)
private val unread = Item.MultiSort(R.string.action_filter_unread, 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) 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 lastRead.state = if (sorting == LibrarySort.LAST_READ) order else SORT_NONE
lastUpdated.state = if (sorting == LibrarySort.LAST_UPDATED) 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 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) { override fun onItemClicked(item: Item) {
@ -145,6 +148,7 @@ class LibraryNavigationView @JvmOverloads constructor(context: Context, attrs: A
lastRead -> LibrarySort.LAST_READ lastRead -> LibrarySort.LAST_READ
lastUpdated -> LibrarySort.LAST_UPDATED lastUpdated -> LibrarySort.LAST_UPDATED
unread -> LibrarySort.UNREAD unread -> LibrarySort.UNREAD
total -> LibrarySort.TOTAL
else -> throw Exception("Unknown sorting") else -> throw Exception("Unknown sorting")
}) })
preferences.librarySortingAscending().set(if (item.state == SORT_ASC) true else false) preferences.librarySortingAscending().set(if (item.state == SORT_ASC) true else false)

View File

@ -146,6 +146,10 @@ class LibraryPresenter(
var counter = 0 var counter = 0
db.getLastReadManga().executeAsBlocking().associate { it.id!! to counter++ } 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 -> val sortFn: (Manga, Manga) -> Int = { manga1, manga2 ->
when (sortingMode) { when (sortingMode) {
@ -158,6 +162,11 @@ class LibraryPresenter(
} }
LibrarySort.LAST_UPDATED -> manga2.last_update.compareTo(manga1.last_update) LibrarySort.LAST_UPDATED -> manga2.last_update.compareTo(manga1.last_update)
LibrarySort.UNREAD -> manga1.unread.compareTo(manga2.unread) 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") else -> throw Exception("Unknown sorting mode")
} }
} }

View File

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

View File

@ -30,6 +30,7 @@
<string name="action_filter_read">Read</string> <string name="action_filter_read">Read</string>
<string name="action_filter_empty">Remove filter</string> <string name="action_filter_empty">Remove filter</string>
<string name="action_sort_alpha">Alphabetically</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_read">Last read</string>
<string name="action_sort_last_updated">Last updated</string> <string name="action_sort_last_updated">Last updated</string>
<string name="action_search">Search</string> <string name="action_search">Search</string>