优化代码

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 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)

@ -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<BookSource>()
val searchStateData = MutableLiveData<Boolean>()
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
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)
}
}

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

@ -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)
}
}

Loading…
Cancel
Save