diff --git a/app/src/main/java/io/legado/app/data/dao/BookGroupDao.kt b/app/src/main/java/io/legado/app/data/dao/BookGroupDao.kt index e33f90513..10a077b40 100644 --- a/app/src/main/java/io/legado/app/data/dao/BookGroupDao.kt +++ b/app/src/main/java/io/legado/app/data/dao/BookGroupDao.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 @@ -17,7 +18,7 @@ interface BookGroupDao { @Query("SELECT * FROM book_groups ORDER BY `order`") fun flowAll(): Flow> - @Query( + @get:Query( """ SELECT * FROM book_groups where (groupId >= 0 and show > 0) or (groupId = -4 and show > 0 and (select count(bookUrl) from books where type != ${BookType.audio} and origin != '${BookType.local}' and ((SELECT sum(groupId) FROM book_groups where groupId > 0) & `group`) = 0) > 0) @@ -26,7 +27,7 @@ interface BookGroupDao { or (groupId = -1 and show > 0) ORDER BY `order`""" ) - fun flowShow(): Flow> + val show: LiveData> @Query("SELECT * FROM book_groups where groupId >= 0 ORDER BY `order`") fun flowSelect(): Flow> diff --git a/app/src/main/java/io/legado/app/ui/main/MainViewModel.kt b/app/src/main/java/io/legado/app/ui/main/MainViewModel.kt index b98ba0c66..07ffce86d 100644 --- a/app/src/main/java/io/legado/app/ui/main/MainViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/main/MainViewModel.kt @@ -17,7 +17,6 @@ import io.legado.app.model.CacheBook import io.legado.app.model.webBook.WebBook import io.legado.app.service.CacheBookService import io.legado.app.utils.postEvent - import kotlinx.coroutines.* import timber.log.Timber import java.util.concurrent.CopyOnWriteArraySet diff --git a/app/src/main/java/io/legado/app/ui/main/bookshelf/BaseBookshelfFragment.kt b/app/src/main/java/io/legado/app/ui/main/bookshelf/BaseBookshelfFragment.kt index a5715cfa9..8dd71e171 100644 --- a/app/src/main/java/io/legado/app/ui/main/bookshelf/BaseBookshelfFragment.kt +++ b/app/src/main/java/io/legado/app/ui/main/bookshelf/BaseBookshelfFragment.kt @@ -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 + private var groupsLiveData: LiveData>? = null abstract fun gotoTop() @@ -93,6 +97,17 @@ abstract class BaseBookshelfFragment(layoutId: Int) : VMBaseFragment) + @SuppressLint("InflateParams") fun addBookByUrl() { alert(titleResource = R.string.add_book_url) { 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 d5fd3eded..afd72e992 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 @@ -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() private val fragmentMap = hashMapOf() - private var groupsFlowJob: Job? = null override val groupId: Long get() = selectedGroup?.groupId ?: 0 override val books: List @@ -73,15 +69,6 @@ class BookshelfFragment1 : BaseBookshelfFragment(R.layout.fragment_bookshelf), binding.viewPagerBookshelf.adapter = adapter } - private fun initBookGroupData() { - groupsFlowJob?.cancel() - groupsFlowJob = launch { - appDb.bookGroupDao.flowShow().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) { + override fun upGroup(data: List) { if (data.isEmpty()) { appDb.bookGroupDao.enableGroup(AppConst.bookGroupAllId) } else { 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 33a5110e9..f1ebc2fce 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 @@ -52,7 +52,6 @@ class BookshelfFragment2 : BaseBookshelfFragment(R.layout.fragment_bookshelf1), } } private var bookGroups: List = emptyList() - private var groupsFlowJob: Job? = null private var booksFlowJob: Job? = null override var groupId = AppConst.bookGroupNoneId override var books: List = 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,16 +96,11 @@ class BookshelfFragment2 : BaseBookshelfFragment(R.layout.fragment_bookshelf1), } @SuppressLint("NotifyDataSetChanged") - private fun initGroupData() { - groupsFlowJob?.cancel() - groupsFlowJob = launch { - appDb.bookGroupDao.flowShow().collect { - if (it != bookGroups) { - bookGroups = it - booksAdapter.notifyDataSetChanged() - binding.tvEmptyMsg.isGone = getItemCount() > 0 - } - } + override fun upGroup(data: List) { + if (data != bookGroups) { + bookGroups = data + booksAdapter.notifyDataSetChanged() + binding.tvEmptyMsg.isGone = getItemCount() > 0 } }