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

@ -5,11 +5,14 @@ import android.view.Menu
import android.view.MenuItem import android.view.MenuItem
import androidx.fragment.app.activityViewModels import androidx.fragment.app.activityViewModels
import androidx.fragment.app.viewModels import androidx.fragment.app.viewModels
import androidx.lifecycle.LiveData
import io.legado.app.R import io.legado.app.R
import io.legado.app.base.VMBaseFragment import io.legado.app.base.VMBaseFragment
import io.legado.app.constant.EventBus import io.legado.app.constant.EventBus
import io.legado.app.constant.PreferKey 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.Book
import io.legado.app.data.entities.BookGroup
import io.legado.app.databinding.DialogBookshelfConfigBinding import io.legado.app.databinding.DialogBookshelfConfigBinding
import io.legado.app.databinding.DialogEditTextBinding import io.legado.app.databinding.DialogEditTextBinding
import io.legado.app.help.AppConfig import io.legado.app.help.AppConfig
@ -60,6 +63,7 @@ abstract class BaseBookshelfFragment(layoutId: Int) : VMBaseFragment<BookshelfVi
} }
abstract val groupId: Long abstract val groupId: Long
abstract val books: List<Book> abstract val books: List<Book>
private var groupsLiveData: LiveData<List<BookGroup>>? = null
abstract fun gotoTop() 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") @SuppressLint("InflateParams")
fun addBookByUrl() { fun addBookByUrl() {
alert(titleResource = R.string.add_book_url) { 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.setEdgeEffectColor
import io.legado.app.utils.toastOnUi import io.legado.app.utils.toastOnUi
import io.legado.app.utils.viewbindingdelegate.viewBinding 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 bookGroups = mutableListOf<BookGroup>()
private val fragmentMap = hashMapOf<Long, BooksFragment>() private val fragmentMap = hashMapOf<Long, BooksFragment>()
private var groupsFlowJob: Job? = null
override val groupId: Long get() = selectedGroup?.groupId ?: 0 override val groupId: Long get() = selectedGroup?.groupId ?: 0
override val books: List<Book> override val books: List<Book>
@ -73,15 +69,6 @@ class BookshelfFragment1 : BaseBookshelfFragment(R.layout.fragment_bookshelf),
binding.viewPagerBookshelf.adapter = adapter binding.viewPagerBookshelf.adapter = adapter
} }
private fun initBookGroupData() {
groupsFlowJob?.cancel()
groupsFlowJob = launch {
appDb.bookGroupDao.show.collect {
upGroup(it)
}
}
}
override fun onQueryTextSubmit(query: String?): Boolean { override fun onQueryTextSubmit(query: String?): Boolean {
SearchActivity.start(requireContext(), query) SearchActivity.start(requireContext(), query)
return false return false
@ -92,7 +79,7 @@ class BookshelfFragment1 : BaseBookshelfFragment(R.layout.fragment_bookshelf),
} }
@Synchronized @Synchronized
private fun upGroup(data: List<BookGroup>) { override fun upGroup(data: List<BookGroup>) {
if (data.isEmpty()) { if (data.isEmpty()) {
appDb.bookGroupDao.enableGroup(AppConst.bookGroupAllId) appDb.bookGroupDao.enableGroup(AppConst.bookGroupAllId)
} else { } else {

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

Loading…
Cancel
Save