komikku/data/src/main/sqldelight/tachiyomi/view/historyView.sq
Cuong-Tran 5dae177b08
feat(history): Add indicator for un-finished reading & has more un-read chapters (#992)
* feat(history): Add indicator for un-finished reading & has more un-read chapters

* Fix StateProvider

* Show reading progress in History screen

* Don't count bookmark excluded chapters in unread dot for History

* Don't count bookmark excluded chapters in Library unread count

* Exclude bookmark filtered chapters from continue-reading list

* Bundle `applyFilter` for bookmark & scanlator together

* Remove parentheses
2025-07-21 15:34:12 +07:00

119 lines
3 KiB
Text

CREATE VIEW historyView AS
SELECT
history._id AS id,
mangas._id AS mangaId,
chapters._id AS chapterId,
mangas.title,
mangas.thumbnail_url AS thumbnailUrl,
mangas.source,
mangas.favorite,
mangas.cover_last_modified,
chapters.chapter_number AS chapterNumber,
-- KMK -->
mangas.chapter_flags AS chapterFlags,
chapters.read AS read,
chapters.last_page_read AS lastPageRead,
coalesce(C.total, 0) AS totalCount,
coalesce(C.readCount, 0) AS readCount,
coalesce(C.bookmarkCount, 0) AS bookmarkCount,
coalesce(C.bookmarkReadCount, 0) AS bookmarkReadCount,
-- KMK <--
history.last_read AS readAt,
history.time_read AS readDuration,
max_last_read.last_read AS maxReadAt,
max_last_read.chapter_id AS maxReadAtChapterId
FROM mangas
JOIN chapters
ON mangas._id = chapters.manga_id
JOIN history
ON chapters._id = history.chapter_id
JOIN (
SELECT chapters.manga_id,chapters._id AS chapter_id, MAX(history.last_read) AS last_read
FROM chapters JOIN history
ON chapters._id = history.chapter_id
GROUP BY chapters.manga_id
) AS max_last_read
ON chapters.manga_id = max_last_read.manga_id
-- KMK -->
LEFT JOIN(
SELECT
chapters.manga_id,
count(*) AS total,
sum(read) AS readCount,
sum(bookmark) AS bookmarkCount,
sum(CASE WHEN bookmark = 1 AND read = 1 THEN 1 ELSE 0 END) AS bookmarkReadCount,
excluded_scanlators.scanlator AS ex_scanlator
FROM chapters
LEFT JOIN excluded_scanlators
ON chapters.manga_id = excluded_scanlators.manga_id
AND chapters.scanlator = excluded_scanlators.scanlator
WHERE ex_scanlator IS NULL
GROUP BY chapters.manga_id
) AS C
ON mangas._id = C.manga_id;
-- KMK <--
history:
SELECT
id,
mangaId,
chapterId,
title,
thumbnailUrl,
source,
favorite,
cover_last_modified,
chapterNumber,
-- KMK -->
read,
lastPageRead,
CASE
WHEN (chapterFlags & :bookmarkUnmask) != 0 THEN totalCount - bookmarkCount
WHEN (chapterFlags & :bookmarkMask) != 0 THEN bookmarkCount
ELSE totalCount
END AS totalCount,
CASE
WHEN (chapterFlags & :bookmarkUnmask) != 0 THEN readCount - bookmarkReadCount
WHEN (chapterFlags & :bookmarkMask) != 0 THEN bookmarkReadCount
ELSE readCount
END AS readCount,
-- KMK <--
readAt,
readDuration
FROM historyView
WHERE historyView.readAt > 0
AND maxReadAtChapterId = historyView.chapterId
AND lower(historyView.title) LIKE ('%' || :query || '%')
ORDER BY readAt DESC;
getLatestHistory:
SELECT
id,
mangaId,
chapterId,
title,
thumbnailUrl,
source,
favorite,
cover_last_modified,
chapterNumber,
-- KMK -->
read,
lastPageRead,
CASE
WHEN (chapterFlags & :bookmarkUnmask) != 0 THEN totalCount - bookmarkCount
WHEN (chapterFlags & :bookmarkMask) != 0 THEN bookmarkCount
ELSE totalCount
END AS totalCount,
CASE
WHEN (chapterFlags & :bookmarkUnmask) != 0 THEN readCount - bookmarkReadCount
WHEN (chapterFlags & :bookmarkMask) != 0 THEN bookmarkReadCount
ELSE readCount
END AS readCount,
-- KMK <--
readAt,
readDuration
FROM historyView
WHERE historyView.readAt > 0
ORDER BY readAt DESC
LIMIT 1;