[新增] 新增更多伴生分组:已读、未读、在读等

This commit is contained in:
HapeLee
2025-12-09 01:08:04 +08:00
parent 3385197d6a
commit 2546a8aa5d
9 changed files with 126 additions and 27 deletions
@@ -169,12 +169,47 @@ abstract class AppDatabase : RoomDatabase() {
""".trimIndent()
db.execSQL(insertBookGroupLocalSql)
@Language("sql")
val insertBookGroupTextSql = """
insert into book_groups(groupId, groupName, 'order', show)
select ${BookGroup.IdText}, '小说', -26, 1
where not exists (select * from book_groups where groupId = ${BookGroup.IdText})
""".trimIndent()
db.execSQL(insertBookGroupTextSql)
@Language("sql")
val insertBookGroupMangaSql = """
insert into book_groups(groupId, groupName, 'order', show)
select ${BookGroup.IdManga}, '漫画', -25, 1
where not exists (select * from book_groups where groupId = ${BookGroup.IdManga})
""".trimIndent()
db.execSQL(insertBookGroupMangaSql)
@Language("sql")
val insertBookGroupMusicSql = """
insert into book_groups(groupId, groupName, 'order', show)
select ${BookGroup.IdAudio}, '音频', -8, 1
where not exists (select * from book_groups where groupId = ${BookGroup.IdAudio})
""".trimIndent()
db.execSQL(insertBookGroupMusicSql)
Language("sql")
val insertGroupReading = """
insert into book_groups(groupId, groupName, 'order', show)
select ${BookGroup.IdReading}, '在读', -30, 1
where not exists (select * from book_groups where groupId = ${BookGroup.IdReading})
""".trimIndent()
db.execSQL(insertGroupReading)
@Language("sql")
val insertGroupUnread = """
insert into book_groups(groupId, groupName, 'order', show)
select ${BookGroup.IdUnread}, '未读', -29, 1
where not exists (select * from book_groups where groupId = ${BookGroup.IdUnread})
""".trimIndent()
db.execSQL(insertGroupUnread)
@Language("sql")
val insertGroupReadFinished = """
insert into book_groups(groupId, groupName, 'order', show)
select ${BookGroup.IdReadFinished}, '已读', -28, 1
where not exists (select * from book_groups where groupId = ${BookGroup.IdReadFinished})
""".trimIndent()
db.execSQL(insertGroupReadFinished)
@Language("sql")
val insertBookGroupNetNoneGroupSql = """
insert into book_groups(groupId, groupName, 'order', show)
@@ -26,7 +26,12 @@ interface BookDao {
BookGroup.IdAudio -> flowAudio()
BookGroup.IdNetNone -> flowNetNoGroup()
BookGroup.IdLocalNone -> flowLocalNoGroup()
BookGroup.IdManga -> flowManga()
BookGroup.IdText -> flowText()
BookGroup.IdError -> flowUpdateError()
BookGroup.IdUnread -> flowUnread()
BookGroup.IdReading -> flowReading()
BookGroup.IdReadFinished -> flowReadFinished()
else -> flowByUserGroup(groupId)
}.map { list ->
list.filterNot { it.isNotShelf }
@@ -77,6 +82,21 @@ interface BookDao {
@Query("SELECT * FROM books where type & ${BookType.updateError} > 0 order by durChapterTime desc")
fun flowUpdateError(): Flow<List<Book>>
@Query("""SELECT * FROM books WHERE durChapterIndex = 0 AND durChapterPos = 0""")
fun flowUnread(): Flow<List<Book>>
@Query("""SELECT * FROM books WHERE totalChapterNum > 0 AND durChapterIndex >= totalChapterNum - 1""")
fun flowReadFinished(): Flow<List<Book>>
@Query("""SELECT * FROM books WHERE totalChapterNum > 0 AND durChapterIndex > 0 AND durChapterIndex < totalChapterNum - 1""")
fun flowReading(): Flow<List<Book>>
@Query("SELECT * FROM books WHERE type & ${BookType.image} > 0")
fun flowManga(): Flow<List<Book>>
@Query("SELECT * FROM books WHERE type & ${BookType.text} > 0")
fun flowText(): Flow<List<Book>>
@Query("SELECT * FROM books WHERE (`group` & :group) > 0")
fun getBooksByGroup(group: Long): List<Book>
@@ -25,33 +25,53 @@ interface BookGroupDao {
@get:Query(
"""
with const as (SELECT sum(groupId) sumGroupId FROM book_groups where groupId > 0)
SELECT book_groups.* FROM book_groups join const
where show > 0
and (
(groupId >= 0 and exists (select 1 from books where `group` & book_groups.groupId > 0))
or groupId = -1
or (groupId = -2 and exists (select 1 from books where type & ${BookType.local} > 0))
or (groupId = -3 and exists (select 1 from books where type & ${BookType.audio} > 0))
or (groupId = -11 and exists (select 1 from books where type & ${BookType.updateError} > 0))
or (groupId = -4
and exists (
select 1 from books
where type & ${BookType.audio} = 0
and type & ${BookType.local} = 0
and const.sumGroupId & `group` = 0
)
)
or (groupId = -5
and exists (
select 1 from books
where type & ${BookType.audio} = 0
and type & ${BookType.local} > 0
and const.sumGroupId & `group` = 0
)
with const as (SELECT sum(groupId) sumGroupId FROM book_groups where groupId > 0)
SELECT book_groups.* FROM book_groups join const
where show > 0
and (
(groupId >= 0 and exists (select 1 from books where `group` & book_groups.groupId > 0))
or groupId = ${BookGroup.IdAll}
or (groupId = ${BookGroup.IdLocal} and exists (select 1 from books where type & ${BookType.local} > 0))
or (groupId = ${BookGroup.IdAudio} and exists (select 1 from books where type & ${BookType.audio} > 0))
or (groupId = ${BookGroup.IdManga} and exists (select 1 FROM books where type & ${BookType.image} > 0))
or (groupId = ${BookGroup.IdText} and exists (select 1 FROM books where type & ${BookType.text} > 0))
or (groupId = ${BookGroup.IdReading} and exists (
SELECT 1 FROM books
WHERE totalChapterNum > 0
AND durChapterIndex > 0
AND durChapterIndex < totalChapterNum - 1
))
or (groupId = ${BookGroup.IdUnread} and exists (
SELECT 1 FROM books
WHERE durChapterIndex = 0
AND durChapterPos = 0
))
or (groupId = ${BookGroup.IdReadFinished} and exists (
SELECT 1 FROM books
WHERE totalChapterNum > 0
AND durChapterIndex >= totalChapterNum - 1
))
or (groupId = ${BookGroup.IdError} and exists (select 1 from books where type & ${BookType.updateError} > 0))
or (groupId = ${BookGroup.IdNetNone}
and exists (
select 1 from books
where type & ${BookType.audio} = 0
and type & ${BookType.local} = 0
and const.sumGroupId & `group` = 0
)
)
ORDER BY `order`"""
or (groupId = ${BookGroup.IdLocalNone}
and exists (
select 1 from books
where type & ${BookType.audio} = 0
and type & ${BookType.local} > 0
and const.sumGroupId & `group` = 0
)
)
)
ORDER BY `order`"""
)
val show: LiveData<List<BookGroup>>
@@ -33,7 +33,12 @@ data class BookGroup(
const val IdAudio = -3L
const val IdNetNone = -4L
const val IdLocalNone = -5L
const val IdManga = -7L
const val IdText = -8L
const val IdError = -11L
const val IdReading = -20L
const val IdUnread = -21L
const val IdReadFinished = -22L
}
data class GroupNameInfo(
@@ -48,7 +53,12 @@ data class BookGroup(
IdLocal -> GroupNameInfo(groupName, context.getString(R.string.local))
IdNetNone -> GroupNameInfo(groupName, context.getString(R.string.net_no_group))
IdLocalNone -> GroupNameInfo(groupName, context.getString(R.string.local_no_group))
IdManga -> GroupNameInfo(groupName, context.getString(R.string.manga))
IdText -> GroupNameInfo(groupName, context.getString(R.string.noval))
IdError -> GroupNameInfo(groupName, context.getString(R.string.update_book_fail))
IdReading -> GroupNameInfo(groupName, context.getString(R.string.is_reading))
IdUnread -> GroupNameInfo(groupName, context.getString(R.string.is_unread))
IdReadFinished -> GroupNameInfo(groupName, context.getString(R.string.is_read_finished))
else -> GroupNameInfo(groupName)
}
}