refactor(recents): Some adjustments

This commit is contained in:
Ahmad Ansori Palembani 2024-12-09 20:20:12 +07:00
parent 07ed81454f
commit 22978ab8bf
Signed by: null2264
GPG key ID: BA64F8B60AF3EFB6
4 changed files with 80 additions and 78 deletions

View file

@ -49,8 +49,8 @@ interface History : Serializable {
): History = HistoryImpl().apply {
this.id = id
this.chapter_id = chapterId
this.last_read = lastRead ?: 0L
this.time_read = timeRead ?: 0L
lastRead?.let { this.last_read = it }
timeRead?.let { this.time_read = it }
}
}
}

View file

@ -38,7 +38,7 @@ data class MangaChapterHistory(val manga: Manga, val chapter: Chapter, val histo
coverLastModified: Long,
// chapter
chapterId: Long?,
_mangaId: Long?,
chapterMangaId: Long?,
chapterUrl: String?,
name: String?,
scanlator: String?,
@ -80,36 +80,38 @@ data class MangaChapterHistory(val manga: Manga, val chapter: Chapter, val histo
)
val chapter = try {
chapterId?.let {
Chapter.mapper(
id = chapterId,
mangaId = _mangaId ?: mangaId,
url = chapterUrl!!,
name = name!!,
scanlator = scanlator,
read = read!!,
bookmark = bookmark!!,
lastPageRead = lastPageRead!!,
pagesLeft = pagesLeft!!,
chapterNumber = chapterNumber!!,
sourceOrder = sourceOrder!!,
dateFetch = dateFetch!!,
dateUpload = dateUpload!!,
)
}
} catch (_: NullPointerException) { null } ?: Chapter.create()
Chapter.mapper(
id = chapterId!!,
mangaId = chapterMangaId!!,
url = chapterUrl!!,
name = name!!,
scanlator = scanlator,
read = read!!,
bookmark = bookmark!!,
lastPageRead = lastPageRead!!,
pagesLeft = pagesLeft!!,
chapterNumber = chapterNumber!!,
sourceOrder = sourceOrder!!,
dateFetch = dateFetch!!,
dateUpload = dateUpload!!,
)
} catch (_: NullPointerException) {
ChapterImpl()
}
val history = try {
historyId?.let {
History.mapper(
id = historyId,
chapterId = historyChapterId ?: chapterId ?: 0L,
lastRead = historyLastRead,
timeRead = historyTimeRead,
)
History.mapper(
id = historyId!!,
chapterId = historyChapterId!!,
lastRead = historyLastRead,
timeRead = historyTimeRead,
)
} catch (_: NullPointerException) {
HistoryImpl().apply {
historyChapterId?.let { chapter_id = it }
historyLastRead?.let { last_read = it }
historyTimeRead?.let { time_read = it }
}
} catch (_: NullPointerException) { null } ?: History.create().apply {
historyLastRead?.let { last_read = it }
}
return MangaChapterHistory(manga, chapter, history)

View file

@ -157,7 +157,7 @@ class ReaderViewModel(
private var finished = false
private var chapterToDownload: Download? = null
private var chapterList = emptyList<ReaderChapter>()
private var chapterList: List<ReaderChapter>? = null
private var chapterItems = emptyList<ReaderChapterItem>()
@ -215,7 +215,7 @@ class ReaderViewModel(
* Whether this presenter is initialized yet.
*/
fun needsInit(): Boolean {
return manga == null
return manga == null || chapterList == null
}
/**
@ -246,7 +246,7 @@ class ReaderViewModel(
loader = ChapterLoader(context, downloadManager, downloadProvider, manga, source)
chapterList = getChapterList()
loadChapter(loader!!, chapterList.first { chapterId == it.chapter.id })
loadChapter(loader!!, chapterList!!.first { chapterId == it.chapter.id })
Result.success(true)
} else {
// Unlikely but okay
@ -402,11 +402,11 @@ class ReaderViewModel(
): ViewerChapters {
loader.loadChapter(chapter)
val chapterPos = chapterList.indexOf(chapter)
val chapterPos = chapterList?.indexOf(chapter) ?: -1
val newChapters = ViewerChapters(
chapter,
chapterList.getOrNull(chapterPos - 1),
chapterList.getOrNull(chapterPos + 1),
chapterList?.getOrNull(chapterPos - 1),
chapterList?.getOrNull(chapterPos + 1),
)
withUIContext {

View file

@ -69,7 +69,7 @@ ON C._id = H.history_chapter_id
AND H.history_last_read > 0
LEFT JOIN scanlators_view AS S
ON C.manga_id = S.manga_id
AND ifnull(C.scanlator, 'N/A') = ifnull(S.name, '/<INVALID>/') -- I assume if it's N/A it shouldn't be filtered
AND C.scanlator = S.name
WHERE lower(M.title) LIKE '%' || :search || '%'
AND (
:apply_filter = 0 OR S.name IS NULL
@ -101,7 +101,7 @@ AND max_last_read.history_chapter_id = H.history_chapter_id
AND max_last_read.history_last_read > 0
LEFT JOIN scanlators_view AS S
ON C.manga_id = S.manga_id
AND ifnull(C.scanlator, 'N/A') = ifnull(S.name, '/<INVALID>/') -- I assume if it's N/A it shouldn't be filtered
AND C.scanlator = S.name
WHERE lower(M.title) LIKE '%' || :search || '%'
AND (
:apply_filter = 0 OR S.name IS NULL
@ -110,10 +110,10 @@ ORDER BY max_last_read.history_last_read DESC
LIMIT :limit OFFSET :offset;
getRecentsAll:
SELECT R.* FROM (
SELECT R1.* FROM (
SELECT
M.*,
chapters.*,
C.*,
history.history_id AS history_id,
history.history_chapter_id AS history_chapter_id,
history.history_last_read AS history_last_read,
@ -122,71 +122,71 @@ FROM (
SELECT M2.*
FROM mangas AS M2
LEFT JOIN (
SELECT manga_id, COUNT(*) AS unread
SELECT manga_id, COUNT(*) AS value
FROM chapters
WHERE read = 0
GROUP BY manga_id
) AS C
ON M2._id = C.manga_id
) AS unread
ON M2._id = unread.manga_id
WHERE (
:include_read = 0 OR C.unread > 0
:include_read = 0 OR unread.value > 0
)
GROUP BY M2._id
ORDER BY title
) AS M
JOIN chapters
ON M._id = chapters.manga_id
JOIN chapters AS C
ON M._id = C.manga_id
JOIN history
ON chapters._id = history.history_chapter_id
ON C._id = history.history_chapter_id
JOIN (
SELECT
chapters.manga_id AS manga_id,
chapters._id AS history_chapter_id,
MAX(history.history_last_read) AS history_last_read
FROM chapters JOIN history
ON chapters._id = history.history_chapter_id
GROUP BY chapters.manga_id
C2.manga_id AS manga_id,
C2._id AS history_chapter_id,
MAX(H2.history_last_read) AS history_last_read
FROM chapters AS C2 JOIN history AS H2
ON C2._id = H2.history_chapter_id
GROUP BY C2.manga_id
) AS max_last_read
ON chapters.manga_id = max_last_read.manga_id
ON C.manga_id = max_last_read.manga_id
AND max_last_read.history_chapter_id = history.history_chapter_id
AND max_last_read.history_last_read > 0
LEFT JOIN scanlators_view AS S
ON chapters.manga_id = S.manga_id
AND ifnull(chapters.scanlator, 'N/A') = ifnull(S.name, '/<INVALID>/') -- I assume if it's N/A it shouldn't be filtered
ON C.manga_id = S.manga_id
AND C.scanlator = S.name
WHERE lower(title) LIKE '%' || :search || '%'
AND (
:apply_filter = 0 OR S.name IS NULL
)
) AS R
) AS R1
UNION --
UNION -- Newly added chapter
SELECT R.* FROM (
SELECT R2.* FROM (
SELECT
M.*,
chapters.*,
C.*,
NULL AS history_id,
NULL AS history_chapter_id,
chapters.date_fetch AS history_last_read,
C.date_fetch AS history_last_read,
NULL AS history_time_read
FROM mangas AS M
JOIN chapters
ON M._id = chapters.manga_id
JOIN chapters AS C
ON M._id = C.manga_id
JOIN history
ON chapters._id = history.history_chapter_id
ON C._id = history.history_chapter_id
JOIN (
SELECT
manga_id,
chapters._id AS history_chapter_id,
C2.manga_id,
C2._id AS history_chapter_id,
max(date_upload)
FROM chapters JOIN mangas AS M2
ON M2._id = manga_id
WHERE read = 0
GROUP BY manga_id
FROM chapters AS C2 JOIN mangas AS M2
ON M2._id = C2.manga_id
WHERE C2.read = 0
GROUP BY C2.manga_id
) AS newest_chapter
LEFT JOIN scanlators_view AS S
ON chapters.manga_id = S.manga_id
AND ifnull(chapters.scanlator, 'N/A') = ifnull(S.name, '/<INVALID>/') -- I assume if it's N/A it shouldn't be filtered
ON C.manga_id = S.manga_id
AND C.scanlator = S.name
WHERE favorite = 1
AND newest_chapter.history_chapter_id = history.history_chapter_id
AND date_fetch > date_added
@ -194,14 +194,14 @@ AND lower(title) LIKE '%' || :search || '%'
AND (
:apply_filter = 0 OR S.name IS NULL
)
) AS R
) AS R2
UNION --
UNION -- Newly added manga
SELECT R.* FROM (
SELECT R3.* FROM (
SELECT
M.*,
chapters.*,
C.*,
NULL AS history_id,
NULL AS history_chapter_id,
M.date_added AS history_last_read,
@ -222,9 +222,9 @@ JOIN (
NULL AS pages_left,
NULL AS chapter_number,
NULL AS source_order
) AS chapters
) AS C
WHERE favorite = 1
AND lower(title) LIKE '%' || :search || '%'
) AS R
) AS R3
ORDER BY history_last_read DESC
LIMIT :limit OFFSET :offset;