This commit is contained in:
kunfei 2023-01-09 10:19:43 +08:00
parent ff11cbe780
commit 2c74cd7255
2 changed files with 33 additions and 18 deletions

View File

@ -12,6 +12,7 @@ import io.legado.app.data.entities.SearchBook
import io.legado.app.data.entities.SearchKeyword import io.legado.app.data.entities.SearchKeyword
import io.legado.app.help.config.AppConfig import io.legado.app.help.config.AppConfig
import io.legado.app.model.webBook.SearchModel import io.legado.app.model.webBook.SearchModel
import io.legado.app.utils.DelayLiveData
import io.legado.app.utils.toastOnUi import io.legado.app.utils.toastOnUi
import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.mapLatest import kotlinx.coroutines.flow.mapLatest
@ -21,15 +22,12 @@ class SearchViewModel(application: Application) : BaseViewModel(application) {
val handler = Handler(Looper.getMainLooper()) val handler = Handler(Looper.getMainLooper())
val bookshelf = hashSetOf<String>() val bookshelf = hashSetOf<String>()
val upAdapterLiveData = MutableLiveData<String>() val upAdapterLiveData = MutableLiveData<String>()
var searchBookLiveData = MutableLiveData<List<SearchBook>>() var searchBookLiveData = DelayLiveData<List<SearchBook>>(1000)
val searchScope: SearchScope = SearchScope(AppConfig.searchScope) val searchScope: SearchScope = SearchScope(AppConfig.searchScope)
var searchFinishCallback: ((isEmpty: Boolean) -> Unit)? = null var searchFinishCallback: ((isEmpty: Boolean) -> Unit)? = null
var isSearchLiveData = MutableLiveData<Boolean>() var isSearchLiveData = MutableLiveData<Boolean>()
var searchKey: String = "" var searchKey: String = ""
private var searchID = 0L private var searchID = 0L
private var searchResult = arrayListOf<SearchBook>()
private val sendRunnable = Runnable { upAdapter() }
private var postTime = 0L
private val searchModel = SearchModel(viewModelScope, object : SearchModel.CallBack { private val searchModel = SearchModel(viewModelScope, object : SearchModel.CallBack {
override fun getSearchScope(): SearchScope { override fun getSearchScope(): SearchScope {
@ -41,8 +39,7 @@ class SearchViewModel(application: Application) : BaseViewModel(application) {
} }
override fun onSearchSuccess(searchBooks: ArrayList<SearchBook>) { override fun onSearchSuccess(searchBooks: ArrayList<SearchBook>) {
searchResult = searchBooks searchBookLiveData.postValue(searchBooks)
upAdapter()
} }
override fun onSearchFinish(isEmpty: Boolean) { override fun onSearchFinish(isEmpty: Boolean) {
@ -73,18 +70,6 @@ class SearchViewModel(application: Application) : BaseViewModel(application) {
} }
} }
@Synchronized
private fun upAdapter() {
if (System.currentTimeMillis() >= postTime + 1000) {
handler.removeCallbacks(sendRunnable)
postTime = System.currentTimeMillis()
searchBookLiveData.postValue(searchResult)
} else {
handler.removeCallbacks(sendRunnable)
handler.postDelayed(sendRunnable, 1000 - System.currentTimeMillis() + postTime)
}
}
/** /**
* 开始搜索 * 开始搜索
*/ */

View File

@ -0,0 +1,30 @@
package io.legado.app.utils
import android.os.Handler
import android.os.Looper
import androidx.lifecycle.LiveData
class DelayLiveData<T>(val delay: Int) : LiveData<T>() {
private val handler = Handler(Looper.getMainLooper())
private val sendRunnable = Runnable { sendData() }
private var postTime = 0L
private var data: T? = null
private fun sendData() {
data?.let {
super.postValue(it)
}
}
public override fun postValue(value: T) {
data = value
if (System.currentTimeMillis() >= postTime + delay) {
handler.removeCallbacks(sendRunnable)
postTime = System.currentTimeMillis()
super.postValue(value)
} else {
handler.removeCallbacks(sendRunnable)
handler.postDelayed(sendRunnable, delay - System.currentTimeMillis() + postTime)
}
}
}