From 59335a00c8777e1bb0975b2425b8b5a1fa546986 Mon Sep 17 00:00:00 2001 From: gedoor Date: Sat, 13 Jun 2020 16:11:27 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../changesource/ChangeSourceViewModel.kt | 2 +- .../app/ui/book/search/SearchActivity.kt | 15 ++++-------- .../app/ui/book/search/SearchViewModel.kt | 23 +++++++++++++++---- 3 files changed, 25 insertions(+), 15 deletions(-) diff --git a/app/src/main/java/io/legado/app/ui/book/changesource/ChangeSourceViewModel.kt b/app/src/main/java/io/legado/app/ui/book/changesource/ChangeSourceViewModel.kt index 1b29f0e01..605822af8 100644 --- a/app/src/main/java/io/legado/app/ui/book/changesource/ChangeSourceViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/changesource/ChangeSourceViewModel.kt @@ -63,8 +63,8 @@ class ChangeSourceViewModel(application: Application) : BaseViewModel(applicatio @Synchronized private fun upAdapter() { if (System.currentTimeMillis() > postTime + 500) { - postTime = System.currentTimeMillis() handler.removeCallbacks(sendRunnable) + postTime = System.currentTimeMillis() val books = searchBooks.toList() searchBooksLiveData.postValue(books.sortedBy { it.originOrder }) } else { 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 6daa679cc..e7e0d691c 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 @@ -52,7 +52,6 @@ class SearchActivity : VMBaseActivity(R.layout.activity_book_se private var menu: Menu? = null private var precisionSearchMenuItem: MenuItem? = null private var groups = linkedSetOf() - private var refreshTime = System.currentTimeMillis() override fun onActivityCreated(savedInstanceState: Bundle?) { initRecyclerView() @@ -181,14 +180,13 @@ class SearchActivity : VMBaseActivity(R.layout.activity_book_se upGroupMenu() }) viewModel.searchBookLiveData.observe(this, Observer { - upSearchItems(it, false) + upSearchItems(it) }) viewModel.isSearchLiveData.observe(this, Observer { if (it) { startSearch() } else { searchFinally() - upSearchItems(viewModel.searchBooks, true) } }) } @@ -284,14 +282,11 @@ class SearchActivity : VMBaseActivity(R.layout.activity_book_se * 更新搜索结果 */ @Synchronized - private fun upSearchItems(items: List, isMandatoryUpdate: Boolean) { + private fun upSearchItems(items: List) { val searchItems = ArrayList(items) - if (isMandatoryUpdate || System.currentTimeMillis() - refreshTime > 500) { - refreshTime = System.currentTimeMillis() - val diffResult = - DiffUtil.calculateDiff(DiffCallBack(adapter.getItems(), searchItems)) - adapter.setItems(searchItems, diffResult) - } + val diffResult = + DiffUtil.calculateDiff(DiffCallBack(adapter.getItems(), searchItems)) + adapter.setItems(searchItems, diffResult) } /** 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 ccfc5dd15..5e7f60026 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 @@ -1,6 +1,7 @@ package io.legado.app.ui.book.search import android.app.Application +import android.os.Handler import androidx.lifecycle.MutableLiveData import io.legado.app.App import io.legado.app.base.BaseViewModel @@ -12,15 +13,18 @@ import io.legado.app.utils.getPrefBoolean import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.isActive -class SearchViewModel(application: Application) : BaseViewModel(application) - , SearchBookModel.CallBack { +class SearchViewModel(application: Application) : BaseViewModel(application), + SearchBookModel.CallBack { + val handler = Handler() private val searchBookModel = SearchBookModel(this, this) var isSearchLiveData = MutableLiveData() var searchBookLiveData = MutableLiveData>() var searchKey: String = "" var isLoading = false - var searchBooks = arrayListOf() + private var searchBooks = arrayListOf() private var searchID = 0L + private var postTime = 0L + private val sendRunnable = Runnable { upAdapter() } /** * 开始搜索 @@ -36,6 +40,17 @@ class SearchViewModel(application: Application) : BaseViewModel(application) searchBookModel.search(searchID, searchKey) } + @Synchronized + private fun upAdapter() { + if (System.currentTimeMillis() > postTime + 500) { + handler.removeCallbacks(sendRunnable) + postTime = System.currentTimeMillis() + searchBookLiveData.postValue(searchBooks) + } else { + handler.postDelayed(sendRunnable, 500) + } + } + override fun onSearchStart() { isSearchLiveData.postValue(true) } @@ -151,7 +166,7 @@ class SearchViewModel(application: Application) : BaseViewModel(application) }) if (!scope.isActive) return searchBooks = copyDataS - searchBookLiveData.postValue(copyDataS) + upAdapter() } }