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