From 93662444da7e67113d40ac6df112cba52379d31a Mon Sep 17 00:00:00 2001 From: kunfei Date: Sun, 30 Jul 2023 10:38:58 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/io/legado/app/constant/AppConst.kt | 8 ---- .../java/io/legado/app/data/AppDatabase.kt | 25 ++++++------ .../java/io/legado/app/data/dao/BookDao.kt | 4 +- .../io/legado/app/data/entities/BookGroup.kt | 39 +++++++++++++++---- .../legado/app/ui/book/cache/CacheActivity.kt | 10 +---- .../ui/book/manage/BookshelfManageActivity.kt | 12 +----- .../bookshelf/style1/BookshelfFragment1.kt | 3 +- .../bookshelf/style1/books/BooksFragment.kt | 14 +------ .../bookshelf/style2/BookshelfFragment2.kt | 24 ++++-------- 9 files changed, 59 insertions(+), 80 deletions(-) diff --git a/app/src/main/java/io/legado/app/constant/AppConst.kt b/app/src/main/java/io/legado/app/constant/AppConst.kt index 887c2ba8a6a3..a97bc003a059 100644 --- a/app/src/main/java/io/legado/app/constant/AppConst.kt +++ b/app/src/main/java/io/legado/app/constant/AppConst.kt @@ -38,14 +38,6 @@ object AppConst { SimpleDateFormat("yy-MM-dd-HH-mm-ss") } - const val rootGroupId = -100L - const val bookGroupAllId = -1L - const val bookGroupLocalId = -2L - const val bookGroupAudioId = -3L - const val bookGroupNetNoneId = -4L - const val bookGroupLocalNoneId = -5L - const val bookGroupErrorId = -11L - const val imagePathKey = "imagePath" val menuViewNames = arrayOf( diff --git a/app/src/main/java/io/legado/app/data/AppDatabase.kt b/app/src/main/java/io/legado/app/data/AppDatabase.kt index 99d557b2efc6..5efe95d8aab1 100644 --- a/app/src/main/java/io/legado/app/data/AppDatabase.kt +++ b/app/src/main/java/io/legado/app/data/AppDatabase.kt @@ -7,7 +7,6 @@ import androidx.room.Database import androidx.room.Room import androidx.room.RoomDatabase import androidx.sqlite.db.SupportSQLiteDatabase -import io.legado.app.constant.AppConst import io.legado.app.data.dao.* import io.legado.app.data.entities.* import io.legado.app.help.DefaultData @@ -99,43 +98,43 @@ abstract class AppDatabase : RoomDatabase() { @Language("sql") val insertBookGroupAllSql = """ insert into book_groups(groupId, groupName, 'order', show) - select ${AppConst.bookGroupAllId}, '全部', -10, 1 - where not exists (select * from book_groups where groupId = ${AppConst.bookGroupAllId}) + select ${BookGroup.IdAll}, '全部', -10, 1 + where not exists (select * from book_groups where groupId = ${BookGroup.IdAll}) """.trimIndent() db.execSQL(insertBookGroupAllSql) @Language("sql") val insertBookGroupLocalSql = """ insert into book_groups(groupId, groupName, 'order', enableRefresh, show) - select ${AppConst.bookGroupLocalId}, '本地', -9, 0, 1 - where not exists (select * from book_groups where groupId = ${AppConst.bookGroupLocalId}) + select ${BookGroup.IdLocal}, '本地', -9, 0, 1 + where not exists (select * from book_groups where groupId = ${BookGroup.IdLocal}) """.trimIndent() db.execSQL(insertBookGroupLocalSql) @Language("sql") val insertBookGroupMusicSql = """ insert into book_groups(groupId, groupName, 'order', show) - select ${AppConst.bookGroupAudioId}, '音频', -8, 1 - where not exists (select * from book_groups where groupId = ${AppConst.bookGroupAudioId}) + select ${BookGroup.IdAudio}, '音频', -8, 1 + where not exists (select * from book_groups where groupId = ${BookGroup.IdAudio}) """.trimIndent() db.execSQL(insertBookGroupMusicSql) @Language("sql") val insertBookGroupNetNoneGroupSql = """ insert into book_groups(groupId, groupName, 'order', show) - select ${AppConst.bookGroupNetNoneId}, '网络未分组', -7, 1 - where not exists (select * from book_groups where groupId = ${AppConst.bookGroupNetNoneId}) + select ${BookGroup.IdNetNone}, '网络未分组', -7, 1 + where not exists (select * from book_groups where groupId = ${BookGroup.IdNetNone}) """.trimIndent() db.execSQL(insertBookGroupNetNoneGroupSql) @Language("sql") val insertBookGroupLocalNoneGroupSql = """ insert into book_groups(groupId, groupName, 'order', show) - select ${AppConst.bookGroupLocalNoneId}, '本地未分组', -6, 0 - where not exists (select * from book_groups where groupId = ${AppConst.bookGroupLocalNoneId}) + select ${BookGroup.IdLocalNone}, '本地未分组', -6, 0 + where not exists (select * from book_groups where groupId = ${BookGroup.IdLocalNone}) """.trimIndent() db.execSQL(insertBookGroupLocalNoneGroupSql) @Language("sql") val insertBookGroupErrorSql = """ insert into book_groups(groupId, groupName, 'order', show) - select ${AppConst.bookGroupErrorId}, '更新失败', -1, 1 - where not exists (select * from book_groups where groupId = ${AppConst.bookGroupErrorId}) + select ${BookGroup.IdError}, '更新失败', -1, 1 + where not exists (select * from book_groups where groupId = ${BookGroup.IdError}) """.trimIndent() db.execSQL(insertBookGroupErrorSql) @Language("sql") diff --git a/app/src/main/java/io/legado/app/data/dao/BookDao.kt b/app/src/main/java/io/legado/app/data/dao/BookDao.kt index 8d0cc10d5b9f..3674c78f8fb1 100644 --- a/app/src/main/java/io/legado/app/data/dao/BookDao.kt +++ b/app/src/main/java/io/legado/app/data/dao/BookDao.kt @@ -1,9 +1,9 @@ package io.legado.app.data.dao import androidx.room.* -import io.legado.app.constant.AppConst import io.legado.app.constant.BookType import io.legado.app.data.entities.Book +import io.legado.app.data.entities.BookGroup import kotlinx.coroutines.flow.Flow @Dao @@ -14,7 +14,7 @@ interface BookDao { select * from books where type & ${BookType.text} > 0 and type & ${BookType.local} = 0 and ((SELECT sum(groupId) FROM book_groups where groupId > 0) & `group`) = 0 - and (select show from book_groups where groupId = ${AppConst.bookGroupNetNoneId}) != 1 + and (select show from book_groups where groupId = ${BookGroup.IdNetNone}) != 1 """ ) fun flowRoot(): Flow> diff --git a/app/src/main/java/io/legado/app/data/entities/BookGroup.kt b/app/src/main/java/io/legado/app/data/entities/BookGroup.kt index 3cbd9fe4e398..a0e5f4418578 100644 --- a/app/src/main/java/io/legado/app/data/entities/BookGroup.kt +++ b/app/src/main/java/io/legado/app/data/entities/BookGroup.kt @@ -6,8 +6,9 @@ import androidx.room.ColumnInfo import androidx.room.Entity import androidx.room.PrimaryKey import io.legado.app.R -import io.legado.app.constant.AppConst +import io.legado.app.data.appDb import io.legado.app.help.config.AppConfig +import kotlinx.coroutines.flow.Flow import kotlinx.parcelize.Parcelize @Parcelize @@ -26,14 +27,38 @@ data class BookGroup( var bookSort: Int = -1 ) : Parcelable { + companion object { + const val IdRoot = -100L + const val IdAll = -1L + const val IdLocal = -2L + const val IdAudio = -3L + const val IdNetNone = -4L + const val IdLocalNone = -5L + const val IdError = -11L + + fun flowBook(groupId: Long): Flow> { + return when (groupId) { + IdRoot -> appDb.bookDao.flowRoot() + IdAll -> appDb.bookDao.flowAll() + IdLocal -> appDb.bookDao.flowLocal() + IdAudio -> appDb.bookDao.flowAudio() + IdNetNone -> appDb.bookDao.flowNetNoGroup() + IdLocalNone -> appDb.bookDao.flowLocalNoGroup() + IdError -> appDb.bookDao.flowUpdateError() + else -> appDb.bookDao.flowByGroup(groupId) + } + } + + } + fun getManageName(context: Context): String { return when (groupId) { - AppConst.bookGroupAllId -> "$groupName(${context.getString(R.string.all)})" - AppConst.bookGroupAudioId -> "$groupName(${context.getString(R.string.audio)})" - AppConst.bookGroupLocalId -> "$groupName(${context.getString(R.string.local)})" - AppConst.bookGroupNetNoneId -> "$groupName(${context.getString(R.string.net_no_group)})" - AppConst.bookGroupLocalNoneId -> "$groupName(${context.getString(R.string.local_no_group)})" - AppConst.bookGroupErrorId -> "$groupName(${context.getString(R.string.update_book_fail)})" + IdAll -> "$groupName(${context.getString(R.string.all)})" + IdAudio -> "$groupName(${context.getString(R.string.audio)})" + IdLocal -> "$groupName(${context.getString(R.string.local)})" + IdNetNone -> "$groupName(${context.getString(R.string.net_no_group)})" + IdLocalNone -> "$groupName(${context.getString(R.string.local_no_group)})" + IdError -> "$groupName(${context.getString(R.string.update_book_fail)})" else -> groupName } } diff --git a/app/src/main/java/io/legado/app/ui/book/cache/CacheActivity.kt b/app/src/main/java/io/legado/app/ui/book/cache/CacheActivity.kt index 84dbfe95fec9..31c0df546974 100644 --- a/app/src/main/java/io/legado/app/ui/book/cache/CacheActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/cache/CacheActivity.kt @@ -11,7 +11,6 @@ import androidx.recyclerview.widget.LinearLayoutManager import com.google.android.material.textfield.TextInputLayout import io.legado.app.R import io.legado.app.base.VMBaseActivity -import io.legado.app.constant.AppConst import io.legado.app.constant.AppConst.charsets import io.legado.app.constant.AppLog import io.legado.app.constant.EventBus @@ -187,14 +186,7 @@ class CacheActivity : VMBaseActivity() private fun initBookData() { booksFlowJob?.cancel() booksFlowJob = launch { - when (groupId) { - AppConst.bookGroupAllId -> appDb.bookDao.flowAll() - AppConst.bookGroupLocalId -> appDb.bookDao.flowLocal() - AppConst.bookGroupAudioId -> appDb.bookDao.flowAudio() - AppConst.bookGroupNetNoneId -> appDb.bookDao.flowNetNoGroup() - AppConst.bookGroupLocalNoneId -> appDb.bookDao.flowLocalNoGroup() - else -> appDb.bookDao.flowByGroup(groupId) - }.conflate().map { books -> + BookGroup.flowBook(groupId).conflate().map { books -> val booksDownload = books.filter { !it.isAudio } diff --git a/app/src/main/java/io/legado/app/ui/book/manage/BookshelfManageActivity.kt b/app/src/main/java/io/legado/app/ui/book/manage/BookshelfManageActivity.kt index c88b53632367..5960cec32f40 100644 --- a/app/src/main/java/io/legado/app/ui/book/manage/BookshelfManageActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/manage/BookshelfManageActivity.kt @@ -15,7 +15,6 @@ import androidx.recyclerview.widget.ItemTouchHelper import androidx.recyclerview.widget.LinearLayoutManager import io.legado.app.R import io.legado.app.base.VMBaseActivity -import io.legado.app.constant.AppConst import io.legado.app.data.appDb import io.legado.app.data.entities.Book import io.legado.app.data.entities.BookGroup @@ -199,16 +198,7 @@ class BookshelfManageActivity : booksFlowJob?.cancel() booksFlowJob = launch { val bookSort = AppConfig.getBookSortByGroupId(viewModel.groupId) - when (viewModel.groupId) { - AppConst.rootGroupId -> appDb.bookDao.flowNetNoGroup() - AppConst.bookGroupAllId -> appDb.bookDao.flowAll() - AppConst.bookGroupLocalId -> appDb.bookDao.flowLocal() - AppConst.bookGroupAudioId -> appDb.bookDao.flowAudio() - AppConst.bookGroupNetNoneId -> appDb.bookDao.flowNetNoGroup() - AppConst.bookGroupLocalNoneId -> appDb.bookDao.flowLocalNoGroup() - AppConst.bookGroupErrorId -> appDb.bookDao.flowUpdateError() - else -> appDb.bookDao.flowByGroup(viewModel.groupId) - }.conflate().map { list -> + BookGroup.flowBook(viewModel.groupId).conflate().map { list -> when (bookSort) { 1 -> list.sortedByDescending { it.latestChapterTime diff --git a/app/src/main/java/io/legado/app/ui/main/bookshelf/style1/BookshelfFragment1.kt b/app/src/main/java/io/legado/app/ui/main/bookshelf/style1/BookshelfFragment1.kt index 8174282d2159..c82c886d3740 100644 --- a/app/src/main/java/io/legado/app/ui/main/bookshelf/style1/BookshelfFragment1.kt +++ b/app/src/main/java/io/legado/app/ui/main/bookshelf/style1/BookshelfFragment1.kt @@ -11,7 +11,6 @@ import androidx.fragment.app.FragmentManager import androidx.fragment.app.FragmentStatePagerAdapter import com.google.android.material.tabs.TabLayout import io.legado.app.R -import io.legado.app.constant.AppConst import io.legado.app.constant.PreferKey import io.legado.app.data.appDb import io.legado.app.data.entities.Book @@ -86,7 +85,7 @@ class BookshelfFragment1() : BaseBookshelfFragment(R.layout.fragment_bookshelf1) @Synchronized override fun upGroup(data: List) { if (data.isEmpty()) { - appDb.bookGroupDao.enableGroup(AppConst.bookGroupAllId) + appDb.bookGroupDao.enableGroup(BookGroup.IdAll) } else { if (data != bookGroups) { bookGroups.clear() diff --git a/app/src/main/java/io/legado/app/ui/main/bookshelf/style1/books/BooksFragment.kt b/app/src/main/java/io/legado/app/ui/main/bookshelf/style1/books/BooksFragment.kt index 94b30e9aebe0..a0b64a1dea6d 100644 --- a/app/src/main/java/io/legado/app/ui/main/bookshelf/style1/books/BooksFragment.kt +++ b/app/src/main/java/io/legado/app/ui/main/bookshelf/style1/books/BooksFragment.kt @@ -10,11 +10,9 @@ import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView import io.legado.app.R import io.legado.app.base.BaseFragment -import io.legado.app.constant.AppConst import io.legado.app.constant.AppLog import io.legado.app.constant.EventBus import io.legado.app.constant.PreferKey -import io.legado.app.data.appDb import io.legado.app.data.entities.Book import io.legado.app.data.entities.BookGroup import io.legado.app.databinding.FragmentBooksBinding @@ -135,16 +133,8 @@ class BooksFragment() : BaseFragment(R.layout.fragment_books), private fun upRecyclerData() { booksFlowJob?.cancel() booksFlowJob = launch { - when (groupId) { - AppConst.bookGroupAllId -> appDb.bookDao.flowAll() - AppConst.bookGroupLocalId -> appDb.bookDao.flowLocal() - AppConst.bookGroupAudioId -> appDb.bookDao.flowAudio() - AppConst.bookGroupNetNoneId -> appDb.bookDao.flowNetNoGroup() - AppConst.bookGroupLocalNoneId -> appDb.bookDao.flowLocalNoGroup() - AppConst.bookGroupErrorId -> appDb.bookDao.flowUpdateError() - else -> appDb.bookDao.flowByGroup(groupId) - // 书籍排序 - }.conflate().map { list -> + BookGroup.flowBook(groupId).conflate().map { list -> + //排序 when (bookSort) { 1 -> list.sortedByDescending { it.latestChapterTime } 2 -> list.sortedWith { o1, o2 -> diff --git a/app/src/main/java/io/legado/app/ui/main/bookshelf/style2/BookshelfFragment2.kt b/app/src/main/java/io/legado/app/ui/main/bookshelf/style2/BookshelfFragment2.kt index 1dc98eb46d73..fa82a1040e2c 100644 --- a/app/src/main/java/io/legado/app/ui/main/bookshelf/style2/BookshelfFragment2.kt +++ b/app/src/main/java/io/legado/app/ui/main/bookshelf/style2/BookshelfFragment2.kt @@ -9,11 +9,9 @@ import androidx.recyclerview.widget.GridLayoutManager import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView import io.legado.app.R -import io.legado.app.constant.AppConst import io.legado.app.constant.AppLog import io.legado.app.constant.EventBus import io.legado.app.constant.PreferKey -import io.legado.app.data.appDb import io.legado.app.data.entities.Book import io.legado.app.data.entities.BookGroup import io.legado.app.databinding.FragmentBookshelf2Binding @@ -65,7 +63,7 @@ class BookshelfFragment2() : BaseBookshelfFragment(R.layout.fragment_bookshelf2) } private var bookGroups: List = emptyList() private var booksFlowJob: Job? = null - override var groupId = AppConst.rootGroupId + override var groupId = BookGroup.IdRoot override var books: List = emptyList() override fun onFragmentCreated(view: View, savedInstanceState: Bundle?) { @@ -135,23 +133,17 @@ class BookshelfFragment2() : BaseBookshelfFragment(R.layout.fragment_bookshelf2) } booksFlowJob?.cancel() booksFlowJob = launch { - when (groupId) { - AppConst.rootGroupId -> appDb.bookDao.flowRoot() - AppConst.bookGroupAllId -> appDb.bookDao.flowAll() - AppConst.bookGroupLocalId -> appDb.bookDao.flowLocal() - AppConst.bookGroupAudioId -> appDb.bookDao.flowAudio() - AppConst.bookGroupNetNoneId -> appDb.bookDao.flowNetNoGroup() - AppConst.bookGroupLocalNoneId -> appDb.bookDao.flowLocalNoGroup() - AppConst.bookGroupErrorId -> appDb.bookDao.flowUpdateError() - else -> appDb.bookDao.flowByGroup(groupId) - }.conflate().map { list -> + BookGroup.flowBook(groupId).conflate().map { list -> + //排序 when (AppConfig.getBookSortByGroupId(groupId)) { 1 -> list.sortedByDescending { it.latestChapterTime } + 2 -> list.sortedWith { o1, o2 -> o1.name.cnCompare(o2.name) } + 3 -> list.sortedBy { it.order } @@ -229,7 +221,7 @@ class BookshelfFragment2() : BaseBookshelfFragment(R.layout.fragment_bookshelf2) } override fun getItemCount(): Int { - return if (groupId == AppConst.rootGroupId) { + return if (groupId == BookGroup.IdRoot) { bookGroups.size + books.size } else { books.size @@ -237,7 +229,7 @@ class BookshelfFragment2() : BaseBookshelfFragment(R.layout.fragment_bookshelf2) } override fun getItemType(position: Int): Int { - if (groupId != AppConst.rootGroupId) { + if (groupId != BookGroup.IdRoot) { return 0 } if (position < bookGroups.size) { @@ -247,7 +239,7 @@ class BookshelfFragment2() : BaseBookshelfFragment(R.layout.fragment_bookshelf2) } override fun getItem(position: Int): Any? { - if (groupId != AppConst.rootGroupId) { + if (groupId != BookGroup.IdRoot) { return books.getOrNull(position) } if (position < bookGroups.size) {