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 41af7bf8a..1ad9ebc67 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 @@ -27,6 +27,7 @@ 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 @@ -44,7 +45,7 @@ class BookshelfFragment1 : BaseBookshelfFragment(R.layout.fragment_bookshelf), } private val bookGroups = mutableListOf() private val fragmentMap = hashMapOf() - + private var groupsFlowJob: Job? = null override val groupId: Long get() = selectedGroup.groupId override val books: List @@ -73,7 +74,8 @@ class BookshelfFragment1 : BaseBookshelfFragment(R.layout.fragment_bookshelf), } private fun initBookGroupData() { - launch { + groupsFlowJob?.cancel() + groupsFlowJob = launch { appDb.bookGroupDao.flowShow().collect { 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 e859b5cdd..75f729b6f 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 @@ -45,9 +45,17 @@ class BooksFragment() : BaseFragment(R.layout.fragment_books), } private val binding by viewBinding(FragmentBooksBinding::bind) - private val activityViewModel: MainViewModel - by activityViewModels() - private lateinit var booksAdapter: BaseBooksAdapter<*> + private val activityViewModel by activityViewModels() + private val bookshelfLayout by lazy { + getPrefInt(PreferKey.bookshelfLayout) + } + private val booksAdapter: BaseBooksAdapter<*> by lazy { + if (bookshelfLayout == 0) { + BooksAdapterList(requireContext(), this) + } else { + BooksAdapterGrid(requireContext(), this) + } + } private var booksFlowJob: Job? = null private var position = 0 private var groupId = -1L @@ -68,13 +76,10 @@ class BooksFragment() : BaseFragment(R.layout.fragment_books), binding.refreshLayout.isRefreshing = false activityViewModel.upToc(booksAdapter.getItems()) } - val bookshelfLayout = getPrefInt(PreferKey.bookshelfLayout) if (bookshelfLayout == 0) { binding.rvBookshelf.layoutManager = LinearLayoutManager(context) - booksAdapter = BooksAdapterList(requireContext(), this) } else { binding.rvBookshelf.layoutManager = GridLayoutManager(context, bookshelfLayout + 2) - booksAdapter = BooksAdapterGrid(requireContext(), this) } binding.rvBookshelf.adapter = booksAdapter booksAdapter.registerAdapterDataObserver(object : RecyclerView.AdapterDataObserver() { 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 6b0ce4d1b..33a5110e9 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 @@ -41,10 +41,20 @@ class BookshelfFragment2 : BaseBookshelfFragment(R.layout.fragment_bookshelf1), BaseBooksAdapter.CallBack { private val binding by viewBinding(FragmentBookshelf1Binding::bind) - private lateinit var booksAdapter: BaseBooksAdapter<*> - override var groupId = AppConst.bookGroupNoneId - private var booksFlowJob: Job? = null + private val bookshelfLayout by lazy { + getPrefInt(PreferKey.bookshelfLayout) + } + private val booksAdapter: BaseBooksAdapter<*> by lazy { + if (bookshelfLayout == 0) { + BooksAdapterList(requireContext(), this) + } else { + BooksAdapterGrid(requireContext(), this) + } + } private var bookGroups: List = emptyList() + private var groupsFlowJob: Job? = null + private var booksFlowJob: Job? = null + override var groupId = AppConst.bookGroupNoneId override var books: List = emptyList() override fun onFragmentCreated(view: View, savedInstanceState: Bundle?) { @@ -61,13 +71,10 @@ class BookshelfFragment2 : BaseBookshelfFragment(R.layout.fragment_bookshelf1), binding.refreshLayout.isRefreshing = false activityViewModel.upToc(books) } - val bookshelfLayout = getPrefInt(PreferKey.bookshelfLayout) if (bookshelfLayout == 0) { binding.rvBookshelf.layoutManager = LinearLayoutManager(context) - booksAdapter = BooksAdapterList(requireContext(), this) } else { binding.rvBookshelf.layoutManager = GridLayoutManager(context, bookshelfLayout + 2) - booksAdapter = BooksAdapterGrid(requireContext(), this) } binding.rvBookshelf.adapter = booksAdapter booksAdapter.registerAdapterDataObserver(object : RecyclerView.AdapterDataObserver() { @@ -91,7 +98,8 @@ class BookshelfFragment2 : BaseBookshelfFragment(R.layout.fragment_bookshelf1), @SuppressLint("NotifyDataSetChanged") private fun initGroupData() { - launch { + groupsFlowJob?.cancel() + groupsFlowJob = launch { appDb.bookGroupDao.flowShow().collect { if (it != bookGroups) { bookGroups = it 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 fa6925e12..bbf0ade13 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 @@ -41,6 +41,7 @@ class RssFragment : VMBaseFragment(R.layout.fragment_rss), private val searchView: SearchView by lazy { binding.titleBar.findViewById(R.id.search_view) } + private var groupsFlowJob: Job? = null private var rssFlowJob: Job? = null private val groups = linkedSetOf() private var groupsMenu: SubMenu? = null @@ -117,7 +118,8 @@ class RssFragment : VMBaseFragment(R.layout.fragment_rss), } private fun initGroupData() { - launch { + groupsFlowJob?.cancel() + groupsFlowJob = launch { appDb.rssSourceDao.flowGroup().collect { groups.clear() it.map { group ->