mirror of
https://git.mihon.tech/mihonapp/mihon
synced 2024-11-26 23:28:58 +03:00
Improved last_read sorting (#576)
This commit is contained in:
parent
32511149d1
commit
79ab9d80f2
4 changed files with 44 additions and 35 deletions
|
@ -40,16 +40,6 @@ interface HistoryQueries : DbProvider {
|
|||
.build())
|
||||
.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.
|
||||
* Inserts history object if not yet in database
|
||||
|
|
|
@ -84,4 +84,11 @@ interface MangaQueries : DbProvider {
|
|||
.build())
|
||||
.prepare()
|
||||
|
||||
fun getLastReadManga() = db.get()
|
||||
.listOfObjects(Manga::class.java)
|
||||
.withQuery(RawQuery.builder()
|
||||
.query(getLastReadMangaQuery())
|
||||
.observesTables(MangaTable.TABLE)
|
||||
.build())
|
||||
.prepare()
|
||||
}
|
|
@ -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}
|
||||
"""
|
||||
|
||||
fun getLastHistoryByMangaId() = """
|
||||
SELECT ${History.TABLE}.*
|
||||
FROM ${History.TABLE}
|
||||
fun getLastReadMangaQuery() = """
|
||||
SELECT ${Manga.TABLE}.*, MAX(${History.TABLE}.${History.COL_LAST_READ}) AS max
|
||||
FROM ${Manga.TABLE}
|
||||
JOIN ${Chapter.TABLE}
|
||||
ON ${History.TABLE}.${History.COL_CHAPTER_ID} = ${Chapter.TABLE}.${Chapter.COL_ID}
|
||||
LEFT JOIN (
|
||||
SELECT MAX(${History.TABLE}.${History.COL_LAST_READ}) AS max
|
||||
FROM ${History.TABLE}
|
||||
GROUP BY ${History.COL_LAST_READ}
|
||||
) AS M
|
||||
WHERE ${Chapter.TABLE}.${Chapter.COL_MANGA_ID} = ? AND M.max = ${History.TABLE}.${History.COL_LAST_READ}
|
||||
ON ${Manga.TABLE}.${Manga.COL_ID} = ${Chapter.TABLE}.${Chapter.COL_MANGA_ID}
|
||||
JOIN ${History.TABLE}
|
||||
ON ${Chapter.TABLE}.${Chapter.COL_ID} = ${History.TABLE}.${History.COL_CHAPTER_ID}
|
||||
WHERE ${Manga.TABLE}.${Manga.COL_FAVORITE} = 1
|
||||
GROUP BY ${Manga.TABLE}.${Manga.COL_ID}
|
||||
ORDER BY max DESC
|
||||
"""
|
||||
|
||||
/**
|
||||
|
|
|
@ -85,6 +85,11 @@ class LibraryPresenter : BasePresenter<LibraryFragment>() {
|
|||
*/
|
||||
private val updateTriggerRelay = BehaviorRelay.create(Unit)
|
||||
|
||||
/**
|
||||
* Value that contains library sorted by last read
|
||||
*/
|
||||
private lateinit var lastReadManga: Map<Long, Int>
|
||||
|
||||
/**
|
||||
* Library subscription.
|
||||
*/
|
||||
|
@ -136,7 +141,7 @@ class LibraryPresenter : BasePresenter<LibraryFragment>() {
|
|||
|
||||
val mangaDirName = downloadManager.getMangaDirName(manga)
|
||||
val mangaDir = mangaDirs.find { it.name == mangaDirName }
|
||||
|
||||
|
||||
return@f if (mangaDir == null) {
|
||||
false
|
||||
} else {
|
||||
|
@ -149,14 +154,22 @@ class LibraryPresenter : BasePresenter<LibraryFragment>() {
|
|||
}
|
||||
|
||||
// Sorting
|
||||
val comparator: Comparator<Manga> = if (preferences.librarySortingAscending().getOrDefault())
|
||||
Comparator { m1, m2 -> sortManga(m1, m2) }
|
||||
else
|
||||
Comparator { m1, m2 -> sortManga(m2, m1) }
|
||||
val sortingMode = preferences.librarySortingMode().getOrDefault()
|
||||
if (sortingMode == LibrarySort.LAST_READ) {
|
||||
var counter = 0
|
||||
lastReadManga = db.getLastReadManga().executeAsBlocking()
|
||||
.associate { it.id!! to counter++ }
|
||||
}
|
||||
|
||||
return map.mapValues { entry -> entry.value
|
||||
.filter(filterFn)
|
||||
.sortedWith(comparator)
|
||||
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)
|
||||
.sortedWith(comparator)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -210,18 +223,18 @@ class LibraryPresenter : BasePresenter<LibraryFragment>() {
|
|||
* 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.
|
||||
*
|
||||
* @param sortingMode current sorting mode
|
||||
* @param manga1 first manga to compare
|
||||
* @param manga2 second manga to compare
|
||||
*/
|
||||
fun sortManga(manga1: Manga, manga2: Manga): Int {
|
||||
when (preferences.librarySortingMode().getOrDefault()) {
|
||||
fun sortManga(sortingMode: Int, manga1: Manga, manga2: Manga): Int {
|
||||
when (sortingMode) {
|
||||
LibrarySort.ALPHA -> return manga1.title.compareTo(manga2.title)
|
||||
LibrarySort.LAST_READ -> {
|
||||
var a = 0L
|
||||
var b = 0L
|
||||
db.getLastHistoryByMangaId(manga1.id!!).executeAsBlocking()?.let { a = it.last_read }
|
||||
db.getLastHistoryByMangaId(manga2.id!!).executeAsBlocking()?.let { b = it.last_read }
|
||||
return b.compareTo(a)
|
||||
// Get index of manga, set equal to list if size unknown.
|
||||
val manga1LastRead = lastReadManga.getOrElse(manga1.id!!, { lastReadManga.size })
|
||||
val manga2LastRead = lastReadManga.getOrElse(manga2.id!!, { lastReadManga.size })
|
||||
return manga1LastRead.compareTo(manga2LastRead)
|
||||
}
|
||||
LibrarySort.LAST_UPDATED -> return manga2.last_update.compareTo(manga1.last_update)
|
||||
else -> return manga1.title.compareTo(manga2.title)
|
||||
|
|
Loading…
Reference in a new issue