From eb163896ce722d0e177402d0a9080ef2a07eddd2 Mon Sep 17 00:00:00 2001 From: gedoor Date: Fri, 12 Jun 2020 22:23:14 +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 --- .../ui/book/changesource/ChangeSourceAdapter.kt | 1 - .../book/changesource/ChangeSourceViewModel.kt | 17 +++++++++++++++-- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/io/legado/app/ui/book/changesource/ChangeSourceAdapter.kt b/app/src/main/java/io/legado/app/ui/book/changesource/ChangeSourceAdapter.kt index 3a148a2c2..12e63a907 100644 --- a/app/src/main/java/io/legado/app/ui/book/changesource/ChangeSourceAdapter.kt +++ b/app/src/main/java/io/legado/app/ui/book/changesource/ChangeSourceAdapter.kt @@ -60,7 +60,6 @@ class ChangeSourceAdapter(context: Context, val callBack: CallBack) : when (it.itemId) { R.id.menu_disable_book_source -> { callBack.disableSource(searchBook) - removeItem(searchBook) } } true 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 e5adf9e85..1b29f0e01 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 @@ -2,6 +2,7 @@ package io.legado.app.ui.book.changesource import android.app.Application import android.os.Bundle +import android.os.Handler import androidx.lifecycle.MutableLiveData import io.legado.app.App import io.legado.app.R @@ -22,6 +23,7 @@ import java.util.concurrent.Executors class ChangeSourceViewModel(application: Application) : BaseViewModel(application) { private var searchPool = Executors.newFixedThreadPool(AppConfig.threadCount).asCoroutineDispatcher() + val handler = Handler() val searchStateData = MutableLiveData() val searchBooksLiveData = MutableLiveData>() var name: String = "" @@ -29,6 +31,9 @@ class ChangeSourceViewModel(application: Application) : BaseViewModel(applicatio private var task: Coroutine<*>? = null private var screenKey: String = "" private val searchBooks = hashSetOf() + private var postTime = 0L + private val sendRunnable = Runnable { upAdapter() } + fun initData(arguments: Bundle?) { arguments?.let { bundle -> @@ -55,9 +60,16 @@ class ChangeSourceViewModel(application: Application) : BaseViewModel(applicatio } } + @Synchronized private fun upAdapter() { - val books = searchBooks.toList() - searchBooksLiveData.postValue(books.sortedBy { it.originOrder }) + if (System.currentTimeMillis() > postTime + 500) { + postTime = System.currentTimeMillis() + handler.removeCallbacks(sendRunnable) + val books = searchBooks.toList() + searchBooksLiveData.postValue(books.sortedBy { it.originOrder }) + } else { + handler.postDelayed(sendRunnable, 500) + } } private fun searchFinish(searchBook: SearchBook) { @@ -172,6 +184,7 @@ class ChangeSourceViewModel(application: Application) : BaseViewModel(applicatio App.db.bookSourceDao().update(source) } searchBooks.remove(searchBook) + upAdapter() } }