Skip to content

Commit

Permalink
优化
Browse files Browse the repository at this point in the history
  • Loading branch information
gedoor committed Jul 30, 2023
1 parent 0044250 commit 9366244
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 80 deletions.
8 changes: 0 additions & 8 deletions app/src/main/java/io/legado/app/constant/AppConst.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
25 changes: 12 additions & 13 deletions app/src/main/java/io/legado/app/data/AppDatabase.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/io/legado/app/data/dao/BookDao.kt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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<List<Book>>
Expand Down
39 changes: 32 additions & 7 deletions app/src/main/java/io/legado/app/data/entities/BookGroup.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<List<Book>> {
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
}
}
Expand Down
10 changes: 1 addition & 9 deletions app/src/main/java/io/legado/app/ui/book/cache/CacheActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -187,14 +186,7 @@ class CacheActivity : VMBaseActivity<ActivityCacheBookBinding, CacheViewModel>()
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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -86,7 +85,7 @@ class BookshelfFragment1() : BaseBookshelfFragment(R.layout.fragment_bookshelf1)
@Synchronized
override fun upGroup(data: List<BookGroup>) {
if (data.isEmpty()) {
appDb.bookGroupDao.enableGroup(AppConst.bookGroupAllId)
appDb.bookGroupDao.enableGroup(BookGroup.IdAll)
} else {
if (data != bookGroups) {
bookGroups.clear()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -65,7 +63,7 @@ class BookshelfFragment2() : BaseBookshelfFragment(R.layout.fragment_bookshelf2)
}
private var bookGroups: List<BookGroup> = emptyList()
private var booksFlowJob: Job? = null
override var groupId = AppConst.rootGroupId
override var groupId = BookGroup.IdRoot
override var books: List<Book> = emptyList()

override fun onFragmentCreated(view: View, savedInstanceState: Bundle?) {
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -229,15 +221,15 @@ 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
}
}

override fun getItemType(position: Int): Int {
if (groupId != AppConst.rootGroupId) {
if (groupId != BookGroup.IdRoot) {
return 0
}
if (position < bookGroups.size) {
Expand All @@ -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) {
Expand Down

0 comments on commit 9366244

Please sign in to comment.