修复订阅分类往回切换时不显示内容的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()) 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 { kotlin.runCatching {
var a = sortUrl var a = sortUrl
if (sortUrl?.startsWith("<js>", false) == true if (sortUrl?.startsWith("<js>", false) == true
@ -133,10 +133,10 @@ data class RssSource(
a?.split("(&&|\n)+".toRegex())?.forEach { c -> a?.split("(&&|\n)+".toRegex())?.forEach { c ->
val d = c.split("::") val d = c.split("::")
if (d.size > 1) if (d.size > 1)
this[d[0]] = d[1] add(Pair(d[0], d[1]))
} }
if (isEmpty()) { if (isEmpty()) {
this[""] = sourceUrl add(Pair("", sourceUrl))
} }
} }
} }

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

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

@ -7,6 +7,7 @@ import androidx.appcompat.widget.Toolbar
import androidx.core.os.bundleOf import androidx.core.os.bundleOf
import androidx.fragment.app.FragmentManager import androidx.fragment.app.FragmentManager
import androidx.fragment.app.viewModels import androidx.fragment.app.viewModels
import androidx.lifecycle.whenStarted
import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView
import io.legado.app.R import io.legado.app.R
@ -153,12 +154,14 @@ class ChangeSourceDialog : BaseDialogFragment(),
adapter.setItems(it) adapter.setItems(it)
}) })
launch { launch {
appDb.bookSourceDao.flowGroupEnabled().collect { lifecycle.whenStarted {
groups.clear() appDb.bookSourceDao.flowGroupEnabled().collect {
it.map { group -> groups.clear()
groups.addAll(group.splitNotBlank(AppPattern.splitGroupRegex)) 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.os.Bundle
import android.view.View import android.view.View
import androidx.fragment.app.activityViewModels import androidx.fragment.app.activityViewModels
import androidx.lifecycle.whenStarted
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
@ -109,14 +110,16 @@ class ChapterListFragment : VMBaseFragment<TocViewModel>(R.layout.fragment_chapt
override fun upChapterList(searchKey: String?) { override fun upChapterList(searchKey: String?) {
tocFlowJob?.cancel() tocFlowJob?.cancel()
tocFlowJob = launch { tocFlowJob = launch {
when { lifecycle.whenStarted {
searchKey.isNullOrBlank() -> appDb.bookChapterDao.flowByBook(viewModel.bookUrl) when {
else -> appDb.bookChapterDao.flowSearch(viewModel.bookUrl, searchKey) searchKey.isNullOrBlank() -> appDb.bookChapterDao.flowByBook(viewModel.bookUrl)
}.collect { else -> appDb.bookChapterDao.flowSearch(viewModel.bookUrl, searchKey)
adapter.setItems(it) }.collect {
if (searchKey.isNullOrBlank() && !scrollToDurChapter) { adapter.setItems(it)
mLayoutManager.scrollToPositionWithOffset(durChapterIndex, 0) if (searchKey.isNullOrBlank() && !scrollToDurChapter) {
scrollToDurChapter = true mLayoutManager.scrollToPositionWithOffset(durChapterIndex, 0)
scrollToDurChapter = true
}
} }
} }
} }

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

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

@ -6,6 +6,7 @@ import android.view.Menu
import android.view.View import android.view.View
import androidx.appcompat.widget.SearchView import androidx.appcompat.widget.SearchView
import androidx.core.view.isGone import androidx.core.view.isGone
import androidx.lifecycle.whenStarted
import androidx.recyclerview.widget.GridLayoutManager import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView
@ -100,11 +101,13 @@ class BookshelfFragment2 : BaseBookshelfFragment(R.layout.fragment_bookshelf1),
@SuppressLint("NotifyDataSetChanged") @SuppressLint("NotifyDataSetChanged")
private fun initGroupData() { private fun initGroupData() {
launch { launch {
appDb.bookGroupDao.flowShow().collect { lifecycle.whenStarted {
if (it != bookGroups) { appDb.bookGroupDao.flowShow().collect {
bookGroups = it if (it != bookGroups) {
booksAdapter.notifyDataSetChanged() bookGroups = it
binding.tvEmptyMsg.isGone = getItemCount() > 0 booksAdapter.notifyDataSetChanged()
binding.tvEmptyMsg.isGone = getItemCount() > 0
}
} }
} }
} }
@ -123,29 +126,31 @@ class BookshelfFragment2 : BaseBookshelfFragment(R.layout.fragment_bookshelf1),
} }
booksFlowJob?.cancel() booksFlowJob?.cancel()
booksFlowJob = launch { booksFlowJob = launch {
when (groupId) { lifecycle.whenStarted {
AppConst.bookGroupAllId -> appDb.bookDao.flowAll() when (groupId) {
AppConst.bookGroupLocalId -> appDb.bookDao.flowLocal() AppConst.bookGroupAllId -> appDb.bookDao.flowAll()
AppConst.bookGroupAudioId -> appDb.bookDao.flowAudio() AppConst.bookGroupLocalId -> appDb.bookDao.flowLocal()
AppConst.bookGroupNoneId -> appDb.bookDao.flowNoGroup() AppConst.bookGroupAudioId -> appDb.bookDao.flowAudio()
else -> appDb.bookDao.flowByGroup(groupId) AppConst.bookGroupNoneId -> appDb.bookDao.flowNoGroup()
}.collect { list -> else -> appDb.bookDao.flowByGroup(groupId)
books = when (getPrefInt(PreferKey.bookshelfSort)) { }.collect { list ->
1 -> list.sortedByDescending { books = when (getPrefInt(PreferKey.bookshelfSort)) {
it.latestChapterTime 1 -> list.sortedByDescending {
} it.latestChapterTime
2 -> list.sortedWith { o1, o2 -> }
o1.name.cnCompare(o2.name) 2 -> list.sortedWith { o1, o2 ->
} o1.name.cnCompare(o2.name)
3 -> list.sortedBy { }
it.order 3 -> list.sortedBy {
} it.order
else -> list.sortedByDescending { }
it.durChapterTime 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 android.view.View
import androidx.appcompat.widget.SearchView import androidx.appcompat.widget.SearchView
import androidx.fragment.app.viewModels import androidx.fragment.app.viewModels
import androidx.lifecycle.whenStarted
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.AppPattern import io.legado.app.constant.AppPattern
@ -121,12 +122,14 @@ class RssFragment : VMBaseFragment<RssSourceViewModel>(R.layout.fragment_rss),
private fun initGroupData() { private fun initGroupData() {
launch { launch {
appDb.rssSourceDao.flowGroup().collect { lifecycle.whenStarted {
groups.clear() appDb.rssSourceDao.flowGroup().collect {
it.map { group -> groups.clear()
groups.addAll(group.splitNotBlank(AppPattern.splitGroupRegex)) it.map { group ->
groups.addAll(group.splitNotBlank(AppPattern.splitGroupRegex))
}
upGroupsMenu()
} }
upGroupsMenu()
} }
} }
} }

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

@ -6,6 +6,7 @@ import android.content.Intent
import android.os.Bundle import android.os.Bundle
import android.view.Menu import android.view.Menu
import android.view.MenuItem import android.view.MenuItem
import android.view.ViewGroup
import androidx.activity.result.contract.ActivityResultContracts import androidx.activity.result.contract.ActivityResultContracts
import androidx.activity.viewModels import androidx.activity.viewModels
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
@ -23,7 +24,8 @@ class RssSortActivity : VMBaseActivity<ActivityRssArtivlesBinding, RssSortViewMo
override val binding by viewBinding(ActivityRssArtivlesBinding::inflate) override val binding by viewBinding(ActivityRssArtivlesBinding::inflate)
override val viewModel by viewModels<RssSortViewModel>() override val viewModel by viewModels<RssSortViewModel>()
private lateinit var adapter: TabFragmentPageAdapter 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( private val upSourceResult = registerForActivityResult(
ActivityResultContracts.StartActivityForResult() ActivityResultContracts.StartActivityForResult()
) { ) {
@ -73,11 +75,11 @@ class RssSortActivity : VMBaseActivity<ActivityRssArtivlesBinding, RssSortViewMo
} }
private fun upFragments() { private fun upFragments() {
fragments.clear() viewModel.rssSource?.sortUrls()?.let {
viewModel.rssSource?.sortUrls()?.forEach { sortList.clear()
fragments[it.key] = RssArticlesFragment.create(it.key, it.value) sortList.addAll(it)
} }
if (fragments.size == 1) { if (sortList.size == 1) {
binding.tabLayout.gone() binding.tabLayout.gone()
} else { } else {
binding.tabLayout.visible() binding.tabLayout.visible()
@ -93,17 +95,23 @@ class RssSortActivity : VMBaseActivity<ActivityRssArtivlesBinding, RssSortViewMo
} }
override fun getPageTitle(position: Int): CharSequence { override fun getPageTitle(position: Int): CharSequence {
return fragments.keys.elementAt(position) return sortList[position].first
} }
override fun getItem(position: Int): Fragment { 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 { 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.Menu
import android.view.MenuItem import android.view.MenuItem
import androidx.core.view.isGone import androidx.core.view.isGone
import androidx.lifecycle.whenStarted
import androidx.recyclerview.widget.ItemTouchHelper import androidx.recyclerview.widget.ItemTouchHelper
import io.legado.app.R import io.legado.app.R
import io.legado.app.base.BaseActivity import io.legado.app.base.BaseActivity
@ -62,9 +63,11 @@ class RuleSubActivity : BaseActivity<ActivityRuleSubBinding>(),
private fun initData() { private fun initData() {
launch { launch {
appDb.ruleSubDao.flowAll().collect { lifecycle.whenStarted {
binding.tvEmptyMsg.isGone = it.isNotEmpty() appDb.ruleSubDao.flowAll().collect {
adapter.setItems(it) binding.tvEmptyMsg.isGone = it.isNotEmpty()
adapter.setItems(it)
}
} }
} }
} }

Loading…
Cancel
Save