|
|
@ -2,6 +2,8 @@ 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 io.legado.app.App |
|
|
|
import io.legado.app.App |
|
|
|
import io.legado.app.base.BaseViewModel |
|
|
|
import io.legado.app.base.BaseViewModel |
|
|
@ -14,19 +16,23 @@ import io.legado.app.model.webBook.WebBook |
|
|
|
import kotlinx.coroutines.Dispatchers |
|
|
|
import kotlinx.coroutines.Dispatchers |
|
|
|
import kotlinx.coroutines.ExecutorCoroutineDispatcher |
|
|
|
import kotlinx.coroutines.ExecutorCoroutineDispatcher |
|
|
|
import kotlinx.coroutines.asCoroutineDispatcher |
|
|
|
import kotlinx.coroutines.asCoroutineDispatcher |
|
|
|
|
|
|
|
import java.util.concurrent.CopyOnWriteArraySet |
|
|
|
import java.util.concurrent.Executors |
|
|
|
import java.util.concurrent.Executors |
|
|
|
import kotlin.math.min |
|
|
|
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()) |
|
|
|
var name: String = "" |
|
|
|
var name: String = "" |
|
|
|
var author: String = "" |
|
|
|
var author: String = "" |
|
|
|
private var tasks = CompositeCoroutine() |
|
|
|
private var tasks = CompositeCoroutine() |
|
|
|
private var bookSourceList = arrayListOf<BookSource>() |
|
|
|
private var bookSourceList = arrayListOf<BookSource>() |
|
|
|
val searchStateData = MutableLiveData<Boolean>() |
|
|
|
val searchStateData = MutableLiveData<Boolean>() |
|
|
|
val searchBooksLiveData = MutableLiveData<List<SearchBook>>() |
|
|
|
val searchBooksLiveData = MutableLiveData<List<SearchBook>>() |
|
|
|
private val searchBooks = ArrayList<SearchBook>() |
|
|
|
private val searchBooks = CopyOnWriteArraySet<SearchBook>() |
|
|
|
|
|
|
|
private val sendRunnable = Runnable { upAdapter() } |
|
|
|
|
|
|
|
private var postTime = 0L |
|
|
|
|
|
|
|
|
|
|
|
@Volatile |
|
|
|
@Volatile |
|
|
|
private var searchIndex = -1 |
|
|
|
private var searchIndex = -1 |
|
|
@ -51,16 +57,27 @@ class ChangeCoverViewModel(application: Application) : BaseViewModel(application |
|
|
|
execute { |
|
|
|
execute { |
|
|
|
App.db.searchBookDao.getEnableHasCover(name, author).let { |
|
|
|
App.db.searchBookDao.getEnableHasCover(name, author).let { |
|
|
|
searchBooks.addAll(it) |
|
|
|
searchBooks.addAll(it) |
|
|
|
|
|
|
|
searchBooksLiveData.postValue(searchBooks.toList()) |
|
|
|
if (it.size <= 1) { |
|
|
|
if (it.size <= 1) { |
|
|
|
searchBooksLiveData.postValue(searchBooks) |
|
|
|
|
|
|
|
startSearch() |
|
|
|
startSearch() |
|
|
|
} else { |
|
|
|
|
|
|
|
searchBooksLiveData.postValue(searchBooks) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Synchronized |
|
|
|
|
|
|
|
private fun upAdapter() { |
|
|
|
|
|
|
|
if (System.currentTimeMillis() >= postTime + 500) { |
|
|
|
|
|
|
|
handler.removeCallbacks(sendRunnable) |
|
|
|
|
|
|
|
postTime = System.currentTimeMillis() |
|
|
|
|
|
|
|
val books = searchBooks.toList() |
|
|
|
|
|
|
|
searchBooksLiveData.postValue(books.sortedBy { it.originOrder }) |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
handler.removeCallbacks(sendRunnable) |
|
|
|
|
|
|
|
handler.postDelayed(sendRunnable, 500) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private fun startSearch() { |
|
|
|
private fun startSearch() { |
|
|
|
execute { |
|
|
|
execute { |
|
|
|
bookSourceList.clear() |
|
|
|
bookSourceList.clear() |
|
|
@ -93,7 +110,7 @@ class ChangeCoverViewModel(application: Application) : BaseViewModel(application |
|
|
|
App.db.searchBookDao.insert(searchBook) |
|
|
|
App.db.searchBookDao.insert(searchBook) |
|
|
|
if (!searchBooks.contains(searchBook)) { |
|
|
|
if (!searchBooks.contains(searchBook)) { |
|
|
|
searchBooks.add(searchBook) |
|
|
|
searchBooks.add(searchBook) |
|
|
|
searchBooksLiveData.postValue(searchBooks) |
|
|
|
upAdapter() |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -121,6 +138,7 @@ class ChangeCoverViewModel(application: Application) : BaseViewModel(application |
|
|
|
startSearch() |
|
|
|
startSearch() |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
tasks.clear() |
|
|
|
tasks.clear() |
|
|
|
|
|
|
|
searchStateData.postValue(false) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|