From fa727896de89c611d4dd5bef8682d5bd684082b5 Mon Sep 17 00:00:00 2001 From: gedoor Date: Wed, 10 Feb 2021 20:36:16 +0800 Subject: [PATCH] edit ChangeCoverViewModel.kt --- .../book/changecover/ChangeCoverViewModel.kt | 78 ++++++++++--------- 1 file changed, 43 insertions(+), 35 deletions(-) diff --git a/app/src/main/java/io/legado/app/ui/book/changecover/ChangeCoverViewModel.kt b/app/src/main/java/io/legado/app/ui/book/changecover/ChangeCoverViewModel.kt index 64ec67578..13488567b 100644 --- a/app/src/main/java/io/legado/app/ui/book/changecover/ChangeCoverViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/changecover/ChangeCoverViewModel.kt @@ -90,45 +90,53 @@ class ChangeCoverViewModel(application: Application) : BaseViewModel(application } } + @Synchronized private fun search() { - synchronized(this) { - if (searchIndex >= bookSourceList.lastIndex) { - return - } - searchIndex++ - val source = bookSourceList[searchIndex] - val task = WebBook(source) - .searchBook(this, name, context = searchPool!!) - .timeout(60000L) - .onSuccess(Dispatchers.IO) { - if (it.isNotEmpty()) { - val searchBook = it[0] - if (searchBook.name == name && searchBook.author == author - && !searchBook.coverUrl.isNullOrEmpty() - ) { - appDb.searchBookDao.insert(searchBook) - if (!searchBooks.contains(searchBook)) { - searchBooks.add(searchBook) - upAdapter() - } - } - } - } - .onFinally { - synchronized(this) { - if (searchIndex < bookSourceList.lastIndex) { - search() - } else { - searchIndex++ - } - if (searchIndex >= bookSourceList.lastIndex + min(bookSourceList.size, - threadCount) - ) { - searchStateData.postValue(false) + if (searchIndex >= bookSourceList.lastIndex) { + return + } + searchIndex++ + val source = bookSourceList[searchIndex] + if (source.getSearchRule().coverUrl.isNullOrBlank()) { + searchNext() + return + } + val task = WebBook(source) + .searchBook(this, name, context = searchPool!!) + .timeout(60000L) + .onSuccess(Dispatchers.IO) { + if (it.isNotEmpty()) { + val searchBook = it[0] + if (searchBook.name == name && searchBook.author == author + && !searchBook.coverUrl.isNullOrEmpty() + ) { + appDb.searchBookDao.insert(searchBook) + if (!searchBooks.contains(searchBook)) { + searchBooks.add(searchBook) + upAdapter() } } } - tasks.add(task) + } + .onFinally { + searchNext() + } + tasks.add(task) + } + + @Synchronized + private fun searchNext() { + if (searchIndex < bookSourceList.lastIndex) { + search() + } else { + searchIndex++ + } + if (searchIndex >= bookSourceList.lastIndex + min( + bookSourceList.size, + threadCount + ) + ) { + searchStateData.postValue(false) } }