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