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 c7a5130d4..483fbbb75 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 @@ -10,7 +10,6 @@ import androidx.fragment.app.FragmentManager import androidx.recyclerview.widget.GridLayoutManager import io.legado.app.R import io.legado.app.base.BaseDialogFragment -import io.legado.app.constant.Theme import io.legado.app.databinding.DialogChangeCoverBinding import io.legado.app.lib.theme.primaryColor import io.legado.app.utils.applyTint @@ -89,7 +88,7 @@ class ChangeCoverDialog : BaseDialogFragment(), } else { stopMenuItem?.setIcon(R.drawable.ic_refresh_black_24dp) } - binding.toolBar.menu.applyTint(requireContext(), Theme.getTheme()) + binding.toolBar.menu.applyTint(requireContext()) }) viewModel.searchBooksLiveData.observe(viewLifecycleOwner, { adapter.setItems(it) diff --git a/app/src/main/java/io/legado/app/ui/book/changecover/ChangeCoverViewModel.kt b/app/src/main/java/io/legado/app/ui/book/changecover/ChangeCoverViewModel.kt index e00e52cf4..e0635376b 100644 --- a/app/src/main/java/io/legado/app/ui/book/changecover/ChangeCoverViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/changecover/ChangeCoverViewModel.kt @@ -2,6 +2,8 @@ package io.legado.app.ui.book.changecover import android.app.Application import android.os.Bundle +import android.os.Handler +import android.os.Looper import androidx.lifecycle.MutableLiveData import io.legado.app.App import io.legado.app.base.BaseViewModel @@ -14,19 +16,23 @@ import io.legado.app.model.webBook.WebBook import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.ExecutorCoroutineDispatcher import kotlinx.coroutines.asCoroutineDispatcher +import java.util.concurrent.CopyOnWriteArraySet import java.util.concurrent.Executors import kotlin.math.min class ChangeCoverViewModel(application: Application) : BaseViewModel(application) { private val threadCount = AppConfig.threadCount private var searchPool: ExecutorCoroutineDispatcher? = null + val handler = Handler(Looper.getMainLooper()) var name: String = "" var author: String = "" private var tasks = CompositeCoroutine() private var bookSourceList = arrayListOf() val searchStateData = MutableLiveData() val searchBooksLiveData = MutableLiveData>() - private val searchBooks = ArrayList() + private val searchBooks = CopyOnWriteArraySet() + private val sendRunnable = Runnable { upAdapter() } + private var postTime = 0L @Volatile private var searchIndex = -1 @@ -51,16 +57,27 @@ class ChangeCoverViewModel(application: Application) : BaseViewModel(application execute { App.db.searchBookDao.getEnableHasCover(name, author).let { searchBooks.addAll(it) + searchBooksLiveData.postValue(searchBooks.toList()) if (it.size <= 1) { - searchBooksLiveData.postValue(searchBooks) startSearch() - } else { - searchBooksLiveData.postValue(searchBooks) } } } } + @Synchronized + private fun upAdapter() { + if (System.currentTimeMillis() >= postTime + 500) { + handler.removeCallbacks(sendRunnable) + postTime = System.currentTimeMillis() + val books = searchBooks.toList() + searchBooksLiveData.postValue(books.sortedBy { it.originOrder }) + } else { + handler.removeCallbacks(sendRunnable) + handler.postDelayed(sendRunnable, 500) + } + } + private fun startSearch() { execute { bookSourceList.clear() @@ -93,7 +110,7 @@ class ChangeCoverViewModel(application: Application) : BaseViewModel(application App.db.searchBookDao.insert(searchBook) if (!searchBooks.contains(searchBook)) { searchBooks.add(searchBook) - searchBooksLiveData.postValue(searchBooks) + upAdapter() } } } @@ -121,6 +138,7 @@ class ChangeCoverViewModel(application: Application) : BaseViewModel(application startSearch() } else { tasks.clear() + searchStateData.postValue(false) } } diff --git a/app/src/main/java/io/legado/app/ui/book/changecover/CoverAdapter.kt b/app/src/main/java/io/legado/app/ui/book/changecover/CoverAdapter.kt index 6801e0ebc..2d181d28c 100644 --- a/app/src/main/java/io/legado/app/ui/book/changecover/CoverAdapter.kt +++ b/app/src/main/java/io/legado/app/ui/book/changecover/CoverAdapter.kt @@ -34,11 +34,9 @@ class CoverAdapter(context: Context, val callBack: CallBack) : binding: ItemCoverBinding, item: SearchBook, payloads: MutableList - ) { - binding.apply { - ivCover.load(item.coverUrl, item.name, item.author) - tvSource.text = item.originName - } + ) = with(binding) { + ivCover.load(item.coverUrl, item.name, item.author) + tvSource.text = item.originName } override fun registerListener(holder: ItemViewHolder, binding: ItemCoverBinding) { diff --git a/app/src/main/java/io/legado/app/ui/book/changesource/ChangeSourceViewModel.kt b/app/src/main/java/io/legado/app/ui/book/changesource/ChangeSourceViewModel.kt index e4d3e3c17..559e91982 100644 --- a/app/src/main/java/io/legado/app/ui/book/changesource/ChangeSourceViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/changesource/ChangeSourceViewModel.kt @@ -66,11 +66,9 @@ class ChangeSourceViewModel(application: Application) : BaseViewModel(applicatio upAdapter() App.db.searchBookDao.getChangeSourceSearch(name, author, searchGroup).let { searchBooks.addAll(it) + searchBooksLiveData.postValue(searchBooks.toList()) if (it.size <= 1) { - upAdapter() startSearch() - } else { - upAdapter() } } } @@ -85,7 +83,7 @@ class ChangeSourceViewModel(application: Application) : BaseViewModel(applicatio searchBooksLiveData.postValue(books.sortedBy { it.originOrder }) } else { handler.removeCallbacks(sendRunnable) - handler.postDelayed(sendRunnable, 500 - System.currentTimeMillis() + postTime) + handler.postDelayed(sendRunnable, 500) } }