feat: 优化

pull/241/head
gedoor 5 years ago
parent 2bb4698b5e
commit eb163896ce
  1. 1
      app/src/main/java/io/legado/app/ui/book/changesource/ChangeSourceAdapter.kt
  2. 13
      app/src/main/java/io/legado/app/ui/book/changesource/ChangeSourceViewModel.kt

@ -60,7 +60,6 @@ class ChangeSourceAdapter(context: Context, val callBack: CallBack) :
when (it.itemId) {
R.id.menu_disable_book_source -> {
callBack.disableSource(searchBook)
removeItem(searchBook)
}
}
true

@ -2,6 +2,7 @@ package io.legado.app.ui.book.changesource
import android.app.Application
import android.os.Bundle
import android.os.Handler
import androidx.lifecycle.MutableLiveData
import io.legado.app.App
import io.legado.app.R
@ -22,6 +23,7 @@ import java.util.concurrent.Executors
class ChangeSourceViewModel(application: Application) : BaseViewModel(application) {
private var searchPool =
Executors.newFixedThreadPool(AppConfig.threadCount).asCoroutineDispatcher()
val handler = Handler()
val searchStateData = MutableLiveData<Boolean>()
val searchBooksLiveData = MutableLiveData<List<SearchBook>>()
var name: String = ""
@ -29,6 +31,9 @@ class ChangeSourceViewModel(application: Application) : BaseViewModel(applicatio
private var task: Coroutine<*>? = null
private var screenKey: String = ""
private val searchBooks = hashSetOf<SearchBook>()
private var postTime = 0L
private val sendRunnable = Runnable { upAdapter() }
fun initData(arguments: Bundle?) {
arguments?.let { bundle ->
@ -55,9 +60,16 @@ class ChangeSourceViewModel(application: Application) : BaseViewModel(applicatio
}
}
@Synchronized
private fun upAdapter() {
if (System.currentTimeMillis() > postTime + 500) {
postTime = System.currentTimeMillis()
handler.removeCallbacks(sendRunnable)
val books = searchBooks.toList()
searchBooksLiveData.postValue(books.sortedBy { it.originOrder })
} else {
handler.postDelayed(sendRunnable, 500)
}
}
private fun searchFinish(searchBook: SearchBook) {
@ -172,6 +184,7 @@ class ChangeSourceViewModel(application: Application) : BaseViewModel(applicatio
App.db.bookSourceDao().update(source)
}
searchBooks.remove(searchBook)
upAdapter()
}
}

Loading…
Cancel
Save