mirror of
https://github.com/gedoor/legado.git
synced 2024-08-30 09:23:26 +08:00
优化
This commit is contained in:
parent
39ce4cdf89
commit
a535a0ced2
@ -31,6 +31,7 @@ import kotlinx.coroutines.asCoroutineDispatcher
|
|||||||
import kotlinx.coroutines.delay
|
import kotlinx.coroutines.delay
|
||||||
import kotlinx.coroutines.isActive
|
import kotlinx.coroutines.isActive
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
|
import java.util.LinkedList
|
||||||
import java.util.concurrent.ConcurrentHashMap
|
import java.util.concurrent.ConcurrentHashMap
|
||||||
import java.util.concurrent.Executors
|
import java.util.concurrent.Executors
|
||||||
import kotlin.math.min
|
import kotlin.math.min
|
||||||
@ -39,7 +40,7 @@ class MainViewModel(application: Application) : BaseViewModel(application) {
|
|||||||
private var threadCount = AppConfig.threadCount
|
private var threadCount = AppConfig.threadCount
|
||||||
private var upTocPool =
|
private var upTocPool =
|
||||||
Executors.newFixedThreadPool(min(threadCount, AppConst.MAX_THREAD)).asCoroutineDispatcher()
|
Executors.newFixedThreadPool(min(threadCount, AppConst.MAX_THREAD)).asCoroutineDispatcher()
|
||||||
private val waitUpTocBooks = arrayListOf<String>()
|
private val waitUpTocBooks = LinkedList<String>()
|
||||||
private val onUpTocBooks = ConcurrentHashMap.newKeySet<String>()
|
private val onUpTocBooks = ConcurrentHashMap.newKeySet<String>()
|
||||||
val onUpBooksLiveData = MutableLiveData<Int>()
|
val onUpBooksLiveData = MutableLiveData<Int>()
|
||||||
private var upTocJob: Job? = null
|
private var upTocJob: Job? = null
|
||||||
@ -113,15 +114,13 @@ class MainViewModel(application: Application) : BaseViewModel(application) {
|
|||||||
|
|
||||||
@Synchronized
|
@Synchronized
|
||||||
private fun updateToc() {
|
private fun updateToc() {
|
||||||
val bookUrl = waitUpTocBooks.firstOrNull() ?: return
|
val bookUrl = waitUpTocBooks.poll() ?: return
|
||||||
if (onUpTocBooks.contains(bookUrl)) {
|
if (onUpTocBooks.contains(bookUrl)) {
|
||||||
waitUpTocBooks.remove(bookUrl)
|
|
||||||
postUpBooksLiveData()
|
postUpBooksLiveData()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
val book = appDb.bookDao.getBook(bookUrl)
|
val book = appDb.bookDao.getBook(bookUrl)
|
||||||
if (book == null) {
|
if (book == null) {
|
||||||
waitUpTocBooks.remove(bookUrl)
|
|
||||||
postUpBooksLiveData()
|
postUpBooksLiveData()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -131,11 +130,9 @@ class MainViewModel(application: Application) : BaseViewModel(application) {
|
|||||||
book.addType(BookType.updateError)
|
book.addType(BookType.updateError)
|
||||||
appDb.bookDao.update(book)
|
appDb.bookDao.update(book)
|
||||||
}
|
}
|
||||||
waitUpTocBooks.remove(book.bookUrl)
|
|
||||||
postUpBooksLiveData()
|
postUpBooksLiveData()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
waitUpTocBooks.remove(bookUrl)
|
|
||||||
upTocAdd(bookUrl)
|
upTocAdd(bookUrl)
|
||||||
execute(context = upTocPool, executeContext = upTocPool) {
|
execute(context = upTocPool, executeContext = upTocPool) {
|
||||||
kotlin.runCatching {
|
kotlin.runCatching {
|
||||||
@ -202,7 +199,6 @@ class MainViewModel(application: Application) : BaseViewModel(application) {
|
|||||||
|
|
||||||
@Synchronized
|
@Synchronized
|
||||||
private fun upTocFinally(bookUrl: String) {
|
private fun upTocFinally(bookUrl: String) {
|
||||||
waitUpTocBooks.remove(bookUrl)
|
|
||||||
onUpTocBooks.remove(bookUrl)
|
onUpTocBooks.remove(bookUrl)
|
||||||
postEvent(EventBus.UP_BOOKSHELF, bookUrl)
|
postEvent(EventBus.UP_BOOKSHELF, bookUrl)
|
||||||
if (waitUpTocBooks.isEmpty()
|
if (waitUpTocBooks.isEmpty()
|
||||||
|
Loading…
Reference in New Issue
Block a user