From 29724d54e3cb1ad72aa940e41ca0ce5144d220a4 Mon Sep 17 00:00:00 2001 From: gedoor Date: Fri, 20 Aug 2021 20:00:49 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E8=AE=A2=E9=98=85=E5=88=86?= =?UTF-8?q?=E7=B1=BB=E5=BE=80=E5=9B=9E=E5=88=87=E6=8D=A2=E6=97=B6=E4=B8=8D?= =?UTF-8?q?=E6=98=BE=E7=A4=BA=E5=86=85=E5=AE=B9=E7=9A=84bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../io/legado/app/data/entities/RssSource.kt | 6 +- .../main/java/io/legado/app/model/Debug.kt | 4 +- .../ui/book/arrange/ArrangeBookActivity.kt | 45 ++++++++------- .../book/changesource/ChangeSourceDialog.kt | 13 +++-- .../app/ui/book/toc/ChapterListFragment.kt | 19 ++++--- .../bookshelf/style1/BookshelfFragment1.kt | 9 ++- .../bookshelf/style1/books/BooksFragment.kt | 33 ++++++----- .../bookshelf/style2/BookshelfFragment2.kt | 57 ++++++++++--------- .../io/legado/app/ui/main/rss/RssFragment.kt | 13 +++-- .../app/ui/rss/article/RssArticlesFragment.kt | 10 +++- .../app/ui/rss/article/RssSortActivity.kt | 24 +++++--- .../ui/rss/subscription/RuleSubActivity.kt | 9 ++- 12 files changed, 142 insertions(+), 100 deletions(-) diff --git a/app/src/main/java/io/legado/app/data/entities/RssSource.kt b/app/src/main/java/io/legado/app/data/entities/RssSource.kt index 5c3280812..81a4b4d99 100644 --- a/app/src/main/java/io/legado/app/data/entities/RssSource.kt +++ b/app/src/main/java/io/legado/app/data/entities/RssSource.kt @@ -107,7 +107,7 @@ data class RssSource( return a == b || (a.isNullOrEmpty() && b.isNullOrEmpty()) } - fun sortUrls(): LinkedHashMap = linkedMapOf().apply { + fun sortUrls(): List> = arrayListOf>().apply { kotlin.runCatching { var a = sortUrl if (sortUrl?.startsWith("", 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)) } } } diff --git a/app/src/main/java/io/legado/app/model/Debug.kt b/app/src/main/java/io/legado/app/model/Debug.kt index cb08cf6f9..c3bac0547 100644 --- a/app/src/main/java/io/legado/app/model/Debug.kt +++ b/app/src/main/java/io/legado/app/model/Debug.kt @@ -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, "⇒列表页解析成功,为空") diff --git a/app/src/main/java/io/legado/app/ui/book/arrange/ArrangeBookActivity.kt b/app/src/main/java/io/legado/app/ui/book/arrange/ArrangeBookActivity.kt index 44fec7475..e2f2e6d69 100644 --- a/app/src/main/java/io/legado/app/ui/book/arrange/ArrangeBookActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/arrange/ArrangeBookActivity.kt @@ -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 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) } } } diff --git a/app/src/main/java/io/legado/app/ui/book/changesource/ChangeSourceDialog.kt b/app/src/main/java/io/legado/app/ui/book/changesource/ChangeSourceDialog.kt index c79096391..8419585c7 100644 --- a/app/src/main/java/io/legado/app/ui/book/changesource/ChangeSourceDialog.kt +++ b/app/src/main/java/io/legado/app/ui/book/changesource/ChangeSourceDialog.kt @@ -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() } } } diff --git a/app/src/main/java/io/legado/app/ui/book/toc/ChapterListFragment.kt b/app/src/main/java/io/legado/app/ui/book/toc/ChapterListFragment.kt index fdf1a2183..dcd6aa7e7 100644 --- a/app/src/main/java/io/legado/app/ui/book/toc/ChapterListFragment.kt +++ b/app/src/main/java/io/legado/app/ui/book/toc/ChapterListFragment.kt @@ -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(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 + } } } } 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 bb818c303..b62bef803 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 @@ -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) + } } } } diff --git a/app/src/main/java/io/legado/app/ui/main/bookshelf/style1/books/BooksFragment.kt b/app/src/main/java/io/legado/app/ui/main/bookshelf/style1/books/BooksFragment.kt index 0ae3e207b..025bb7c21 100644 --- a/app/src/main/java/io/legado/app/ui/main/bookshelf/style1/books/BooksFragment.kt +++ b/app/src/main/java/io/legado/app/ui/main/bookshelf/style1/books/BooksFragment.kt @@ -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) } } } 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 a78178258..fb48d30d1 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 @@ -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 } } } diff --git a/app/src/main/java/io/legado/app/ui/main/rss/RssFragment.kt b/app/src/main/java/io/legado/app/ui/main/rss/RssFragment.kt index b140c4638..cdc68c6bc 100644 --- a/app/src/main/java/io/legado/app/ui/main/rss/RssFragment.kt +++ b/app/src/main/java/io/legado/app/ui/main/rss/RssFragment.kt @@ -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(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() } } } diff --git a/app/src/main/java/io/legado/app/ui/rss/article/RssArticlesFragment.kt b/app/src/main/java/io/legado/app/ui/rss/article/RssArticlesFragment.kt index b90324d1a..3f74c8e81 100644 --- a/app/src/main/java/io/legado/app/ui/rss/article/RssArticlesFragment.kt +++ b/app/src/main/java/io/legado/app/ui/rss/article/RssArticlesFragment.kt @@ -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(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) + } } } } diff --git a/app/src/main/java/io/legado/app/ui/rss/article/RssSortActivity.kt b/app/src/main/java/io/legado/app/ui/rss/article/RssSortActivity.kt index ec7e366db..dffc88a7c 100644 --- a/app/src/main/java/io/legado/app/ui/rss/article/RssSortActivity.kt +++ b/app/src/main/java/io/legado/app/ui/rss/article/RssSortActivity.kt @@ -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() private lateinit var adapter: TabFragmentPageAdapter - private val fragments = linkedMapOf() + private val sortList = mutableListOf>() + private val fragmentMap = hashMapOf() private val upSourceResult = registerForActivityResult( ActivityResultContracts.StartActivityForResult() ) { @@ -73,11 +75,11 @@ class RssSortActivity : VMBaseActivity(), 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) + } } } }