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.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<Boolean>()
val searchBooksLiveData = MutableLiveData<List<SearchBook>>()
private val searchBooks = CopyOnWriteArraySet<SearchBook>()
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()
}
}
}

@ -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<Boolean>()
val searchBooksLiveData = MutableLiveData<List<SearchBook>>()
var name: String = ""
@ -39,7 +36,6 @@ class ChangeSourceViewModel(application: Application) : BaseViewModel(applicatio
private var bookSourceList = arrayListOf<BookSource>()
private val searchBooks = CopyOnWriteArraySet<SearchBook>()
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()
}
}
}

@ -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<Boolean>()
var searchBookLiveData = MutableLiveData<List<SearchBook>>()
var searchKey: String = ""
@ -26,7 +23,6 @@ class SearchViewModel(application: Application) : BaseViewModel(application),
private var searchBooks = arrayListOf<SearchBook>()
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()
}
}
}

Loading…
Cancel
Save