From 50d5845638b967b4c7d0bed4ec9ec5a8deca8f86 Mon Sep 17 00:00:00 2001 From: kunfei Date: Tue, 10 Jan 2023 09:38:30 +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 --- .../java/io/legado/app/ui/book/search/SearchViewModel.kt | 4 ++-- .../app/utils/{DelayLiveData.kt => ConflateLiveData.kt} | 6 +++++- 2 files changed, 7 insertions(+), 3 deletions(-) rename app/src/main/java/io/legado/app/utils/{DelayLiveData.kt => ConflateLiveData.kt} (85%) 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 f05b4f3eb..390a5c74c 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,7 +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.ConflateLiveData import io.legado.app.utils.toastOnUi import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.flow.mapLatest @@ -22,7 +22,7 @@ class SearchViewModel(application: Application) : BaseViewModel(application) { val handler = Handler(Looper.getMainLooper()) val bookshelf = hashSetOf() val upAdapterLiveData = MutableLiveData() - var searchBookLiveData = DelayLiveData>(1000) + var searchBookLiveData = ConflateLiveData>(1000) val searchScope: SearchScope = SearchScope(AppConfig.searchScope) var searchFinishCallback: ((isEmpty: Boolean) -> Unit)? = null var isSearchLiveData = MutableLiveData() diff --git a/app/src/main/java/io/legado/app/utils/DelayLiveData.kt b/app/src/main/java/io/legado/app/utils/ConflateLiveData.kt similarity index 85% rename from app/src/main/java/io/legado/app/utils/DelayLiveData.kt rename to app/src/main/java/io/legado/app/utils/ConflateLiveData.kt index 975f8fbc1..6e30ba16e 100644 --- a/app/src/main/java/io/legado/app/utils/DelayLiveData.kt +++ b/app/src/main/java/io/legado/app/utils/ConflateLiveData.kt @@ -4,7 +4,11 @@ import android.os.Handler import android.os.Looper import androidx.lifecycle.LiveData -class DelayLiveData(val delay: Int) : LiveData() { +/** + * 合并发送,只发送最新数据 + * @param delay 发送时间间隔 + */ +class ConflateLiveData(val delay: Int) : LiveData() { private val handler = Handler(Looper.getMainLooper()) private val sendRunnable = Runnable { sendData() } private var postTime = 0L