Optimize unread progess filters by using the library db query

This commit is contained in:
Jay 2020-05-15 02:09:29 -04:00
parent a3c5cc2e91
commit eed9e0c765
6 changed files with 16 additions and 52 deletions

View File

@ -6,6 +6,8 @@ class LibraryManga : MangaImpl() {
var category: Int = 0
var hasRead: Boolean = false
companion object {
fun createBlank(categoryId: Int): LibraryManga = LibraryManga().apply {
title = ""

View File

@ -37,15 +37,6 @@ interface MangaQueries : DbProvider {
.withGetResolver(LibraryMangaGetResolver.INSTANCE)
.prepare()
fun getLibraryManga(id: Long) = db.get()
.`object`(LibraryManga::class.java)
.withQuery(RawQuery.builder()
.query(getLibraryMangaQuery(id))
.observesTables(MangaTable.TABLE, ChapterTable.TABLE, MangaCategoryTable.TABLE, CategoryTable.TABLE)
.build())
.withGetResolver(LibraryMangaGetResolver.INSTANCE)
.prepare()
fun getFavoriteMangas() = db.get()
.listOfObjects(Manga::class.java)
.withQuery(Query.builder()

View File

@ -12,7 +12,7 @@ import eu.kanade.tachiyomi.data.database.tables.MangaTable as Manga
val libraryQuery = """
SELECT M.*, COALESCE(MC.${MangaCategory.COL_CATEGORY_ID}, 0) AS ${Manga.COL_CATEGORY}
FROM (
SELECT ${Manga.TABLE}.*, COALESCE(C.unread, 0) AS ${Manga.COL_UNREAD}
SELECT ${Manga.TABLE}.*, COALESCE(C.unread, 0) AS ${Manga.COL_UNREAD}, COALESCE(R.hasread, 0) AS ${Manga.COL_HAS_READ}
FROM ${Manga.TABLE}
LEFT JOIN (
SELECT ${Chapter.COL_MANGA_ID}, COUNT(*) AS unread
@ -21,6 +21,13 @@ val libraryQuery = """
GROUP BY ${Chapter.COL_MANGA_ID}
) AS C
ON ${Manga.COL_ID} = C.${Chapter.COL_MANGA_ID}
LEFT JOIN (
SELECT ${Chapter.COL_MANGA_ID}, COUNT(*) AS hasread
FROM ${Chapter.TABLE}
WHERE ${Chapter.COL_READ} = 1
GROUP BY ${Chapter.COL_MANGA_ID}
) AS R
ON ${Manga.COL_ID} = R.${Chapter.COL_MANGA_ID}
WHERE ${Manga.COL_FAVORITE} = 1
GROUP BY ${Manga.COL_ID}
ORDER BY ${Manga.COL_TITLE}
@ -30,27 +37,6 @@ val libraryQuery = """
ON MC.${MangaCategory.COL_MANGA_ID} = M.${Manga.COL_ID}
"""
fun getLibraryMangaQuery(id: Long) = """
SELECT M.*, COALESCE(MC.${MangaCategory.COL_CATEGORY_ID}, 0) AS ${Manga.COL_CATEGORY}
FROM (
SELECT ${Manga.TABLE}.*, COALESCE(C.unread, 0) AS ${Manga.COL_UNREAD}
FROM ${Manga.TABLE}
LEFT JOIN (
SELECT ${Chapter.COL_MANGA_ID}, COUNT(*) AS unread
FROM ${Chapter.TABLE}
WHERE ${Chapter.COL_READ} = 0
GROUP BY ${Chapter.COL_MANGA_ID}
) AS C
ON ${Manga.COL_ID} = C.${Chapter.COL_MANGA_ID}
WHERE ${Manga.COL_FAVORITE} = 1 AND ${Manga.COL_ID} = $id
GROUP BY ${Manga.COL_ID}
ORDER BY ${Manga.COL_TITLE}
) AS M
LEFT JOIN (
SELECT * FROM ${MangaCategory.TABLE}) AS MC
ON MC.${MangaCategory.COL_MANGA_ID} = M.${Manga.COL_ID}
"""
/**
* Query to get the recent chapters of manga from the library up to a date.
*/

View File

@ -18,6 +18,7 @@ class LibraryMangaGetResolver : DefaultGetResolver<LibraryManga>(), BaseMangaGet
mapBaseFromCursor(manga, cursor)
manga.unread = cursor.getInt(cursor.getColumnIndex(MangaTable.COL_UNREAD))
manga.category = cursor.getInt(cursor.getColumnIndex(MangaTable.COL_CATEGORY))
manga.hasRead = cursor.getInt(cursor.getColumnIndex(MangaTable.COL_HAS_READ)) > 0
return manga
}

View File

@ -36,6 +36,8 @@ object MangaTable {
const val COL_UNREAD = "unread"
const val COL_HAS_READ = "has_read"
const val COL_CATEGORY = "category"
const val COL_HIDE_TITLE = "hideTitle"

View File

@ -227,12 +227,8 @@ class LibraryPresenter(
if (filterUnread == STATE_EXCLUDE && item.manga.unread > 0) return false
// Filter for unread chapters
if (filterUnread == 3 && (item.manga.unread == 0 || db.getChapters(item.manga)
.executeAsBlocking().size != item.manga.unread)
) return false
if (filterUnread == 4 && (item.manga.unread == 0 || db.getChapters(item.manga)
.executeAsBlocking().size == item.manga.unread)
) return false
if (filterUnread == 3 && !(item.manga.unread > 0 && !item.manga.hasRead)) return false
if (filterUnread == 4 && !(item.manga.unread > 0 && item.manga.hasRead)) return false
if (filterMangaType > 0) {
if (if (filterMangaType == Manga.TYPE_MANHWA) (filterMangaType != item.manga.mangaType() && filterMangaType != Manga.TYPE_WEBTOON)
@ -654,21 +650,7 @@ class LibraryPresenter(
/** Called when Library Service updates a manga, update the item as well */
fun updateManga(manga: LibraryManga) {
scope.launch {
val rawMap = allLibraryItems
val currentMap = libraryItems
val id = manga.id ?: return@launch
val dbManga = db.getLibraryManga(id).executeOnIO() ?: return@launch
arrayOf(rawMap, currentMap).forEach { map ->
map.forEach { item ->
if (item.manga.id == dbManga.id) {
item.manga.last_update = dbManga.last_update
item.manga.unread = dbManga.unread
}
}
}
getLibrary()
}
getLibrary()
}
/** Undo the removal of the manga once in library */