From d7951cca368ccd74417dc99556d0aae0f3f9c609 Mon Sep 17 00:00:00 2001 From: gedoor Date: Fri, 27 Aug 2021 15:55:56 +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 --- .../ui/book/changecover/ChangeCoverViewModel.kt | 17 ++++++++--------- .../book/changesource/ChangeSourceViewModel.kt | 17 ++++++++--------- .../app/ui/book/search/SearchViewModel.kt | 17 ++++++++--------- 3 files changed, 24 insertions(+), 27 deletions(-) diff --git a/app/src/main/java/io/legado/app/ui/book/changecover/ChangeCoverViewModel.kt b/app/src/main/java/io/legado/app/ui/book/changecover/ChangeCoverViewModel.kt index 0e2ed6a3a..c19ee3cac 100644 --- a/app/src/main/java/io/legado/app/ui/book/changecover/ChangeCoverViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/changecover/ChangeCoverViewModel.kt @@ -2,8 +2,6 @@ package io.legado.app.ui.book.changecover import android.app.Application import android.os.Bundle -import android.os.Handler -import android.os.Looper import androidx.lifecycle.MutableLiveData import androidx.lifecycle.viewModelScope import io.legado.app.base.BaseViewModel @@ -14,8 +12,7 @@ import io.legado.app.data.entities.SearchBook import io.legado.app.help.AppConfig import io.legado.app.help.coroutine.CompositeCoroutine import io.legado.app.model.webBook.WebBook -import kotlinx.coroutines.ExecutorCoroutineDispatcher -import kotlinx.coroutines.asCoroutineDispatcher +import kotlinx.coroutines.* import java.util.concurrent.CopyOnWriteArraySet import java.util.concurrent.Executors import kotlin.math.min @@ -23,7 +20,7 @@ import kotlin.math.min class ChangeCoverViewModel(application: Application) : BaseViewModel(application) { private val threadCount = AppConfig.threadCount private var searchPool: ExecutorCoroutineDispatcher? = null - val handler = Handler(Looper.getMainLooper()) + private var upAdapterJob: Job? = null var name: String = "" var author: String = "" private var tasks = CompositeCoroutine() @@ -31,7 +28,6 @@ class ChangeCoverViewModel(application: Application) : BaseViewModel(application val searchStateData = MutableLiveData() val searchBooksLiveData = MutableLiveData>() private val searchBooks = CopyOnWriteArraySet() - private val sendRunnable = Runnable { upAdapter() } private var postTime = 0L @Volatile @@ -68,13 +64,16 @@ class ChangeCoverViewModel(application: Application) : BaseViewModel(application @Synchronized private fun upAdapter() { if (System.currentTimeMillis() >= postTime + 500) { - handler.removeCallbacks(sendRunnable) + upAdapterJob?.cancel() postTime = System.currentTimeMillis() val books = searchBooks.toList() searchBooksLiveData.postValue(books.sortedBy { it.originOrder }) } else { - handler.removeCallbacks(sendRunnable) - handler.postDelayed(sendRunnable, 500) + upAdapterJob?.cancel() + upAdapterJob = viewModelScope.launch { + delay(500) + upAdapter() + } } } 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 353f96a1a..aaf6a0a6d 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,8 +2,6 @@ package io.legado.app.ui.book.changesource import android.app.Application import android.os.Bundle -import android.os.Handler -import android.os.Looper import androidx.lifecycle.MutableLiveData import androidx.lifecycle.viewModelScope import io.legado.app.base.BaseViewModel @@ -18,9 +16,8 @@ import io.legado.app.help.coroutine.CompositeCoroutine import io.legado.app.model.webBook.WebBook import io.legado.app.utils.getPrefBoolean import io.legado.app.utils.getPrefString +import kotlinx.coroutines.* import kotlinx.coroutines.Dispatchers.IO -import kotlinx.coroutines.ExecutorCoroutineDispatcher -import kotlinx.coroutines.asCoroutineDispatcher import splitties.init.appCtx import java.util.concurrent.CopyOnWriteArraySet import java.util.concurrent.Executors @@ -29,7 +26,7 @@ import kotlin.math.min class ChangeSourceViewModel(application: Application) : BaseViewModel(application) { private val threadCount = AppConfig.threadCount private var searchPool: ExecutorCoroutineDispatcher? = null - val handler = Handler(Looper.getMainLooper()) + private var upAdapterJob: Job? = null val searchStateData = MutableLiveData() val searchBooksLiveData = MutableLiveData>() var name: String = "" @@ -39,7 +36,6 @@ class ChangeSourceViewModel(application: Application) : BaseViewModel(applicatio private var bookSourceList = arrayListOf() private val searchBooks = CopyOnWriteArraySet() private var postTime = 0L - private val sendRunnable = Runnable { upAdapter() } private val searchGroup get() = appCtx.getPrefString("searchGroup") ?: "" @Volatile @@ -81,13 +77,16 @@ class ChangeSourceViewModel(application: Application) : BaseViewModel(applicatio @Synchronized private fun upAdapter() { if (System.currentTimeMillis() >= postTime + 500) { - handler.removeCallbacks(sendRunnable) + upAdapterJob?.cancel() postTime = System.currentTimeMillis() val books = searchBooks.toList() searchBooksLiveData.postValue(books.sortedBy { it.originOrder }) } else { - handler.removeCallbacks(sendRunnable) - handler.postDelayed(sendRunnable, 500) + upAdapterJob?.cancel() + upAdapterJob = viewModelScope.launch { + delay(500) + upAdapter() + } } } 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 9b2f9999e..43bec708f 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,8 +1,6 @@ package io.legado.app.ui.book.search import android.app.Application -import android.os.Handler -import android.os.Looper import androidx.lifecycle.MutableLiveData import androidx.lifecycle.viewModelScope import io.legado.app.base.BaseViewModel @@ -12,13 +10,12 @@ import io.legado.app.data.entities.SearchBook import io.legado.app.data.entities.SearchKeyword import io.legado.app.model.webBook.SearchBookModel import io.legado.app.utils.getPrefBoolean -import kotlinx.coroutines.CoroutineScope -import kotlinx.coroutines.isActive +import kotlinx.coroutines.* class SearchViewModel(application: Application) : BaseViewModel(application), SearchBookModel.CallBack { - val handler = Handler(Looper.getMainLooper()) private val searchBookModel = SearchBookModel(viewModelScope, this) + private var upAdapterJob: Job? = null var isSearchLiveData = MutableLiveData() var searchBookLiveData = MutableLiveData>() var searchKey: String = "" @@ -26,7 +23,6 @@ class SearchViewModel(application: Application) : BaseViewModel(application), private var searchBooks = arrayListOf() private var searchID = 0L private var postTime = 0L - private val sendRunnable = Runnable { upAdapter() } /** * 开始搜索 @@ -48,12 +44,15 @@ class SearchViewModel(application: Application) : BaseViewModel(application), @Synchronized private fun upAdapter() { if (System.currentTimeMillis() >= postTime + 500) { - handler.removeCallbacks(sendRunnable) + upAdapterJob?.cancel() postTime = System.currentTimeMillis() searchBookLiveData.postValue(searchBooks) } else { - handler.removeCallbacks(sendRunnable) - handler.postDelayed(sendRunnable, 500 - System.currentTimeMillis() + postTime) + upAdapterJob?.cancel() + upAdapterJob = viewModelScope.launch { + delay(500) + upAdapter() + } } }