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. 17
      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) { when (it.itemId) {
R.id.menu_disable_book_source -> { R.id.menu_disable_book_source -> {
callBack.disableSource(searchBook) callBack.disableSource(searchBook)
removeItem(searchBook)
} }
} }
true true

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

Loading…
Cancel
Save