From 06352e3b3a8a012dd1de062dccb2fe65935f6b73 Mon Sep 17 00:00:00 2001 From: kunfei Date: Sun, 8 Jan 2023 19:01:15 +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/SearchActivity.kt | 9 +--- .../app/ui/book/search/SearchViewModel.kt | 46 ++++++++++--------- 2 files changed, 27 insertions(+), 28 deletions(-) diff --git a/app/src/main/java/io/legado/app/ui/book/search/SearchActivity.kt b/app/src/main/java/io/legado/app/ui/book/search/SearchActivity.kt index 49d50c26a..19b60b0a6 100644 --- a/app/src/main/java/io/legado/app/ui/book/search/SearchActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/search/SearchActivity.kt @@ -11,7 +11,6 @@ import android.widget.TextView import androidx.activity.viewModels import androidx.appcompat.widget.SearchView import androidx.core.view.isVisible -import androidx.lifecycle.lifecycleScope import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView import com.google.android.flexbox.FlexboxLayoutManager @@ -31,7 +30,6 @@ import io.legado.app.utils.* import io.legado.app.utils.viewbindingdelegate.viewBinding import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.Job -import kotlinx.coroutines.delay import kotlinx.coroutines.flow.conflate import kotlinx.coroutines.launch import kotlinx.coroutines.withContext @@ -277,11 +275,8 @@ class SearchActivity : VMBaseActivity() val upAdapterLiveData = MutableLiveData() + var searchBookLiveData = MutableLiveData>() 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 { @@ -34,7 +41,8 @@ class SearchViewModel(application: Application) : BaseViewModel(application) { } override fun onSearchSuccess(searchBooks: ArrayList) { - searchFlowCallBack?.invoke(searchBooks) + searchResult = searchBooks + upAdapter() } override fun onSearchFinish(isEmpty: Boolean) { @@ -50,22 +58,6 @@ class SearchViewModel(application: Application) : BaseViewModel(application) { } }) - var searchFinishCallback: ((isEmpty: Boolean) -> Unit)? = null - var isSearchLiveData = MutableLiveData() - var searchKey: String = "" - private var searchID = 0L - private var searchFlowCallBack: ((searchBooks: ArrayList) -> Unit)? = null - val searchDataFlow = callbackFlow { - - searchFlowCallBack = { - trySend(ArrayList(it)) - } - - awaitClose { - searchFlowCallBack = null - } - }.flowOn(IO) - init { execute { @@ -81,6 +73,18 @@ 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) + } + } + /** * 开始搜索 */