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) + } + } + /** * 开始搜索 */