From 8d97b980e354d50a7682e87677ef336833b760fe Mon Sep 17 00:00:00 2001 From: Andreas Date: Tue, 18 Oct 2022 22:33:26 +0200 Subject: [PATCH] Tweak library view (#8240) - Move category join to improve performance - Move bookmark calculation into query - Move unread calculation into domain --- app/build.gradle.kts | 2 +- .../java/eu/kanade/data/manga/MangaMapper.kt | 7 +++-- .../java/eu/kanade/domain/DomainModule.kt | 2 -- .../GetBookmarkedChaptersByMangaId.kt | 20 ------------ .../domain/library/model/LibraryManga.kt | 9 ++++-- .../tachiyomi/ui/library/LibraryPresenter.kt | 16 ++++------ app/src/main/sqldelight/migrations/22.sqm | 31 +++++++++++++++++++ app/src/main/sqldelight/view/libraryView.sq | 12 ++++--- 8 files changed, 56 insertions(+), 43 deletions(-) delete mode 100644 app/src/main/java/eu/kanade/domain/chapter/interactor/GetBookmarkedChaptersByMangaId.kt create mode 100644 app/src/main/sqldelight/migrations/22.sqm diff --git a/app/build.gradle.kts b/app/build.gradle.kts index f543d02bdf..10324eb08f 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -27,7 +27,7 @@ android { applicationId = "eu.kanade.tachiyomi" minSdk = AndroidConfig.minSdk targetSdk = AndroidConfig.targetSdk - versionCode = 87 + versionCode = 88 versionName = "0.13.6" buildConfigField("String", "COMMIT_COUNT", "\"${getCommitCount()}\"") diff --git a/app/src/main/java/eu/kanade/data/manga/MangaMapper.kt b/app/src/main/java/eu/kanade/data/manga/MangaMapper.kt index 80edff3631..6fb16429d1 100644 --- a/app/src/main/java/eu/kanade/data/manga/MangaMapper.kt +++ b/app/src/main/java/eu/kanade/data/manga/MangaMapper.kt @@ -28,8 +28,8 @@ val mangaMapper: (Long, Long, String, String?, String?, String?, List?, ) } -val libraryManga: (Long, Long, String, String?, String?, String?, List?, String, Long, String?, Boolean, Long?, Long?, Boolean, Long, Long, Long, Long, UpdateStrategy, Long, Long, Long, Long, Long, Long) -> LibraryManga = - { id, source, url, artist, author, description, genre, title, status, thumbnailUrl, favorite, lastUpdate, nextUpdate, initialized, viewerFlags, chapterFlags, coverLastModified, dateAdded, updateStrategy, unreadCount, readCount, latestUpload, chapterFetchedAt, lastRead, category -> +val libraryManga: (Long, Long, String, String?, String?, String?, List?, String, Long, String?, Boolean, Long?, Long?, Boolean, Long, Long, Long, Long, UpdateStrategy, Long, Long, Long, Long, Long, Long, Long) -> LibraryManga = + { id, source, url, artist, author, description, genre, title, status, thumbnailUrl, favorite, lastUpdate, nextUpdate, initialized, viewerFlags, chapterFlags, coverLastModified, dateAdded, updateStrategy, totalCount, readCount, latestUpload, chapterFetchedAt, lastRead, bookmarkCount, category -> LibraryManga( manga = mangaMapper( id, @@ -53,8 +53,9 @@ val libraryManga: (Long, Long, String, String?, String?, String?, List?, updateStrategy, ), category = category, - unreadCount = unreadCount, + totalChapters = totalCount, readCount = readCount, + bookmarkCount = bookmarkCount, latestUpload = latestUpload, chapterFetchedAt = chapterFetchedAt, lastRead = lastRead, diff --git a/app/src/main/java/eu/kanade/domain/DomainModule.kt b/app/src/main/java/eu/kanade/domain/DomainModule.kt index ead3841e6a..b4047afef9 100644 --- a/app/src/main/java/eu/kanade/domain/DomainModule.kt +++ b/app/src/main/java/eu/kanade/domain/DomainModule.kt @@ -19,7 +19,6 @@ import eu.kanade.domain.category.interactor.SetMangaCategories import eu.kanade.domain.category.interactor.SetSortModeForCategory import eu.kanade.domain.category.interactor.UpdateCategory import eu.kanade.domain.category.repository.CategoryRepository -import eu.kanade.domain.chapter.interactor.GetBookmarkedChaptersByMangaId import eu.kanade.domain.chapter.interactor.GetChapter import eu.kanade.domain.chapter.interactor.GetChapterByMangaId import eu.kanade.domain.chapter.interactor.SetMangaDefaultChapterFlags @@ -111,7 +110,6 @@ class DomainModule : InjektModule { addSingletonFactory { ChapterRepositoryImpl(get()) } addFactory { GetChapter(get()) } addFactory { GetChapterByMangaId(get()) } - addFactory { GetBookmarkedChaptersByMangaId(get()) } addFactory { UpdateChapter(get()) } addFactory { SetReadStatus(get(), get(), get(), get()) } addFactory { ShouldUpdateDbChapter() } diff --git a/app/src/main/java/eu/kanade/domain/chapter/interactor/GetBookmarkedChaptersByMangaId.kt b/app/src/main/java/eu/kanade/domain/chapter/interactor/GetBookmarkedChaptersByMangaId.kt deleted file mode 100644 index 05e03a5be9..0000000000 --- a/app/src/main/java/eu/kanade/domain/chapter/interactor/GetBookmarkedChaptersByMangaId.kt +++ /dev/null @@ -1,20 +0,0 @@ -package eu.kanade.domain.chapter.interactor - -import eu.kanade.domain.chapter.model.Chapter -import eu.kanade.domain.chapter.repository.ChapterRepository -import eu.kanade.tachiyomi.util.system.logcat -import logcat.LogPriority - -class GetBookmarkedChaptersByMangaId( - private val chapterRepository: ChapterRepository, -) { - - suspend fun await(mangaId: Long): List { - return try { - chapterRepository.getBookmarkedChaptersByMangaId(mangaId) - } catch (e: Exception) { - logcat(LogPriority.ERROR, e) - emptyList() - } - } -} diff --git a/app/src/main/java/eu/kanade/domain/library/model/LibraryManga.kt b/app/src/main/java/eu/kanade/domain/library/model/LibraryManga.kt index 9b0cf1103a..085e4096f9 100644 --- a/app/src/main/java/eu/kanade/domain/library/model/LibraryManga.kt +++ b/app/src/main/java/eu/kanade/domain/library/model/LibraryManga.kt @@ -5,15 +5,20 @@ import eu.kanade.domain.manga.model.Manga data class LibraryManga( val manga: Manga, val category: Long, - val unreadCount: Long, + val totalChapters: Long, val readCount: Long, + val bookmarkCount: Long, val latestUpload: Long, val chapterFetchedAt: Long, val lastRead: Long, ) { val id: Long = manga.id - val totalChapters = readCount + unreadCount + val unreadCount + get() = totalChapters - readCount + + val hasBookmarks + get() = bookmarkCount > 0 val hasStarted = readCount > 0 } 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 423611f7c5..3e111f6169 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 @@ -19,7 +19,6 @@ import eu.kanade.domain.base.BasePreferences import eu.kanade.domain.category.interactor.GetCategories import eu.kanade.domain.category.interactor.SetMangaCategories import eu.kanade.domain.category.model.Category -import eu.kanade.domain.chapter.interactor.GetBookmarkedChaptersByMangaId import eu.kanade.domain.chapter.interactor.GetChapterByMangaId import eu.kanade.domain.chapter.interactor.SetReadStatus import eu.kanade.domain.chapter.model.toDbChapter @@ -55,7 +54,6 @@ import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.collectLatest import kotlinx.coroutines.flow.combine import kotlinx.coroutines.flow.map -import kotlinx.coroutines.runBlocking import rx.Observable import rx.android.schedulers.AndroidSchedulers import rx.schedulers.Schedulers @@ -84,7 +82,6 @@ class LibraryPresenter( private val getLibraryManga: GetLibraryManga = Injekt.get(), private val getTracks: GetTracks = Injekt.get(), private val getCategories: GetCategories = Injekt.get(), - private val getBookmarkedChaptersByMangaId: GetBookmarkedChaptersByMangaId = Injekt.get(), private val getChapterByMangaId: GetChapterByMangaId = Injekt.get(), private val setReadStatus: SetReadStatus = Injekt.get(), private val updateManga: UpdateManga = Injekt.get(), @@ -218,14 +215,13 @@ class LibraryPresenter( val filterFnBookmarked: (LibraryItem) -> Boolean = bookmarked@{ item -> if (filterBookmarked == State.IGNORE.value) return@bookmarked true - return@bookmarked runBlocking { - val isBookmarked = getBookmarkedChaptersByMangaId.await(item.libraryManga.manga.id).isNotEmpty() - return@runBlocking if (filterBookmarked == State.INCLUDE.value) { - isBookmarked - } else { - !isBookmarked - } + val hasBookmarks = item.libraryManga.hasBookmarks + + return@bookmarked if (filterBookmarked == State.INCLUDE.value) { + hasBookmarks + } else { + !hasBookmarks } } diff --git a/app/src/main/sqldelight/migrations/22.sqm b/app/src/main/sqldelight/migrations/22.sqm new file mode 100644 index 0000000000..a55abdff15 --- /dev/null +++ b/app/src/main/sqldelight/migrations/22.sqm @@ -0,0 +1,31 @@ +DROP VIEW libraryView; + +CREATE VIEW libraryView AS +SELECT + M.*, + coalesce(C.total, 0) AS totalCount, + coalesce(C.readCount, 0) AS readCount, + coalesce(C.latestUpload, 0) AS latestUpload, + coalesce(C.fetchedAt, 0) AS chapterFetchedAt, + coalesce(C.lastRead, 0) AS lastRead, + coalesce(C.bookmarkCount, 0) AS bookmarkCount, + coalesce(MC.category_id, 0) AS category +FROM mangas M +LEFT JOIN( + SELECT + chapters.manga_id, + count(*) AS total, + sum(read) AS readCount, + coalesce(max(chapters.date_upload), 0) AS latestUpload, + coalesce(max(history.last_read), 0) AS lastRead, + coalesce(max(chapters.date_fetch), 0) AS fetchedAt, + sum(chapters.bookmark) AS bookmarkCount + FROM chapters + LEFT JOIN history + ON chapters._id = history.chapter_id + GROUP BY chapters.manga_id +) AS C +ON M._id = C.manga_id +LEFT JOIN mangas_categories AS MC +ON MC.manga_id = M._id +WHERE M.favorite = 1; \ No newline at end of file diff --git a/app/src/main/sqldelight/view/libraryView.sq b/app/src/main/sqldelight/view/libraryView.sq index 93373abf17..4b1468872e 100644 --- a/app/src/main/sqldelight/view/libraryView.sq +++ b/app/src/main/sqldelight/view/libraryView.sq @@ -1,15 +1,14 @@ CREATE VIEW libraryView AS SELECT M.*, - coalesce(C.total - C.readCount, 0) AS unreadCount, + coalesce(C.total, 0) AS totalCount, coalesce(C.readCount, 0) AS readCount, coalesce(C.latestUpload, 0) AS latestUpload, coalesce(C.fetchedAt, 0) AS chapterFetchedAt, coalesce(C.lastRead, 0) AS lastRead, - COALESCE(MC.category_id, 0) AS category + coalesce(C.bookmarkCount, 0) AS bookmarkCount, + coalesce(MC.category_id, 0) AS category FROM mangas M -LEFT JOIN mangas_categories AS MC -ON MC.manga_id = M._id LEFT JOIN( SELECT chapters.manga_id, @@ -17,13 +16,16 @@ LEFT JOIN( sum(read) AS readCount, coalesce(max(chapters.date_upload), 0) AS latestUpload, coalesce(max(history.last_read), 0) AS lastRead, - coalesce(max(chapters.date_fetch), 0) AS fetchedAt + coalesce(max(chapters.date_fetch), 0) AS fetchedAt, + sum(chapters.bookmark) AS bookmarkCount FROM chapters LEFT JOIN history ON chapters._id = history.chapter_id GROUP BY chapters.manga_id ) AS C ON M._id = C.manga_id +LEFT JOIN mangas_categories AS MC +ON MC.manga_id = M._id WHERE M.favorite = 1; library: