Improved last_read sorting (#576)

This commit is contained in:
Bram van de Kerkhof 2016-12-13 21:36:26 +01:00 committed by inorichi
parent 32511149d1
commit 79ab9d80f2
4 changed files with 44 additions and 35 deletions

View File

@ -40,16 +40,6 @@ interface HistoryQueries : DbProvider {
.build()) .build())
.prepare() .prepare()
fun getLastHistoryByMangaId(mangaId: Long) = db.get()
.`object`(History::class.java)
.withQuery(RawQuery.builder()
.query(getLastHistoryByMangaId())
.args(mangaId)
.observesTables(HistoryTable.TABLE)
.build())
.prepare()
/** /**
* Updates the history last read. * Updates the history last read.
* Inserts history object if not yet in database * Inserts history object if not yet in database

View File

@ -84,4 +84,11 @@ interface MangaQueries : DbProvider {
.build()) .build())
.prepare() .prepare()
fun getLastReadManga() = db.get()
.listOfObjects(Manga::class.java)
.withQuery(RawQuery.builder()
.query(getLastReadMangaQuery())
.observesTables(MangaTable.TABLE)
.build())
.prepare()
} }

View File

@ -73,17 +73,16 @@ fun getHistoryByMangaId() = """
WHERE ${Chapter.TABLE}.${Chapter.COL_MANGA_ID} = ? AND ${History.TABLE}.${History.COL_CHAPTER_ID} = ${Chapter.TABLE}.${Chapter.COL_ID} WHERE ${Chapter.TABLE}.${Chapter.COL_MANGA_ID} = ? AND ${History.TABLE}.${History.COL_CHAPTER_ID} = ${Chapter.TABLE}.${Chapter.COL_ID}
""" """
fun getLastHistoryByMangaId() = """ fun getLastReadMangaQuery() = """
SELECT ${History.TABLE}.* SELECT ${Manga.TABLE}.*, MAX(${History.TABLE}.${History.COL_LAST_READ}) AS max
FROM ${History.TABLE} FROM ${Manga.TABLE}
JOIN ${Chapter.TABLE} JOIN ${Chapter.TABLE}
ON ${History.TABLE}.${History.COL_CHAPTER_ID} = ${Chapter.TABLE}.${Chapter.COL_ID} ON ${Manga.TABLE}.${Manga.COL_ID} = ${Chapter.TABLE}.${Chapter.COL_MANGA_ID}
LEFT JOIN ( JOIN ${History.TABLE}
SELECT MAX(${History.TABLE}.${History.COL_LAST_READ}) AS max ON ${Chapter.TABLE}.${Chapter.COL_ID} = ${History.TABLE}.${History.COL_CHAPTER_ID}
FROM ${History.TABLE} WHERE ${Manga.TABLE}.${Manga.COL_FAVORITE} = 1
GROUP BY ${History.COL_LAST_READ} GROUP BY ${Manga.TABLE}.${Manga.COL_ID}
) AS M ORDER BY max DESC
WHERE ${Chapter.TABLE}.${Chapter.COL_MANGA_ID} = ? AND M.max = ${History.TABLE}.${History.COL_LAST_READ}
""" """
/** /**

View File

@ -85,6 +85,11 @@ class LibraryPresenter : BasePresenter<LibraryFragment>() {
*/ */
private val updateTriggerRelay = BehaviorRelay.create(Unit) private val updateTriggerRelay = BehaviorRelay.create(Unit)
/**
* Value that contains library sorted by last read
*/
private lateinit var lastReadManga: Map<Long, Int>
/** /**
* Library subscription. * Library subscription.
*/ */
@ -149,12 +154,20 @@ class LibraryPresenter : BasePresenter<LibraryFragment>() {
} }
// Sorting // Sorting
val comparator: Comparator<Manga> = if (preferences.librarySortingAscending().getOrDefault()) val sortingMode = preferences.librarySortingMode().getOrDefault()
Comparator { m1, m2 -> sortManga(m1, m2) } if (sortingMode == LibrarySort.LAST_READ) {
else var counter = 0
Comparator { m1, m2 -> sortManga(m2, m1) } lastReadManga = db.getLastReadManga().executeAsBlocking()
.associate { it.id!! to counter++ }
}
return map.mapValues { entry -> entry.value val comparator: Comparator<Manga> = if (preferences.librarySortingAscending().getOrDefault())
Comparator { m1, m2 -> sortManga(sortingMode, m1, m2) }
else
Comparator { m1, m2 -> sortManga(sortingMode, m2, m1) }
return map.mapValues { entry ->
entry.value
.filter(filterFn) .filter(filterFn)
.sortedWith(comparator) .sortedWith(comparator)
} }
@ -210,18 +223,18 @@ class LibraryPresenter : BasePresenter<LibraryFragment>() {
* Returns zero if this object is equal to the specified other object, * Returns zero if this object is equal to the specified other object,
* a negative number if it's less than other, or a positive number if it's greater than other. * a negative number if it's less than other, or a positive number if it's greater than other.
* *
* @param sortingMode current sorting mode
* @param manga1 first manga to compare * @param manga1 first manga to compare
* @param manga2 second manga to compare * @param manga2 second manga to compare
*/ */
fun sortManga(manga1: Manga, manga2: Manga): Int { fun sortManga(sortingMode: Int, manga1: Manga, manga2: Manga): Int {
when (preferences.librarySortingMode().getOrDefault()) { when (sortingMode) {
LibrarySort.ALPHA -> return manga1.title.compareTo(manga2.title) LibrarySort.ALPHA -> return manga1.title.compareTo(manga2.title)
LibrarySort.LAST_READ -> { LibrarySort.LAST_READ -> {
var a = 0L // Get index of manga, set equal to list if size unknown.
var b = 0L val manga1LastRead = lastReadManga.getOrElse(manga1.id!!, { lastReadManga.size })
db.getLastHistoryByMangaId(manga1.id!!).executeAsBlocking()?.let { a = it.last_read } val manga2LastRead = lastReadManga.getOrElse(manga2.id!!, { lastReadManga.size })
db.getLastHistoryByMangaId(manga2.id!!).executeAsBlocking()?.let { b = it.last_read } return manga1LastRead.compareTo(manga2LastRead)
return b.compareTo(a)
} }
LibrarySort.LAST_UPDATED -> return manga2.last_update.compareTo(manga1.last_update) LibrarySort.LAST_UPDATED -> return manga2.last_update.compareTo(manga1.last_update)
else -> return manga1.title.compareTo(manga2.title) else -> return manga1.title.compareTo(manga2.title)