From bedba9b94dd5757a192dfb113d7c4df318bc07ad Mon Sep 17 00:00:00 2001 From: Jobobby04 Date: Thu, 24 Nov 2022 15:29:35 -0500 Subject: [PATCH] Fix library not updating the UI --- .../kanade/data/manga/MangaRepositoryImpl.kt | 7 ++-- app/src/main/sqldelight/migrations/23.sqm | 29 ++++++++++++++++ app/src/main/sqldelight/view/libraryView.sq | 33 +++++++++++++++++++ 3 files changed, 67 insertions(+), 2 deletions(-) create mode 100644 app/src/main/sqldelight/migrations/23.sqm create mode 100644 app/src/main/sqldelight/view/libraryView.sq diff --git a/app/src/main/java/eu/kanade/data/manga/MangaRepositoryImpl.kt b/app/src/main/java/eu/kanade/data/manga/MangaRepositoryImpl.kt index 6bd03dd17..d6df6ef82 100644 --- a/app/src/main/java/eu/kanade/data/manga/MangaRepositoryImpl.kt +++ b/app/src/main/java/eu/kanade/data/manga/MangaRepositoryImpl.kt @@ -12,6 +12,7 @@ import eu.kanade.domain.manga.repository.MangaRepository import eu.kanade.tachiyomi.util.system.logcat import eu.kanade.tachiyomi.util.system.toLong import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.map import logcat.LogPriority class MangaRepositoryImpl( @@ -44,8 +45,10 @@ class MangaRepositoryImpl( } override fun getLibraryMangaAsFlow(): Flow> { - return handler.subscribeToList { (handler as AndroidDatabaseHandler).getLibraryQuery() } - // return handler.subscribeToList { libraryViewQueries.library(libraryManga) } + return handler.subscribeToList { libraryViewQueries.library(libraryManga) } + // SY --> + .map { getLibraryManga() } + // SY <-- } override fun getFavoritesBySourceId(sourceId: Long): Flow> { diff --git a/app/src/main/sqldelight/migrations/23.sqm b/app/src/main/sqldelight/migrations/23.sqm new file mode 100644 index 000000000..b21fc7485 --- /dev/null +++ b/app/src/main/sqldelight/migrations/23.sqm @@ -0,0 +1,29 @@ +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 new file mode 100644 index 000000000..fe88aeed5 --- /dev/null +++ b/app/src/main/sqldelight/view/libraryView.sq @@ -0,0 +1,33 @@ +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; + +library: +SELECT * +FROM libraryView; \ No newline at end of file