Merge remote-tracking branch 'origin/master'

pull/1486/head
kunfei 3 years ago
commit 76a1faab3a
  1. 5
      app/src/main/java/io/legado/app/data/dao/BookGroupDao.kt
  2. 1
      app/src/main/java/io/legado/app/ui/main/MainViewModel.kt
  3. 15
      app/src/main/java/io/legado/app/ui/main/bookshelf/BaseBookshelfFragment.kt
  4. 15
      app/src/main/java/io/legado/app/ui/main/bookshelf/style1/BookshelfFragment1.kt
  5. 18
      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
@ -17,7 +18,7 @@ interface BookGroupDao {
@Query("SELECT * FROM book_groups ORDER BY `order`")
fun flowAll(): Flow<List<BookGroup>>
@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<List<BookGroup>>
val show: LiveData<List<BookGroup>>
@Query("SELECT * FROM book_groups where groupId >= 0 ORDER BY `order`")
fun flowSelect(): Flow<List<BookGroup>>

@ -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

@ -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.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<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,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<BookGroup>) {
if (data != bookGroups) {
bookGroups = data
booksAdapter.notifyDataSetChanged()
binding.tvEmptyMsg.isGone = getItemCount() > 0
}
}

Loading…
Cancel
Save