From 7097f4c0ec9d5f693992d0bed3dd0568374c1769 Mon Sep 17 00:00:00 2001 From: kunfei Date: Sun, 27 Feb 2022 10:00:02 +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 | 6 +----- .../app/ui/book/changecover/ChangeCoverDialog.kt | 14 +++++--------- .../book/changesource/ChangeBookSourceDialog.kt | 13 +++++-------- .../changesource/ChangeChapterSourceDialog.kt | 13 +++++-------- .../legado/app/ui/book/search/SearchActivity.kt | 16 ++++++++-------- 5 files changed, 24 insertions(+), 38 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 55fd9b03e..c66078be9 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 @@ -48,11 +48,7 @@ abstract class DiffRecyclerAdapter(protected val context fun setItems(items: List?) { kotlin.runCatching { - if (items == null) { - asyncListDiffer.submitList(null) - } else { - asyncListDiffer.submitList(ArrayList(items)) - } + asyncListDiffer.submitList(items?.toMutableList()) } } 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 d891165d6..cc9880855 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 @@ -6,7 +6,7 @@ import android.view.View import android.view.ViewGroup import androidx.appcompat.widget.Toolbar import androidx.fragment.app.viewModels -import androidx.lifecycle.whenStarted +import androidx.lifecycle.lifecycleScope import androidx.recyclerview.widget.GridLayoutManager import io.legado.app.R import io.legado.app.base.BaseDialogFragment @@ -15,10 +15,8 @@ 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 /** * 换封面 @@ -68,12 +66,10 @@ class ChangeCoverDialog() : BaseDialogFragment(R.layout.dialog_change_cover), } private fun initData() { - launch(Dispatchers.Default) { - whenStarted { - viewModel.dataFlow.conflate().collect { - adapter.setItems(it) - delay(1000) - } + lifecycleScope.launchWhenStarted { + viewModel.dataFlow.conflate().collect { + adapter.setItems(it) + delay(1000) } } } 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 319a5f0ba..d44a4ca42 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 @@ -9,7 +9,7 @@ import androidx.appcompat.widget.SearchView import androidx.appcompat.widget.Toolbar import androidx.core.os.bundleOf import androidx.fragment.app.viewModels -import androidx.lifecycle.whenStarted +import androidx.lifecycle.lifecycleScope import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView import io.legado.app.R @@ -29,7 +29,6 @@ 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 @@ -146,12 +145,10 @@ class ChangeBookSourceDialog() : BaseDialogFragment(R.layout.dialog_book_change_ } binding.toolBar.menu.applyTint(requireContext()) } - launch(Dispatchers.Default) { - whenStarted { - viewModel.searchDataFlow.conflate().collect { - adapter.setItems(it) - delay(1000) - } + lifecycleScope.launchWhenStarted { + viewModel.searchDataFlow.conflate().collect { + adapter.setItems(it) + delay(1000) } } launch { 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 3d7024729..1c4cc3254 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 @@ -9,7 +9,7 @@ import androidx.appcompat.widget.Toolbar import androidx.core.os.bundleOf import androidx.core.view.isVisible import androidx.fragment.app.viewModels -import androidx.lifecycle.whenStarted +import androidx.lifecycle.lifecycleScope import androidx.recyclerview.widget.RecyclerView import io.legado.app.R import io.legado.app.base.BaseDialogFragment @@ -31,7 +31,6 @@ 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 @@ -178,12 +177,10 @@ class ChangeChapterSourceDialog() : BaseDialogFragment(R.layout.dialog_chapter_c } binding.toolBar.menu.applyTint(requireContext()) } - launch(Dispatchers.Default) { - whenStarted { - viewModel.searchDataFlow.conflate().collect { - searchBookAdapter.setItems(it) - delay(1000) - } + lifecycleScope.launchWhenStarted { + viewModel.searchDataFlow.conflate().collect { + searchBookAdapter.setItems(it) + delay(1000) } } launch { 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 5b20b9d51..371a55657 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 @@ -11,7 +11,6 @@ import android.widget.TextView import androidx.activity.viewModels import androidx.appcompat.widget.SearchView import androidx.lifecycle.lifecycleScope -import androidx.lifecycle.whenStarted import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView import com.google.android.flexbox.FlexboxLayoutManager @@ -29,9 +28,12 @@ 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, @@ -195,12 +197,10 @@ class SearchActivity : VMBaseActivity