pull/1486/head
gedoor 3 years ago
parent ebfe3a972c
commit b848dd3d52
  1. 3
      app/src/main/java/io/legado/app/data/dao/BookGroupDao.kt
  2. 15
      app/src/main/java/io/legado/app/ui/main/bookshelf/BaseBookshelfFragment.kt
  3. 15
      app/src/main/java/io/legado/app/ui/main/bookshelf/style1/BookshelfFragment1.kt
  4. 14
      app/src/main/java/io/legado/app/ui/main/bookshelf/style2/BookshelfFragment2.kt

@ -1,5 +1,6 @@
package io.legado.app.data.dao
import androidx.lifecycle.LiveData
import androidx.room.*
import io.legado.app.constant.BookType
import io.legado.app.data.entities.BookGroup
@ -26,7 +27,7 @@ interface BookGroupDao {
or (groupId = -1 and show > 0)
ORDER BY `order`"""
)
val show: Flow<List<BookGroup>>
val show: LiveData<List<BookGroup>>
@Query("SELECT * FROM book_groups where groupId >= 0 ORDER BY `order`")
fun flowSelect(): Flow<List<BookGroup>>

@ -5,11 +5,14 @@ import android.view.Menu
import android.view.MenuItem
import androidx.fragment.app.activityViewModels
import androidx.fragment.app.viewModels
import androidx.lifecycle.LiveData
import io.legado.app.R
import io.legado.app.base.VMBaseFragment
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.DialogBookshelfConfigBinding
import io.legado.app.databinding.DialogEditTextBinding
import io.legado.app.help.AppConfig
@ -60,6 +63,7 @@ abstract class BaseBookshelfFragment(layoutId: Int) : VMBaseFragment<BookshelfVi
}
abstract val groupId: Long
abstract val books: List<Book>
private var groupsLiveData: LiveData<List<BookGroup>>? = null
abstract fun gotoTop()
@ -93,6 +97,17 @@ abstract class BaseBookshelfFragment(layoutId: Int) : VMBaseFragment<BookshelfVi
}
}
protected fun initBookGroupData() {
groupsLiveData?.removeObservers(viewLifecycleOwner)
groupsLiveData = appDb.bookGroupDao.show.apply {
observe(viewLifecycleOwner) {
upGroup(it)
}
}
}
abstract fun upGroup(data: List<BookGroup>)
@SuppressLint("InflateParams")
fun addBookByUrl() {
alert(titleResource = R.string.add_book_url) {

@ -27,9 +27,6 @@ import io.legado.app.utils.putPrefInt
import io.legado.app.utils.setEdgeEffectColor
import io.legado.app.utils.toastOnUi
import io.legado.app.utils.viewbindingdelegate.viewBinding
import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch
/**
* 书架界面
@ -45,7 +42,6 @@ class BookshelfFragment1 : BaseBookshelfFragment(R.layout.fragment_bookshelf),
}
private val bookGroups = mutableListOf<BookGroup>()
private val fragmentMap = hashMapOf<Long, BooksFragment>()
private var groupsFlowJob: Job? = null
override val groupId: Long get() = selectedGroup?.groupId ?: 0
override val books: List<Book>
@ -73,15 +69,6 @@ class BookshelfFragment1 : BaseBookshelfFragment(R.layout.fragment_bookshelf),
binding.viewPagerBookshelf.adapter = adapter
}
private fun initBookGroupData() {
groupsFlowJob?.cancel()
groupsFlowJob = launch {
appDb.bookGroupDao.show.collect {
upGroup(it)
}
}
}
override fun onQueryTextSubmit(query: String?): Boolean {
SearchActivity.start(requireContext(), query)
return false
@ -92,7 +79,7 @@ class BookshelfFragment1 : BaseBookshelfFragment(R.layout.fragment_bookshelf),
}
@Synchronized
private fun upGroup(data: List<BookGroup>) {
override fun upGroup(data: List<BookGroup>) {
if (data.isEmpty()) {
appDb.bookGroupDao.enableGroup(AppConst.bookGroupAllId)
} else {

@ -52,7 +52,6 @@ class BookshelfFragment2 : BaseBookshelfFragment(R.layout.fragment_bookshelf1),
}
}
private var bookGroups: List<BookGroup> = emptyList()
private var groupsFlowJob: Job? = null
private var booksFlowJob: Job? = null
override var groupId = AppConst.bookGroupNoneId
override var books: List<Book> = emptyList()
@ -60,7 +59,7 @@ class BookshelfFragment2 : BaseBookshelfFragment(R.layout.fragment_bookshelf1),
override fun onFragmentCreated(view: View, savedInstanceState: Bundle?) {
setSupportToolbar(binding.titleBar.toolbar)
initRecyclerView()
initGroupData()
initBookGroupData()
initBooksData()
}
@ -97,18 +96,13 @@ class BookshelfFragment2 : BaseBookshelfFragment(R.layout.fragment_bookshelf1),
}
@SuppressLint("NotifyDataSetChanged")
private fun initGroupData() {
groupsFlowJob?.cancel()
groupsFlowJob = launch {
appDb.bookGroupDao.show.collect {
if (it != bookGroups) {
bookGroups = it
override fun upGroup(data: List<BookGroup>) {
if (data != bookGroups) {
bookGroups = data
booksAdapter.notifyDataSetChanged()
binding.tvEmptyMsg.isGone = getItemCount() > 0
}
}
}
}
@SuppressLint("NotifyDataSetChanged")
private fun initBooksData() {

Loading…
Cancel
Save