From 2be79303aa63aaa8915ac12c113601085e9b01ea Mon Sep 17 00:00:00 2001 From: kunfei Date: Sun, 27 Feb 2022 09:31:36 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app/base/adapter/DiffRecyclerAdapter.kt | 66 ++++++++----------- .../ui/book/changecover/ChangeCoverDialog.kt | 3 +- .../changesource/ChangeBookSourceDialog.kt | 3 +- .../changesource/ChangeChapterSourceDialog.kt | 3 +- .../app/ui/book/search/SearchActivity.kt | 7 +- .../bookshelf/style1/books/BooksFragment.kt | 4 +- 6 files changed, 36 insertions(+), 50 deletions(-) diff --git a/app/src/main/java/io/legado/app/base/adapter/DiffRecyclerAdapter.kt b/app/src/main/java/io/legado/app/base/adapter/DiffRecyclerAdapter.kt index e7a533067..55fd9b03e 100644 --- a/app/src/main/java/io/legado/app/base/adapter/DiffRecyclerAdapter.kt +++ b/app/src/main/java/io/legado/app/base/adapter/DiffRecyclerAdapter.kt @@ -8,8 +8,6 @@ import androidx.recyclerview.widget.DiffUtil import androidx.recyclerview.widget.GridLayoutManager import androidx.recyclerview.widget.RecyclerView import androidx.viewbinding.ViewBinding -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.withContext import splitties.views.onLongClick /** @@ -48,63 +46,51 @@ abstract class DiffRecyclerAdapter(protected val context recyclerView.adapter = this } - suspend fun setItems(items: List?) { - withContext(Dispatchers.Default) { - synchronized(asyncListDiffer) { - kotlin.runCatching { - if (items == null) { - asyncListDiffer.submitList(null) - } else { - asyncListDiffer.submitList(ArrayList(items)) - } - } + fun setItems(items: List?) { + kotlin.runCatching { + if (items == null) { + asyncListDiffer.submitList(null) + } else { + asyncListDiffer.submitList(ArrayList(items)) } } } fun setItem(position: Int, item: ITEM) { - synchronized(asyncListDiffer) { - kotlin.runCatching { - asyncListDiffer.currentList[position] = item - notifyItemChanged(position) - } + kotlin.runCatching { + asyncListDiffer.currentList[position] = item + notifyItemChanged(position) } } fun updateItem(item: ITEM) { - synchronized(asyncListDiffer) { - kotlin.runCatching { - val index = asyncListDiffer.currentList.indexOf(item) - if (index >= 0) { - asyncListDiffer.currentList[index] = item - notifyItemChanged(index) - } + kotlin.runCatching { + val index = asyncListDiffer.currentList.indexOf(item) + if (index >= 0) { + asyncListDiffer.currentList[index] = item + notifyItemChanged(index) } } } fun updateItem(position: Int, payload: Any) { - synchronized(asyncListDiffer) { - kotlin.runCatching { - val size = itemCount - if (position in 0 until size) { - notifyItemChanged(position, payload) - } + kotlin.runCatching { + val size = itemCount + if (position in 0 until size) { + notifyItemChanged(position, payload) } } } fun updateItems(fromPosition: Int, toPosition: Int, payloads: Any) { - synchronized(asyncListDiffer) { - kotlin.runCatching { - val size = itemCount - if (fromPosition in 0 until size && toPosition in 0 until size) { - notifyItemRangeChanged( - fromPosition, - toPosition - fromPosition + 1, - payloads - ) - } + kotlin.runCatching { + val size = itemCount + if (fromPosition in 0 until size && toPosition in 0 until size) { + notifyItemRangeChanged( + fromPosition, + toPosition - fromPosition + 1, + payloads + ) } } } diff --git a/app/src/main/java/io/legado/app/ui/book/changecover/ChangeCoverDialog.kt b/app/src/main/java/io/legado/app/ui/book/changecover/ChangeCoverDialog.kt index 245634f06..d891165d6 100644 --- a/app/src/main/java/io/legado/app/ui/book/changecover/ChangeCoverDialog.kt +++ b/app/src/main/java/io/legado/app/ui/book/changecover/ChangeCoverDialog.kt @@ -15,6 +15,7 @@ import io.legado.app.lib.theme.primaryColor import io.legado.app.utils.applyTint import io.legado.app.utils.setLayout import io.legado.app.utils.viewbindingdelegate.viewBinding +import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.delay import kotlinx.coroutines.flow.conflate import kotlinx.coroutines.launch @@ -67,7 +68,7 @@ class ChangeCoverDialog() : BaseDialogFragment(R.layout.dialog_change_cover), } private fun initData() { - launch { + launch(Dispatchers.Default) { whenStarted { viewModel.dataFlow.conflate().collect { adapter.setItems(it) diff --git a/app/src/main/java/io/legado/app/ui/book/changesource/ChangeBookSourceDialog.kt b/app/src/main/java/io/legado/app/ui/book/changesource/ChangeBookSourceDialog.kt index 2f5daca35..319a5f0ba 100644 --- a/app/src/main/java/io/legado/app/ui/book/changesource/ChangeBookSourceDialog.kt +++ b/app/src/main/java/io/legado/app/ui/book/changesource/ChangeBookSourceDialog.kt @@ -29,6 +29,7 @@ import io.legado.app.ui.book.source.manage.BookSourceActivity import io.legado.app.ui.widget.recycler.VerticalDivider import io.legado.app.utils.* import io.legado.app.utils.viewbindingdelegate.viewBinding +import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.delay import kotlinx.coroutines.flow.conflate import kotlinx.coroutines.launch @@ -145,7 +146,7 @@ class ChangeBookSourceDialog() : BaseDialogFragment(R.layout.dialog_book_change_ } binding.toolBar.menu.applyTint(requireContext()) } - launch { + launch(Dispatchers.Default) { whenStarted { viewModel.searchDataFlow.conflate().collect { adapter.setItems(it) diff --git a/app/src/main/java/io/legado/app/ui/book/changesource/ChangeChapterSourceDialog.kt b/app/src/main/java/io/legado/app/ui/book/changesource/ChangeChapterSourceDialog.kt index b57de5f64..3d7024729 100644 --- a/app/src/main/java/io/legado/app/ui/book/changesource/ChangeChapterSourceDialog.kt +++ b/app/src/main/java/io/legado/app/ui/book/changesource/ChangeChapterSourceDialog.kt @@ -31,6 +31,7 @@ import io.legado.app.ui.book.source.manage.BookSourceActivity import io.legado.app.ui.widget.recycler.VerticalDivider import io.legado.app.utils.* import io.legado.app.utils.viewbindingdelegate.viewBinding +import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.delay import kotlinx.coroutines.flow.conflate import kotlinx.coroutines.launch @@ -177,7 +178,7 @@ class ChangeChapterSourceDialog() : BaseDialogFragment(R.layout.dialog_chapter_c } binding.toolBar.menu.applyTint(requireContext()) } - launch { + launch(Dispatchers.Default) { whenStarted { viewModel.searchDataFlow.conflate().collect { searchBookAdapter.setItems(it) diff --git a/app/src/main/java/io/legado/app/ui/book/search/SearchActivity.kt b/app/src/main/java/io/legado/app/ui/book/search/SearchActivity.kt index ca986697c..5b20b9d51 100644 --- a/app/src/main/java/io/legado/app/ui/book/search/SearchActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/search/SearchActivity.kt @@ -29,12 +29,9 @@ import io.legado.app.ui.book.source.manage.BookSourceActivity import io.legado.app.ui.widget.recycler.LoadMoreView import io.legado.app.utils.* import io.legado.app.utils.viewbindingdelegate.viewBinding +import kotlinx.coroutines.* import kotlinx.coroutines.Dispatchers.IO -import kotlinx.coroutines.Job -import kotlinx.coroutines.delay import kotlinx.coroutines.flow.conflate -import kotlinx.coroutines.launch -import kotlinx.coroutines.withContext class SearchActivity : VMBaseActivity(), BookAdapter.CallBack, @@ -198,7 +195,7 @@ class SearchActivity : VMBaseActivity appDb.bookDao.flowAll() AppConst.bookGroupLocalId -> appDb.bookDao.flowLocal()