pull/37/head
kunfei 5 years ago
parent c323a43899
commit c1c5b3b90a
  1. 39
      app/src/main/java/io/legado/app/ui/main/MainViewModel.kt

@ -7,6 +7,7 @@ import io.legado.app.constant.BookType
import io.legado.app.constant.Bus import io.legado.app.constant.Bus
import io.legado.app.help.storage.Restore import io.legado.app.help.storage.Restore
import io.legado.app.model.WebBook import io.legado.app.model.WebBook
import io.legado.app.utils.LogUtils
import io.legado.app.utils.postEvent import io.legado.app.utils.postEvent
import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.delay import kotlinx.coroutines.delay
@ -25,25 +26,29 @@ class MainViewModel(application: Application) : BaseViewModel(application) {
execute { execute {
App.db.bookDao().allBooks.forEach { book -> App.db.bookDao().allBooks.forEach { book ->
if (book.origin != BookType.local) { if (book.origin != BookType.local) {
App.db.bookSourceDao().getBookSource(book.origin)?.let { bookSource -> if (!updateList.contains(book.bookUrl)) {
synchronized(this) { App.db.bookSourceDao().getBookSource(book.origin)?.let { bookSource ->
updateList.add(book.bookUrl) synchronized(this) {
postEvent(Bus.UP_BOOK, book.bookUrl) updateList.add(book.bookUrl)
} LogUtils.d("updateAdd", book.name)
WebBook(bookSource).getChapterList(book) postEvent(Bus.UP_BOOK, book.bookUrl)
.onSuccess(IO) {
it?.let {
App.db.bookDao().update(book)
App.db.bookChapterDao().delByBook(book.bookUrl)
App.db.bookChapterDao().insert(*it.toTypedArray())
}
} }
.onFinally { WebBook(bookSource).getChapterList(book)
synchronized(this) { .onSuccess(IO) {
updateList.remove(book.bookUrl) it?.let {
postEvent(Bus.UP_BOOK, book.bookUrl) App.db.bookDao().update(book)
App.db.bookChapterDao().delByBook(book.bookUrl)
App.db.bookChapterDao().insert(*it.toTypedArray())
}
} }
} .onFinally {
synchronized(this) {
updateList.remove(book.bookUrl)
LogUtils.d("updateRemove", book.name)
postEvent(Bus.UP_BOOK, book.bookUrl)
}
}
}
} }
} }
delay(50) delay(50)

Loading…
Cancel
Save