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

Loading…
Cancel
Save