From 2c74cd7255e61bd7bda83641f24bd5eaf8ba450b Mon Sep 17 00:00:00 2001 From: kunfei Date: Mon, 9 Jan 2023 10:19:43 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app/ui/book/search/SearchViewModel.kt | 21 ++----------- .../java/io/legado/app/utils/DelayLiveData.kt | 30 +++++++++++++++++++ 2 files changed, 33 insertions(+), 18 deletions(-) create mode 100644 app/src/main/java/io/legado/app/utils/DelayLiveData.kt diff --git a/app/src/main/java/io/legado/app/ui/book/search/SearchViewModel.kt b/app/src/main/java/io/legado/app/ui/book/search/SearchViewModel.kt index bab06a292..f05b4f3eb 100644 --- a/app/src/main/java/io/legado/app/ui/book/search/SearchViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/search/SearchViewModel.kt @@ -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() val upAdapterLiveData = MutableLiveData() - var searchBookLiveData = MutableLiveData>() + var searchBookLiveData = DelayLiveData>(1000) val searchScope: SearchScope = SearchScope(AppConfig.searchScope) var searchFinishCallback: ((isEmpty: Boolean) -> Unit)? = null var isSearchLiveData = MutableLiveData() var searchKey: String = "" private var searchID = 0L - private var searchResult = arrayListOf() - 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) { - 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) - } - } - /** * 开始搜索 */ diff --git a/app/src/main/java/io/legado/app/utils/DelayLiveData.kt b/app/src/main/java/io/legado/app/utils/DelayLiveData.kt new file mode 100644 index 000000000..d2f97d727 --- /dev/null +++ b/app/src/main/java/io/legado/app/utils/DelayLiveData.kt @@ -0,0 +1,30 @@ +package io.legado.app.utils + +import android.os.Handler +import android.os.Looper +import androidx.lifecycle.LiveData + +class DelayLiveData(val delay: Int) : LiveData() { + 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) + } + } +} \ No newline at end of file