pull/1282/head
gedoor 3 years ago
parent 967462b891
commit d7951cca36
  1. 17
      app/src/main/java/io/legado/app/ui/book/changecover/ChangeCoverViewModel.kt
  2. 17
      app/src/main/java/io/legado/app/ui/book/changesource/ChangeSourceViewModel.kt
  3. 17
      app/src/main/java/io/legado/app/ui/book/search/SearchViewModel.kt

@ -2,8 +2,6 @@ package io.legado.app.ui.book.changecover
import android.app.Application import android.app.Application
import android.os.Bundle import android.os.Bundle
import android.os.Handler
import android.os.Looper
import androidx.lifecycle.MutableLiveData import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope import androidx.lifecycle.viewModelScope
import io.legado.app.base.BaseViewModel 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.AppConfig
import io.legado.app.help.coroutine.CompositeCoroutine import io.legado.app.help.coroutine.CompositeCoroutine
import io.legado.app.model.webBook.WebBook import io.legado.app.model.webBook.WebBook
import kotlinx.coroutines.ExecutorCoroutineDispatcher import kotlinx.coroutines.*
import kotlinx.coroutines.asCoroutineDispatcher
import java.util.concurrent.CopyOnWriteArraySet import java.util.concurrent.CopyOnWriteArraySet
import java.util.concurrent.Executors import java.util.concurrent.Executors
import kotlin.math.min import kotlin.math.min
@ -23,7 +20,7 @@ import kotlin.math.min
class ChangeCoverViewModel(application: Application) : BaseViewModel(application) { class ChangeCoverViewModel(application: Application) : BaseViewModel(application) {
private val threadCount = AppConfig.threadCount private val threadCount = AppConfig.threadCount
private var searchPool: ExecutorCoroutineDispatcher? = null private var searchPool: ExecutorCoroutineDispatcher? = null
val handler = Handler(Looper.getMainLooper()) private var upAdapterJob: Job? = null
var name: String = "" var name: String = ""
var author: String = "" var author: String = ""
private var tasks = CompositeCoroutine() private var tasks = CompositeCoroutine()
@ -31,7 +28,6 @@ class ChangeCoverViewModel(application: Application) : BaseViewModel(application
val searchStateData = MutableLiveData<Boolean>() val searchStateData = MutableLiveData<Boolean>()
val searchBooksLiveData = MutableLiveData<List<SearchBook>>() val searchBooksLiveData = MutableLiveData<List<SearchBook>>()
private val searchBooks = CopyOnWriteArraySet<SearchBook>() private val searchBooks = CopyOnWriteArraySet<SearchBook>()
private val sendRunnable = Runnable { upAdapter() }
private var postTime = 0L private var postTime = 0L
@Volatile @Volatile
@ -68,13 +64,16 @@ class ChangeCoverViewModel(application: Application) : BaseViewModel(application
@Synchronized @Synchronized
private fun upAdapter() { private fun upAdapter() {
if (System.currentTimeMillis() >= postTime + 500) { if (System.currentTimeMillis() >= postTime + 500) {
handler.removeCallbacks(sendRunnable) upAdapterJob?.cancel()
postTime = System.currentTimeMillis() postTime = System.currentTimeMillis()
val books = searchBooks.toList() val books = searchBooks.toList()
searchBooksLiveData.postValue(books.sortedBy { it.originOrder }) searchBooksLiveData.postValue(books.sortedBy { it.originOrder })
} else { } else {
handler.removeCallbacks(sendRunnable) upAdapterJob?.cancel()
handler.postDelayed(sendRunnable, 500) upAdapterJob = viewModelScope.launch {
delay(500)
upAdapter()
}
} }
} }

@ -2,8 +2,6 @@ package io.legado.app.ui.book.changesource
import android.app.Application import android.app.Application
import android.os.Bundle import android.os.Bundle
import android.os.Handler
import android.os.Looper
import androidx.lifecycle.MutableLiveData import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope import androidx.lifecycle.viewModelScope
import io.legado.app.base.BaseViewModel 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.model.webBook.WebBook
import io.legado.app.utils.getPrefBoolean import io.legado.app.utils.getPrefBoolean
import io.legado.app.utils.getPrefString import io.legado.app.utils.getPrefString
import kotlinx.coroutines.*
import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.ExecutorCoroutineDispatcher
import kotlinx.coroutines.asCoroutineDispatcher
import splitties.init.appCtx import splitties.init.appCtx
import java.util.concurrent.CopyOnWriteArraySet import java.util.concurrent.CopyOnWriteArraySet
import java.util.concurrent.Executors import java.util.concurrent.Executors
@ -29,7 +26,7 @@ import kotlin.math.min
class ChangeSourceViewModel(application: Application) : BaseViewModel(application) { class ChangeSourceViewModel(application: Application) : BaseViewModel(application) {
private val threadCount = AppConfig.threadCount private val threadCount = AppConfig.threadCount
private var searchPool: ExecutorCoroutineDispatcher? = null private var searchPool: ExecutorCoroutineDispatcher? = null
val handler = Handler(Looper.getMainLooper()) private var upAdapterJob: Job? = null
val searchStateData = MutableLiveData<Boolean>() val searchStateData = MutableLiveData<Boolean>()
val searchBooksLiveData = MutableLiveData<List<SearchBook>>() val searchBooksLiveData = MutableLiveData<List<SearchBook>>()
var name: String = "" var name: String = ""
@ -39,7 +36,6 @@ class ChangeSourceViewModel(application: Application) : BaseViewModel(applicatio
private var bookSourceList = arrayListOf<BookSource>() private var bookSourceList = arrayListOf<BookSource>()
private val searchBooks = CopyOnWriteArraySet<SearchBook>() private val searchBooks = CopyOnWriteArraySet<SearchBook>()
private var postTime = 0L private var postTime = 0L
private val sendRunnable = Runnable { upAdapter() }
private val searchGroup get() = appCtx.getPrefString("searchGroup") ?: "" private val searchGroup get() = appCtx.getPrefString("searchGroup") ?: ""
@Volatile @Volatile
@ -81,13 +77,16 @@ class ChangeSourceViewModel(application: Application) : BaseViewModel(applicatio
@Synchronized @Synchronized
private fun upAdapter() { private fun upAdapter() {
if (System.currentTimeMillis() >= postTime + 500) { if (System.currentTimeMillis() >= postTime + 500) {
handler.removeCallbacks(sendRunnable) upAdapterJob?.cancel()
postTime = System.currentTimeMillis() postTime = System.currentTimeMillis()
val books = searchBooks.toList() val books = searchBooks.toList()
searchBooksLiveData.postValue(books.sortedBy { it.originOrder }) searchBooksLiveData.postValue(books.sortedBy { it.originOrder })
} else { } else {
handler.removeCallbacks(sendRunnable) upAdapterJob?.cancel()
handler.postDelayed(sendRunnable, 500) upAdapterJob = viewModelScope.launch {
delay(500)
upAdapter()
}
} }
} }

@ -1,8 +1,6 @@
package io.legado.app.ui.book.search package io.legado.app.ui.book.search
import android.app.Application import android.app.Application
import android.os.Handler
import android.os.Looper
import androidx.lifecycle.MutableLiveData import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope import androidx.lifecycle.viewModelScope
import io.legado.app.base.BaseViewModel 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.data.entities.SearchKeyword
import io.legado.app.model.webBook.SearchBookModel import io.legado.app.model.webBook.SearchBookModel
import io.legado.app.utils.getPrefBoolean import io.legado.app.utils.getPrefBoolean
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.*
import kotlinx.coroutines.isActive
class SearchViewModel(application: Application) : BaseViewModel(application), class SearchViewModel(application: Application) : BaseViewModel(application),
SearchBookModel.CallBack { SearchBookModel.CallBack {
val handler = Handler(Looper.getMainLooper())
private val searchBookModel = SearchBookModel(viewModelScope, this) private val searchBookModel = SearchBookModel(viewModelScope, this)
private var upAdapterJob: Job? = null
var isSearchLiveData = MutableLiveData<Boolean>() var isSearchLiveData = MutableLiveData<Boolean>()
var searchBookLiveData = MutableLiveData<List<SearchBook>>() var searchBookLiveData = MutableLiveData<List<SearchBook>>()
var searchKey: String = "" var searchKey: String = ""
@ -26,7 +23,6 @@ class SearchViewModel(application: Application) : BaseViewModel(application),
private var searchBooks = arrayListOf<SearchBook>() private var searchBooks = arrayListOf<SearchBook>()
private var searchID = 0L private var searchID = 0L
private var postTime = 0L private var postTime = 0L
private val sendRunnable = Runnable { upAdapter() }
/** /**
* 开始搜索 * 开始搜索
@ -48,12 +44,15 @@ class SearchViewModel(application: Application) : BaseViewModel(application),
@Synchronized @Synchronized
private fun upAdapter() { private fun upAdapter() {
if (System.currentTimeMillis() >= postTime + 500) { if (System.currentTimeMillis() >= postTime + 500) {
handler.removeCallbacks(sendRunnable) upAdapterJob?.cancel()
postTime = System.currentTimeMillis() postTime = System.currentTimeMillis()
searchBookLiveData.postValue(searchBooks) searchBookLiveData.postValue(searchBooks)
} else { } else {
handler.removeCallbacks(sendRunnable) upAdapterJob?.cancel()
handler.postDelayed(sendRunnable, 500 - System.currentTimeMillis() + postTime) upAdapterJob = viewModelScope.launch {
delay(500)
upAdapter()
}
} }
} }

Loading…
Cancel
Save