修复订阅分类往回切换时不显示内容的bug

pull/1259/head
gedoor 3 years ago
parent 6acc0a7429
commit 29724d54e3
  1. 6
      app/src/main/java/io/legado/app/data/entities/RssSource.kt
  2. 4
      app/src/main/java/io/legado/app/model/Debug.kt
  3. 45
      app/src/main/java/io/legado/app/ui/book/arrange/ArrangeBookActivity.kt
  4. 13
      app/src/main/java/io/legado/app/ui/book/changesource/ChangeSourceDialog.kt
  5. 19
      app/src/main/java/io/legado/app/ui/book/toc/ChapterListFragment.kt
  6. 9
      app/src/main/java/io/legado/app/ui/main/bookshelf/style1/BookshelfFragment1.kt
  7. 33
      app/src/main/java/io/legado/app/ui/main/bookshelf/style1/books/BooksFragment.kt
  8. 57
      app/src/main/java/io/legado/app/ui/main/bookshelf/style2/BookshelfFragment2.kt
  9. 13
      app/src/main/java/io/legado/app/ui/main/rss/RssFragment.kt
  10. 10
      app/src/main/java/io/legado/app/ui/rss/article/RssArticlesFragment.kt
  11. 24
      app/src/main/java/io/legado/app/ui/rss/article/RssSortActivity.kt
  12. 9
      app/src/main/java/io/legado/app/ui/rss/subscription/RuleSubActivity.kt

@ -107,7 +107,7 @@ data class RssSource(
return a == b || (a.isNullOrEmpty() && b.isNullOrEmpty())
}
fun sortUrls(): LinkedHashMap<String, String> = linkedMapOf<String, String>().apply {
fun sortUrls(): List<Pair<String, String>> = arrayListOf<Pair<String, String>>().apply {
kotlin.runCatching {
var a = sortUrl
if (sortUrl?.startsWith("<js>", false) == true
@ -133,10 +133,10 @@ data class RssSource(
a?.split("(&&|\n)+".toRegex())?.forEach { c ->
val d = c.split("::")
if (d.size > 1)
this[d[0]] = d[1]
add(Pair(d[0], d[1]))
}
if (isEmpty()) {
this[""] = sourceUrl
add(Pair("", sourceUrl))
}
}
}

@ -80,8 +80,8 @@ object Debug {
cancelDebug()
debugSource = rssSource.sourceUrl
log(debugSource, "︾开始解析")
val sort = rssSource.sortUrls().entries.first()
Rss.getArticles(scope, sort.key, sort.value, rssSource, 1)
val sort = rssSource.sortUrls().first()
Rss.getArticles(scope, sort.first, sort.second, rssSource, 1)
.onSuccess {
if (it.articles.isEmpty()) {
log(debugSource, "⇒列表页解析成功,为空")

@ -1,10 +1,12 @@
package io.legado.app.ui.book.arrange
import android.annotation.SuppressLint
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import androidx.activity.viewModels
import androidx.appcompat.widget.PopupMenu
import androidx.lifecycle.whenStarted
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.LinearLayoutManager
import io.legado.app.R
@ -106,13 +108,16 @@ class ArrangeBookActivity : VMBaseActivity<ActivityArrangeBookBinding, ArrangeBo
binding.selectActionBar.setCallBack(this)
}
@SuppressLint("NotifyDataSetChanged")
private fun initGroupData() {
launch {
appDb.bookGroupDao.flowAll().collect {
groupList.clear()
groupList.addAll(it)
adapter.notifyDataSetChanged()
upMenu()
lifecycle.whenStarted {
appDb.bookGroupDao.flowAll().collect {
groupList.clear()
groupList.addAll(it)
adapter.notifyDataSetChanged()
upMenu()
}
}
}
}
@ -120,22 +125,24 @@ class ArrangeBookActivity : VMBaseActivity<ActivityArrangeBookBinding, ArrangeBo
private fun initBookData() {
booksFlowJob?.cancel()
booksFlowJob = launch {
when (groupId) {
AppConst.bookGroupAllId -> appDb.bookDao.flowAll()
AppConst.bookGroupLocalId -> appDb.bookDao.flowLocal()
AppConst.bookGroupAudioId -> appDb.bookDao.flowAudio()
AppConst.bookGroupNoneId -> appDb.bookDao.flowNoGroup()
else -> appDb.bookDao.flowByGroup(groupId)
}.collect { list ->
val books = when (getPrefInt(PreferKey.bookshelfSort)) {
1 -> list.sortedByDescending { it.latestChapterTime }
2 -> list.sortedWith { o1, o2 ->
o1.name.cnCompare(o2.name)
lifecycle.whenStarted {
when (groupId) {
AppConst.bookGroupAllId -> appDb.bookDao.flowAll()
AppConst.bookGroupLocalId -> appDb.bookDao.flowLocal()
AppConst.bookGroupAudioId -> appDb.bookDao.flowAudio()
AppConst.bookGroupNoneId -> appDb.bookDao.flowNoGroup()
else -> appDb.bookDao.flowByGroup(groupId)
}.collect { list ->
val books = when (getPrefInt(PreferKey.bookshelfSort)) {
1 -> list.sortedByDescending { it.latestChapterTime }
2 -> list.sortedWith { o1, o2 ->
o1.name.cnCompare(o2.name)
}
3 -> list.sortedBy { it.order }
else -> list.sortedByDescending { it.durChapterTime }
}
3 -> list.sortedBy { it.order }
else -> list.sortedByDescending { it.durChapterTime }
adapter.setItems(books)
}
adapter.setItems(books)
}
}
}

@ -7,6 +7,7 @@ import androidx.appcompat.widget.Toolbar
import androidx.core.os.bundleOf
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.viewModels
import androidx.lifecycle.whenStarted
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import io.legado.app.R
@ -153,12 +154,14 @@ class ChangeSourceDialog : BaseDialogFragment(),
adapter.setItems(it)
})
launch {
appDb.bookSourceDao.flowGroupEnabled().collect {
groups.clear()
it.map { group ->
groups.addAll(group.splitNotBlank(AppPattern.splitGroupRegex))
lifecycle.whenStarted {
appDb.bookSourceDao.flowGroupEnabled().collect {
groups.clear()
it.map { group ->
groups.addAll(group.splitNotBlank(AppPattern.splitGroupRegex))
}
upGroupMenu()
}
upGroupMenu()
}
}
}

@ -6,6 +6,7 @@ import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.whenStarted
import io.legado.app.R
import io.legado.app.base.VMBaseFragment
import io.legado.app.constant.EventBus
@ -109,14 +110,16 @@ class ChapterListFragment : VMBaseFragment<TocViewModel>(R.layout.fragment_chapt
override fun upChapterList(searchKey: String?) {
tocFlowJob?.cancel()
tocFlowJob = launch {
when {
searchKey.isNullOrBlank() -> appDb.bookChapterDao.flowByBook(viewModel.bookUrl)
else -> appDb.bookChapterDao.flowSearch(viewModel.bookUrl, searchKey)
}.collect {
adapter.setItems(it)
if (searchKey.isNullOrBlank() && !scrollToDurChapter) {
mLayoutManager.scrollToPositionWithOffset(durChapterIndex, 0)
scrollToDurChapter = true
lifecycle.whenStarted {
when {
searchKey.isNullOrBlank() -> appDb.bookChapterDao.flowByBook(viewModel.bookUrl)
else -> appDb.bookChapterDao.flowSearch(viewModel.bookUrl, searchKey)
}.collect {
adapter.setItems(it)
if (searchKey.isNullOrBlank() && !scrollToDurChapter) {
mLayoutManager.scrollToPositionWithOffset(durChapterIndex, 0)
scrollToDurChapter = true
}
}
}
}

@ -10,6 +10,7 @@ import androidx.appcompat.widget.SearchView
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentStatePagerAdapter
import androidx.lifecycle.whenStarted
import com.google.android.material.tabs.TabLayout
import io.legado.app.R
import io.legado.app.constant.AppConst
@ -78,9 +79,11 @@ class BookshelfFragment1 : BaseBookshelfFragment(R.layout.fragment_bookshelf),
private fun initBookGroupData() {
launch {
appDb.bookGroupDao.flowShow().collect {
viewModel.checkGroup(it)
upGroup(it)
lifecycle.whenStarted {
appDb.bookGroupDao.flowShow().collect {
viewModel.checkGroup(it)
upGroup(it)
}
}
}
}

@ -5,6 +5,7 @@ import android.os.Bundle
import android.view.View
import androidx.core.view.isGone
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.whenStarted
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
@ -106,23 +107,25 @@ class BooksFragment : BaseFragment(R.layout.fragment_books),
private fun upRecyclerData() {
booksFlowJob?.cancel()
booksFlowJob = launch {
when (groupId) {
AppConst.bookGroupAllId -> appDb.bookDao.flowAll()
AppConst.bookGroupLocalId -> appDb.bookDao.flowLocal()
AppConst.bookGroupAudioId -> appDb.bookDao.flowAudio()
AppConst.bookGroupNoneId -> appDb.bookDao.flowNoGroup()
else -> appDb.bookDao.flowByGroup(groupId)
}.collect { list ->
binding.tvEmptyMsg.isGone = list.isNotEmpty()
val books = when (getPrefInt(PreferKey.bookshelfSort)) {
1 -> list.sortedByDescending { it.latestChapterTime }
2 -> list.sortedWith { o1, o2 ->
o1.name.cnCompare(o2.name)
lifecycle.whenStarted {
when (groupId) {
AppConst.bookGroupAllId -> appDb.bookDao.flowAll()
AppConst.bookGroupLocalId -> appDb.bookDao.flowLocal()
AppConst.bookGroupAudioId -> appDb.bookDao.flowAudio()
AppConst.bookGroupNoneId -> appDb.bookDao.flowNoGroup()
else -> appDb.bookDao.flowByGroup(groupId)
}.collect { list ->
binding.tvEmptyMsg.isGone = list.isNotEmpty()
val books = when (getPrefInt(PreferKey.bookshelfSort)) {
1 -> list.sortedByDescending { it.latestChapterTime }
2 -> list.sortedWith { o1, o2 ->
o1.name.cnCompare(o2.name)
}
3 -> list.sortedBy { it.order }
else -> list.sortedByDescending { it.durChapterTime }
}
3 -> list.sortedBy { it.order }
else -> list.sortedByDescending { it.durChapterTime }
booksAdapter.setItems(books)
}
booksAdapter.setItems(books)
}
}
}

@ -6,6 +6,7 @@ import android.view.Menu
import android.view.View
import androidx.appcompat.widget.SearchView
import androidx.core.view.isGone
import androidx.lifecycle.whenStarted
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
@ -100,11 +101,13 @@ class BookshelfFragment2 : BaseBookshelfFragment(R.layout.fragment_bookshelf1),
@SuppressLint("NotifyDataSetChanged")
private fun initGroupData() {
launch {
appDb.bookGroupDao.flowShow().collect {
if (it != bookGroups) {
bookGroups = it
booksAdapter.notifyDataSetChanged()
binding.tvEmptyMsg.isGone = getItemCount() > 0
lifecycle.whenStarted {
appDb.bookGroupDao.flowShow().collect {
if (it != bookGroups) {
bookGroups = it
booksAdapter.notifyDataSetChanged()
binding.tvEmptyMsg.isGone = getItemCount() > 0
}
}
}
}
@ -123,29 +126,31 @@ class BookshelfFragment2 : BaseBookshelfFragment(R.layout.fragment_bookshelf1),
}
booksFlowJob?.cancel()
booksFlowJob = launch {
when (groupId) {
AppConst.bookGroupAllId -> appDb.bookDao.flowAll()
AppConst.bookGroupLocalId -> appDb.bookDao.flowLocal()
AppConst.bookGroupAudioId -> appDb.bookDao.flowAudio()
AppConst.bookGroupNoneId -> appDb.bookDao.flowNoGroup()
else -> appDb.bookDao.flowByGroup(groupId)
}.collect { list ->
books = when (getPrefInt(PreferKey.bookshelfSort)) {
1 -> list.sortedByDescending {
it.latestChapterTime
}
2 -> list.sortedWith { o1, o2 ->
o1.name.cnCompare(o2.name)
}
3 -> list.sortedBy {
it.order
}
else -> list.sortedByDescending {
it.durChapterTime
lifecycle.whenStarted {
when (groupId) {
AppConst.bookGroupAllId -> appDb.bookDao.flowAll()
AppConst.bookGroupLocalId -> appDb.bookDao.flowLocal()
AppConst.bookGroupAudioId -> appDb.bookDao.flowAudio()
AppConst.bookGroupNoneId -> appDb.bookDao.flowNoGroup()
else -> appDb.bookDao.flowByGroup(groupId)
}.collect { list ->
books = when (getPrefInt(PreferKey.bookshelfSort)) {
1 -> list.sortedByDescending {
it.latestChapterTime
}
2 -> list.sortedWith { o1, o2 ->
o1.name.cnCompare(o2.name)
}
3 -> list.sortedBy {
it.order
}
else -> list.sortedByDescending {
it.durChapterTime
}
}
booksAdapter.notifyDataSetChanged()
binding.tvEmptyMsg.isGone = getItemCount() > 0
}
booksAdapter.notifyDataSetChanged()
binding.tvEmptyMsg.isGone = getItemCount() > 0
}
}
}

@ -7,6 +7,7 @@ import android.view.SubMenu
import android.view.View
import androidx.appcompat.widget.SearchView
import androidx.fragment.app.viewModels
import androidx.lifecycle.whenStarted
import io.legado.app.R
import io.legado.app.base.VMBaseFragment
import io.legado.app.constant.AppPattern
@ -121,12 +122,14 @@ class RssFragment : VMBaseFragment<RssSourceViewModel>(R.layout.fragment_rss),
private fun initGroupData() {
launch {
appDb.rssSourceDao.flowGroup().collect {
groups.clear()
it.map { group ->
groups.addAll(group.splitNotBlank(AppPattern.splitGroupRegex))
lifecycle.whenStarted {
appDb.rssSourceDao.flowGroup().collect {
groups.clear()
it.map { group ->
groups.addAll(group.splitNotBlank(AppPattern.splitGroupRegex))
}
upGroupsMenu()
}
upGroupsMenu()
}
}
}

@ -5,6 +5,8 @@ import android.os.Bundle
import android.view.View
import androidx.fragment.app.activityViewModels
import androidx.fragment.app.viewModels
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.whenStarted
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
@ -95,9 +97,11 @@ class RssArticlesFragment : VMBaseFragment<RssArticlesViewModel>(R.layout.fragme
private fun initData() {
val rssUrl = activityViewModel.url ?: return
articlesFlowJob?.cancel()
articlesFlowJob = launch {
appDb.rssArticleDao.flowByOriginSort(rssUrl, viewModel.sortName).collect {
adapter.setItems(it)
articlesFlowJob = lifecycleScope.launch {
lifecycle.whenStarted {
appDb.rssArticleDao.flowByOriginSort(rssUrl, viewModel.sortName).collect {
adapter.setItems(it)
}
}
}
}

@ -6,6 +6,7 @@ import android.content.Intent
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import android.view.ViewGroup
import androidx.activity.result.contract.ActivityResultContracts
import androidx.activity.viewModels
import androidx.fragment.app.Fragment
@ -23,7 +24,8 @@ class RssSortActivity : VMBaseActivity<ActivityRssArtivlesBinding, RssSortViewMo
override val binding by viewBinding(ActivityRssArtivlesBinding::inflate)
override val viewModel by viewModels<RssSortViewModel>()
private lateinit var adapter: TabFragmentPageAdapter
private val fragments = linkedMapOf<String, Fragment>()
private val sortList = mutableListOf<Pair<String, String>>()
private val fragmentMap = hashMapOf<String, Fragment>()
private val upSourceResult = registerForActivityResult(
ActivityResultContracts.StartActivityForResult()
) {
@ -73,11 +75,11 @@ class RssSortActivity : VMBaseActivity<ActivityRssArtivlesBinding, RssSortViewMo
}
private fun upFragments() {
fragments.clear()
viewModel.rssSource?.sortUrls()?.forEach {
fragments[it.key] = RssArticlesFragment.create(it.key, it.value)
viewModel.rssSource?.sortUrls()?.let {
sortList.clear()
sortList.addAll(it)
}
if (fragments.size == 1) {
if (sortList.size == 1) {
binding.tabLayout.gone()
} else {
binding.tabLayout.visible()
@ -93,17 +95,23 @@ class RssSortActivity : VMBaseActivity<ActivityRssArtivlesBinding, RssSortViewMo
}
override fun getPageTitle(position: Int): CharSequence {
return fragments.keys.elementAt(position)
return sortList[position].first
}
override fun getItem(position: Int): Fragment {
return fragments.values.elementAt(position)
val sort = sortList[position]
return RssArticlesFragment.create(sort.first, sort.second)
}
override fun getCount(): Int {
return fragments.size
return sortList.size
}
override fun instantiateItem(container: ViewGroup, position: Int): Any {
val fragment = super.instantiateItem(container, position) as Fragment
fragmentMap[sortList[position].first] = fragment
return fragment
}
}
}

@ -4,6 +4,7 @@ import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import androidx.core.view.isGone
import androidx.lifecycle.whenStarted
import androidx.recyclerview.widget.ItemTouchHelper
import io.legado.app.R
import io.legado.app.base.BaseActivity
@ -62,9 +63,11 @@ class RuleSubActivity : BaseActivity<ActivityRuleSubBinding>(),
private fun initData() {
launch {
appDb.ruleSubDao.flowAll().collect {
binding.tvEmptyMsg.isGone = it.isNotEmpty()
adapter.setItems(it)
lifecycle.whenStarted {
appDb.ruleSubDao.flowAll().collect {
binding.tvEmptyMsg.isGone = it.isNotEmpty()
adapter.setItems(it)
}
}
}
}

Loading…
Cancel
Save