feat: 优化代码

pull/182/head
kunfei 5 years ago
parent d42f18e4bb
commit 7cbbc0f057
  1. 3
      app/src/main/java/io/legado/app/data/dao/BookDao.kt
  2. 17
      app/src/main/java/io/legado/app/ui/book/arrange/ArrangeBookActivity.kt
  3. 27
      app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfFragment.kt
  4. 8
      app/src/main/java/io/legado/app/ui/main/bookshelf/books/BooksFragment.kt

@ -45,6 +45,9 @@ interface BookDao {
@Query("SELECT * FROM books WHERE name = :name and author = :author") @Query("SELECT * FROM books WHERE name = :name and author = :author")
fun getBook(name: String, author: String): Book? fun getBook(name: String, author: String): Book?
@get:Query("select count(bookUrl) from books where (SELECT sum(groupId) FROM book_groups) & `group` = 0")
val noGroupSize: Int
@get:Query("SELECT * FROM books where origin <> '${BookType.local}' and type = 0") @get:Query("SELECT * FROM books where origin <> '${BookType.local}' and type = 0")
val webBooks: List<Book> val webBooks: List<Book>

@ -11,6 +11,7 @@ import androidx.recyclerview.widget.LinearLayoutManager
import io.legado.app.App import io.legado.app.App
import io.legado.app.R import io.legado.app.R
import io.legado.app.base.VMBaseActivity import io.legado.app.base.VMBaseActivity
import io.legado.app.constant.AppConst
import io.legado.app.constant.PreferKey import io.legado.app.constant.PreferKey
import io.legado.app.data.entities.Book import io.legado.app.data.entities.Book
import io.legado.app.data.entities.BookGroup import io.legado.app.data.entities.BookGroup
@ -105,10 +106,10 @@ class ArrangeBookActivity : VMBaseActivity<ArrangeBookViewModel>(R.layout.activi
booksLiveData?.removeObservers(this) booksLiveData?.removeObservers(this)
booksLiveData = booksLiveData =
when (groupId) { when (groupId) {
-1 -> App.db.bookDao().observeAll() AppConst.bookGroupAll.groupId -> App.db.bookDao().observeAll()
-2 -> App.db.bookDao().observeLocal() AppConst.bookGroupLocal.groupId -> App.db.bookDao().observeLocal()
-3 -> App.db.bookDao().observeAudio() AppConst.bookGroupAudio.groupId -> App.db.bookDao().observeAudio()
-11 -> App.db.bookDao().observeNoGroup() AppConst.bookGroupNone.groupId -> App.db.bookDao().observeNoGroup()
else -> App.db.bookDao().observeByGroup(groupId) else -> App.db.bookDao().observeByGroup(groupId)
} }
booksLiveData?.observe(this, Observer { list -> booksLiveData?.observe(this, Observer { list ->
@ -129,22 +130,22 @@ class ArrangeBookActivity : VMBaseActivity<ArrangeBookViewModel>(R.layout.activi
.show(supportFragmentManager, "groupManage") .show(supportFragmentManager, "groupManage")
R.id.menu_no_group -> { R.id.menu_no_group -> {
title_bar.subtitle = getString(R.string.no_group) title_bar.subtitle = getString(R.string.no_group)
groupId = -11 groupId = AppConst.bookGroupNone.groupId
initBookData() initBookData()
} }
R.id.menu_all -> { R.id.menu_all -> {
title_bar.subtitle = item.title title_bar.subtitle = item.title
groupId = -1 groupId = AppConst.bookGroupAll.groupId
initBookData() initBookData()
} }
R.id.menu_local -> { R.id.menu_local -> {
title_bar.subtitle = item.title title_bar.subtitle = item.title
groupId = -2 groupId = AppConst.bookGroupLocal.groupId
initBookData() initBookData()
} }
R.id.menu_audio -> { R.id.menu_audio -> {
title_bar.subtitle = item.title title_bar.subtitle = item.title
groupId = -3 groupId = AppConst.bookGroupAudio.groupId
initBookData() initBookData()
} }
else -> if (item.groupId == R.id.menu_group) { else -> if (item.groupId == R.id.menu_group) {

@ -39,6 +39,9 @@ import kotlinx.android.synthetic.main.dialog_edit_text.view.*
import kotlinx.android.synthetic.main.fragment_bookshelf.* import kotlinx.android.synthetic.main.fragment_bookshelf.*
import kotlinx.android.synthetic.main.view_tab_layout.* import kotlinx.android.synthetic.main.view_tab_layout.*
import kotlinx.android.synthetic.main.view_title_bar.* import kotlinx.android.synthetic.main.view_title_bar.*
import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.jetbrains.anko.startActivity import org.jetbrains.anko.startActivity
@ -99,8 +102,13 @@ class BookshelfFragment : VMBaseFragment<BookshelfViewModel>(R.layout.fragment_b
bookGroupLiveData = App.db.bookGroupDao().liveDataAll() bookGroupLiveData = App.db.bookGroupDao().liveDataAll()
bookGroupLiveData?.observe(viewLifecycleOwner, Observer { bookGroupLiveData?.observe(viewLifecycleOwner, Observer {
viewModel.checkGroup(it) viewModel.checkGroup(it)
launch {
synchronized(this) { synchronized(this) {
tab_layout.removeOnTabSelectedListener(this)
}
withContext(IO) {
synchronized(this@BookshelfFragment) {
tab_layout.removeOnTabSelectedListener(this@BookshelfFragment)
bookGroups.clear() bookGroups.clear()
if (AppConfig.bookGroupAllShow) { if (AppConfig.bookGroupAllShow) {
bookGroups.add(AppConst.bookGroupAll) bookGroups.add(AppConst.bookGroupAll)
@ -111,10 +119,15 @@ class BookshelfFragment : VMBaseFragment<BookshelfViewModel>(R.layout.fragment_b
if (AppConfig.bookGroupAudioShow) { if (AppConfig.bookGroupAudioShow) {
bookGroups.add(AppConst.bookGroupAudio) bookGroups.add(AppConst.bookGroupAudio)
} }
if (App.db.bookDao().noGroupSize > 0) {
bookGroups.add(AppConst.bookGroupNone)
}
bookGroups.addAll(it) bookGroups.addAll(it)
}
}
view_pager_bookshelf.adapter?.notifyDataSetChanged() view_pager_bookshelf.adapter?.notifyDataSetChanged()
tab_layout.getTabAt(getPrefInt(PreferKey.saveTabPosition, 0))?.select() tab_layout.getTabAt(getPrefInt(PreferKey.saveTabPosition, 0))?.select()
tab_layout.addOnTabSelectedListener(this) tab_layout.addOnTabSelectedListener(this@BookshelfFragment)
} }
}) })
} }
@ -129,10 +142,16 @@ class BookshelfFragment : VMBaseFragment<BookshelfViewModel>(R.layout.fragment_b
} }
override fun upGroup() { override fun upGroup() {
synchronized(this) { launch {
withContext(IO) {
synchronized(this@BookshelfFragment) {
bookGroups.remove(AppConst.bookGroupAll) bookGroups.remove(AppConst.bookGroupAll)
bookGroups.remove(AppConst.bookGroupLocal) bookGroups.remove(AppConst.bookGroupLocal)
bookGroups.remove(AppConst.bookGroupAudio) bookGroups.remove(AppConst.bookGroupAudio)
bookGroups.remove(AppConst.bookGroupNone)
if (App.db.bookDao().noGroupSize > 0) {
bookGroups.add(0, AppConst.bookGroupNone)
}
if (AppConfig.bookGroupAudioShow) { if (AppConfig.bookGroupAudioShow) {
bookGroups.add(0, AppConst.bookGroupAudio) bookGroups.add(0, AppConst.bookGroupAudio)
} }
@ -142,6 +161,8 @@ class BookshelfFragment : VMBaseFragment<BookshelfViewModel>(R.layout.fragment_b
if (AppConfig.bookGroupAllShow) { if (AppConfig.bookGroupAllShow) {
bookGroups.add(0, AppConst.bookGroupAll) bookGroups.add(0, AppConst.bookGroupAll)
} }
}
}
view_pager_bookshelf.adapter?.notifyDataSetChanged() view_pager_bookshelf.adapter?.notifyDataSetChanged()
} }
} }

@ -11,6 +11,7 @@ import androidx.recyclerview.widget.RecyclerView
import io.legado.app.App import io.legado.app.App
import io.legado.app.R import io.legado.app.R
import io.legado.app.base.BaseFragment import io.legado.app.base.BaseFragment
import io.legado.app.constant.AppConst
import io.legado.app.constant.BookType import io.legado.app.constant.BookType
import io.legado.app.constant.EventBus import io.legado.app.constant.EventBus
import io.legado.app.constant.PreferKey import io.legado.app.constant.PreferKey
@ -98,9 +99,10 @@ class BooksFragment : BaseFragment(R.layout.fragment_books),
private fun upRecyclerData() { private fun upRecyclerData() {
bookshelfLiveData?.removeObservers(this) bookshelfLiveData?.removeObservers(this)
bookshelfLiveData = when (groupId) { bookshelfLiveData = when (groupId) {
-1 -> App.db.bookDao().observeAll() AppConst.bookGroupAll.groupId -> App.db.bookDao().observeAll()
-2 -> App.db.bookDao().observeLocal() AppConst.bookGroupLocal.groupId -> App.db.bookDao().observeLocal()
-3 -> App.db.bookDao().observeAudio() AppConst.bookGroupAudio.groupId -> App.db.bookDao().observeAudio()
AppConst.bookGroupNone.groupId -> App.db.bookDao().observeNoGroup()
else -> App.db.bookDao().observeByGroup(groupId) else -> App.db.bookDao().observeByGroup(groupId)
} }
bookshelfLiveData?.observe(this, Observer { list -> bookshelfLiveData?.observe(this, Observer { list ->

Loading…
Cancel
Save