diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/database/queries/HistoryQueries.kt b/app/src/main/java/eu/kanade/tachiyomi/data/database/queries/HistoryQueries.kt index 0d2e0f611f..627c3b236c 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/database/queries/HistoryQueries.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/database/queries/HistoryQueries.kt @@ -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 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 a3d6be9f82..e6206835b3 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 @@ -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() } \ 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 46bc39759c..d7736919da 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 @@ -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 """ /** 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 6a9c8c91c3..acff3cd5f9 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 @@ -85,6 +85,11 @@ class LibraryPresenter : BasePresenter() { */ private val updateTriggerRelay = BehaviorRelay.create(Unit) + /** + * Value that contains library sorted by last read + */ + private lateinit var lastReadManga: Map + /** * Library subscription. */ @@ -136,7 +141,7 @@ class LibraryPresenter : BasePresenter() { 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() { } // Sorting - val comparator: Comparator = 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 = 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() { * 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)