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) + } } } }