优化代码

pull/743/head
gedoor 4 years ago
parent dac6ca99cb
commit 961784403c
  1. 3
      app/src/main/java/io/legado/app/ui/book/changecover/ChangeCoverDialog.kt
  2. 28
      app/src/main/java/io/legado/app/ui/book/changecover/ChangeCoverViewModel.kt
  3. 4
      app/src/main/java/io/legado/app/ui/book/changecover/CoverAdapter.kt
  4. 6
      app/src/main/java/io/legado/app/ui/book/changesource/ChangeSourceViewModel.kt

@ -10,7 +10,6 @@ import androidx.fragment.app.FragmentManager
import androidx.recyclerview.widget.GridLayoutManager import androidx.recyclerview.widget.GridLayoutManager
import io.legado.app.R import io.legado.app.R
import io.legado.app.base.BaseDialogFragment import io.legado.app.base.BaseDialogFragment
import io.legado.app.constant.Theme
import io.legado.app.databinding.DialogChangeCoverBinding import io.legado.app.databinding.DialogChangeCoverBinding
import io.legado.app.lib.theme.primaryColor import io.legado.app.lib.theme.primaryColor
import io.legado.app.utils.applyTint import io.legado.app.utils.applyTint
@ -89,7 +88,7 @@ class ChangeCoverDialog : BaseDialogFragment(),
} else { } else {
stopMenuItem?.setIcon(R.drawable.ic_refresh_black_24dp) stopMenuItem?.setIcon(R.drawable.ic_refresh_black_24dp)
} }
binding.toolBar.menu.applyTint(requireContext(), Theme.getTheme()) binding.toolBar.menu.applyTint(requireContext())
}) })
viewModel.searchBooksLiveData.observe(viewLifecycleOwner, { viewModel.searchBooksLiveData.observe(viewLifecycleOwner, {
adapter.setItems(it) adapter.setItems(it)

@ -2,6 +2,8 @@ package io.legado.app.ui.book.changecover
import android.app.Application import android.app.Application
import android.os.Bundle import android.os.Bundle
import android.os.Handler
import android.os.Looper
import androidx.lifecycle.MutableLiveData import androidx.lifecycle.MutableLiveData
import io.legado.app.App import io.legado.app.App
import io.legado.app.base.BaseViewModel import io.legado.app.base.BaseViewModel
@ -14,19 +16,23 @@ import io.legado.app.model.webBook.WebBook
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExecutorCoroutineDispatcher import kotlinx.coroutines.ExecutorCoroutineDispatcher
import kotlinx.coroutines.asCoroutineDispatcher import kotlinx.coroutines.asCoroutineDispatcher
import java.util.concurrent.CopyOnWriteArraySet
import java.util.concurrent.Executors import java.util.concurrent.Executors
import kotlin.math.min import kotlin.math.min
class ChangeCoverViewModel(application: Application) : BaseViewModel(application) { class ChangeCoverViewModel(application: Application) : BaseViewModel(application) {
private val threadCount = AppConfig.threadCount private val threadCount = AppConfig.threadCount
private var searchPool: ExecutorCoroutineDispatcher? = null private var searchPool: ExecutorCoroutineDispatcher? = null
val handler = Handler(Looper.getMainLooper())
var name: String = "" var name: String = ""
var author: String = "" var author: String = ""
private var tasks = CompositeCoroutine() private var tasks = CompositeCoroutine()
private var bookSourceList = arrayListOf<BookSource>() private var bookSourceList = arrayListOf<BookSource>()
val searchStateData = MutableLiveData<Boolean>() val searchStateData = MutableLiveData<Boolean>()
val searchBooksLiveData = MutableLiveData<List<SearchBook>>() val searchBooksLiveData = MutableLiveData<List<SearchBook>>()
private val searchBooks = ArrayList<SearchBook>() private val searchBooks = CopyOnWriteArraySet<SearchBook>()
private val sendRunnable = Runnable { upAdapter() }
private var postTime = 0L
@Volatile @Volatile
private var searchIndex = -1 private var searchIndex = -1
@ -51,16 +57,27 @@ class ChangeCoverViewModel(application: Application) : BaseViewModel(application
execute { execute {
App.db.searchBookDao.getEnableHasCover(name, author).let { App.db.searchBookDao.getEnableHasCover(name, author).let {
searchBooks.addAll(it) searchBooks.addAll(it)
searchBooksLiveData.postValue(searchBooks.toList())
if (it.size <= 1) { if (it.size <= 1) {
searchBooksLiveData.postValue(searchBooks)
startSearch() 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() { private fun startSearch() {
execute { execute {
bookSourceList.clear() bookSourceList.clear()
@ -93,7 +110,7 @@ class ChangeCoverViewModel(application: Application) : BaseViewModel(application
App.db.searchBookDao.insert(searchBook) App.db.searchBookDao.insert(searchBook)
if (!searchBooks.contains(searchBook)) { if (!searchBooks.contains(searchBook)) {
searchBooks.add(searchBook) searchBooks.add(searchBook)
searchBooksLiveData.postValue(searchBooks) upAdapter()
} }
} }
} }
@ -121,6 +138,7 @@ class ChangeCoverViewModel(application: Application) : BaseViewModel(application
startSearch() startSearch()
} else { } else {
tasks.clear() tasks.clear()
searchStateData.postValue(false)
} }
} }

@ -34,12 +34,10 @@ class CoverAdapter(context: Context, val callBack: CallBack) :
binding: ItemCoverBinding, binding: ItemCoverBinding,
item: SearchBook, item: SearchBook,
payloads: MutableList<Any> payloads: MutableList<Any>
) { ) = with(binding) {
binding.apply {
ivCover.load(item.coverUrl, item.name, item.author) ivCover.load(item.coverUrl, item.name, item.author)
tvSource.text = item.originName tvSource.text = item.originName
} }
}
override fun registerListener(holder: ItemViewHolder, binding: ItemCoverBinding) { override fun registerListener(holder: ItemViewHolder, binding: ItemCoverBinding) {
holder.itemView.apply { holder.itemView.apply {

@ -66,11 +66,9 @@ class ChangeSourceViewModel(application: Application) : BaseViewModel(applicatio
upAdapter() upAdapter()
App.db.searchBookDao.getChangeSourceSearch(name, author, searchGroup).let { App.db.searchBookDao.getChangeSourceSearch(name, author, searchGroup).let {
searchBooks.addAll(it) searchBooks.addAll(it)
searchBooksLiveData.postValue(searchBooks.toList())
if (it.size <= 1) { if (it.size <= 1) {
upAdapter()
startSearch() startSearch()
} else {
upAdapter()
} }
} }
} }
@ -85,7 +83,7 @@ class ChangeSourceViewModel(application: Application) : BaseViewModel(applicatio
searchBooksLiveData.postValue(books.sortedBy { it.originOrder }) searchBooksLiveData.postValue(books.sortedBy { it.originOrder })
} else { } else {
handler.removeCallbacks(sendRunnable) handler.removeCallbacks(sendRunnable)
handler.postDelayed(sendRunnable, 500 - System.currentTimeMillis() + postTime) handler.postDelayed(sendRunnable, 500)
} }
} }

Loading…
Cancel
Save