优化小说更新

pull/341/head^2
gedoor 4 years ago
parent 9126880698
commit ef504403b0
  1. 2
      app/src/main/java/io/legado/app/constant/EventBus.kt
  2. 2
      app/src/main/java/io/legado/app/ui/main/MainActivity.kt
  3. 61
      app/src/main/java/io/legado/app/ui/main/MainViewModel.kt
  4. 2
      app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfFragment.kt
  5. 2
      app/src/main/java/io/legado/app/ui/main/bookshelf/books/BooksFragment.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"

@ -62,7 +62,7 @@ class MainActivity : VMBaseActivity<MainViewModel>(R.layout.activity_main),
//自动更新书籍
if (AppConfig.autoRefreshBook) {
view_pager_main.postDelayed({
viewModel.upChapterList()
viewModel.upAllBookToc()
}, 1000)
}
view_pager_main.postDelayed({

@ -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<String>()
val threadCount = AppConfig.threadCount
private var upTocPool = Executors.newFixedThreadPool(threadCount).asCoroutineDispatcher()
val updateList = CopyOnWriteArraySet<String>()
private val bookMap = ConcurrentHashMap<String, Book>()
@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 upChapterList(books: List<Book>) {
execute {
fun upToc(books: List<Book>) {
books.filter {
it.origin != BookType.local && it.canUpdate
}.forEach { book ->
if (!updateList.contains(book.bookUrl)) {
}.forEach {
bookMap[it.bookUrl] = it
}
for (i in 0 until threadCount) {
if (usePoolCount < threadCount) {
usePoolCount++
updateToc()
}
}
}
private fun updateToc() {
execute {
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,26 +74,37 @@ 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() {

@ -78,7 +78,7 @@ class BookshelfFragment : VMBaseFragment<BookshelfViewModel>(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()

@ -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) {

Loading…
Cancel
Save