mirror of
https://github.com/gedoor/legado.git
synced 2024-09-01 09:34:25 +08:00
优化
This commit is contained in:
parent
ff11cbe780
commit
2c74cd7255
@ -12,6 +12,7 @@ import io.legado.app.data.entities.SearchBook
|
||||
import io.legado.app.data.entities.SearchKeyword
|
||||
import io.legado.app.help.config.AppConfig
|
||||
import io.legado.app.model.webBook.SearchModel
|
||||
import io.legado.app.utils.DelayLiveData
|
||||
import io.legado.app.utils.toastOnUi
|
||||
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
||||
import kotlinx.coroutines.flow.mapLatest
|
||||
@ -21,15 +22,12 @@ class SearchViewModel(application: Application) : BaseViewModel(application) {
|
||||
val handler = Handler(Looper.getMainLooper())
|
||||
val bookshelf = hashSetOf<String>()
|
||||
val upAdapterLiveData = MutableLiveData<String>()
|
||||
var searchBookLiveData = MutableLiveData<List<SearchBook>>()
|
||||
var searchBookLiveData = DelayLiveData<List<SearchBook>>(1000)
|
||||
val searchScope: SearchScope = SearchScope(AppConfig.searchScope)
|
||||
var searchFinishCallback: ((isEmpty: Boolean) -> Unit)? = null
|
||||
var isSearchLiveData = MutableLiveData<Boolean>()
|
||||
var searchKey: String = ""
|
||||
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 {
|
||||
|
||||
override fun getSearchScope(): SearchScope {
|
||||
@ -41,8 +39,7 @@ class SearchViewModel(application: Application) : BaseViewModel(application) {
|
||||
}
|
||||
|
||||
override fun onSearchSuccess(searchBooks: ArrayList<SearchBook>) {
|
||||
searchResult = searchBooks
|
||||
upAdapter()
|
||||
searchBookLiveData.postValue(searchBooks)
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 开始搜索
|
||||
*/
|
||||
|
30
app/src/main/java/io/legado/app/utils/DelayLiveData.kt
Normal file
30
app/src/main/java/io/legado/app/utils/DelayLiveData.kt
Normal 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)
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user