From ef504403b081d474786ebd6cc2fff22f9511e0fe Mon Sep 17 00:00:00 2001 From: gedoor Date: Thu, 27 Aug 2020 17:14:52 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=B0=8F=E8=AF=B4=E6=9B=B4?= =?UTF-8?q?=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/io/legado/app/constant/EventBus.kt | 2 +- .../io/legado/app/ui/main/MainActivity.kt | 2 +- .../io/legado/app/ui/main/MainViewModel.kt | 63 ++++++++++++++----- .../ui/main/bookshelf/BookshelfFragment.kt | 2 +- .../ui/main/bookshelf/books/BooksFragment.kt | 2 +- 5 files changed, 50 insertions(+), 21 deletions(-) diff --git a/app/src/main/java/io/legado/app/constant/EventBus.kt b/app/src/main/java/io/legado/app/constant/EventBus.kt index e24ba8000..6fe8b5cea 100644 --- a/app/src/main/java/io/legado/app/constant/EventBus.kt +++ b/app/src/main/java/io/legado/app/constant/EventBus.kt @@ -3,7 +3,7 @@ package io.legado.app.constant object EventBus { const val MEDIA_BUTTON = "mediaButton" const val RECREATE = "RECREATE" - const val UP_BOOK = "sourceDebugLog" + const val UP_BOOK = "upBookToc" const val ALOUD_STATE = "aloud_state" const val TTS_PROGRESS = "ttsStart" const val TTS_DS = "ttsDs" diff --git a/app/src/main/java/io/legado/app/ui/main/MainActivity.kt b/app/src/main/java/io/legado/app/ui/main/MainActivity.kt index 77a98460c..7d6c71134 100644 --- a/app/src/main/java/io/legado/app/ui/main/MainActivity.kt +++ b/app/src/main/java/io/legado/app/ui/main/MainActivity.kt @@ -62,7 +62,7 @@ class MainActivity : VMBaseActivity(R.layout.activity_main), //自动更新书籍 if (AppConfig.autoRefreshBook) { view_pager_main.postDelayed({ - viewModel.upChapterList() + viewModel.upAllBookToc() }, 1000) } view_pager_main.postDelayed({ diff --git a/app/src/main/java/io/legado/app/ui/main/MainViewModel.kt b/app/src/main/java/io/legado/app/ui/main/MainViewModel.kt index b0122bc8b..3917769b0 100644 --- a/app/src/main/java/io/legado/app/ui/main/MainViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/main/MainViewModel.kt @@ -18,13 +18,18 @@ import io.legado.app.utils.fromJsonObject import io.legado.app.utils.postEvent import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.asCoroutineDispatcher -import kotlinx.coroutines.delay +import java.util.concurrent.ConcurrentHashMap +import java.util.concurrent.CopyOnWriteArraySet import java.util.concurrent.Executors class MainViewModel(application: Application) : BaseViewModel(application) { - private var upTocPool = - Executors.newFixedThreadPool(AppConfig.threadCount).asCoroutineDispatcher() - val updateList = hashSetOf() + val threadCount = AppConfig.threadCount + private var upTocPool = Executors.newFixedThreadPool(threadCount).asCoroutineDispatcher() + val updateList = CopyOnWriteArraySet() + private val bookMap = ConcurrentHashMap() + + @Volatile + private var usePoolCount = 0 override fun onCleared() { super.onCleared() @@ -36,18 +41,31 @@ class MainViewModel(application: Application) : BaseViewModel(application) { upTocPool = Executors.newFixedThreadPool(AppConfig.threadCount).asCoroutineDispatcher() } - fun upChapterList() { + fun upAllBookToc() { execute { - upChapterList(App.db.bookDao().hasUpdateBooks) + upToc(App.db.bookDao().hasUpdateBooks) + } + } + + fun upToc(books: List) { + books.filter { + it.origin != BookType.local && it.canUpdate + }.forEach { + bookMap[it.bookUrl] = it + } + for (i in 0 until threadCount) { + if (usePoolCount < threadCount) { + usePoolCount++ + updateToc() + } } } - fun upChapterList(books: List) { + private fun updateToc() { execute { - books.filter { - it.origin != BookType.local && it.canUpdate - }.forEach { book -> - if (!updateList.contains(book.bookUrl)) { + bookMap.forEach { bookEntry -> + if (!updateList.contains(bookEntry.key)) { + val book = bookEntry.value App.db.bookSourceDao().getBookSource(book.origin)?.let { bookSource -> synchronized(this) { updateList.add(book.bookUrl) @@ -56,28 +74,39 @@ class MainViewModel(application: Application) : BaseViewModel(application) { WebBook(bookSource).getChapterList(book, context = upTocPool) .timeout(300000) .onSuccess(IO) { - synchronized(this) { - updateList.remove(book.bookUrl) - postEvent(EventBus.UP_BOOK, book.bookUrl) - } App.db.bookDao().update(book) App.db.bookChapterDao().delByBook(book.bookUrl) App.db.bookChapterDao().insert(*it.toTypedArray()) } .onError { + it.printStackTrace() + } + .onFinally { synchronized(this) { + bookMap.remove(bookEntry.key) updateList.remove(book.bookUrl) postEvent(EventBus.UP_BOOK, book.bookUrl) + upNext() } - it.printStackTrace() } + } ?: synchronized(this) { + bookMap.remove(bookEntry.key) + upNext() } + return@forEach } - delay(50) } } } + private fun upNext() { + if (bookMap.size > updateList.size) { + updateToc() + } else { + usePoolCount-- + } + } + fun initRss() { execute { val url = "https://gitee.com/alanskycn/yuedu/raw/master/JS/RSS/rssSource" diff --git a/app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfFragment.kt b/app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfFragment.kt index 9abfddbd4..4444f33d7 100644 --- a/app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfFragment.kt +++ b/app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfFragment.kt @@ -78,7 +78,7 @@ class BookshelfFragment : VMBaseFragment(R.layout.fragment_b val group = bookGroups[tab_layout.selectedTabPosition] val fragment = fragmentMap[group.groupId] fragment?.getBooks()?.let { - activityViewModel.upChapterList(it) + activityViewModel.upToc(it) } } R.id.menu_bookshelf_layout -> configBookshelf() diff --git a/app/src/main/java/io/legado/app/ui/main/bookshelf/books/BooksFragment.kt b/app/src/main/java/io/legado/app/ui/main/bookshelf/books/BooksFragment.kt index c03292011..ebbe310e8 100644 --- a/app/src/main/java/io/legado/app/ui/main/bookshelf/books/BooksFragment.kt +++ b/app/src/main/java/io/legado/app/ui/main/bookshelf/books/BooksFragment.kt @@ -66,7 +66,7 @@ class BooksFragment : BaseFragment(R.layout.fragment_books), refresh_layout.setColorSchemeColors(accentColor) refresh_layout.setOnRefreshListener { refresh_layout.isRefreshing = false - activityViewModel.upChapterList(booksAdapter.getItems()) + activityViewModel.upToc(booksAdapter.getItems()) } val bookshelfLayout = getPrefInt(PreferKey.bookshelfLayout) if (bookshelfLayout == 0) {