diff --git a/app/src/main/java/io/legado/app/data/dao/BookChapterDao.kt b/app/src/main/java/io/legado/app/data/dao/BookChapterDao.kt index fdbff2bf1..8ed58cadf 100644 --- a/app/src/main/java/io/legado/app/data/dao/BookChapterDao.kt +++ b/app/src/main/java/io/legado/app/data/dao/BookChapterDao.kt @@ -25,4 +25,8 @@ interface BookChapterDao { @Insert(onConflict = OnConflictStrategy.REPLACE) fun insert(vararg bookChapter: BookChapter) + + @Query("delete from chapters where bookUrl = :bookUrl") + fun delByBook(bookUrl: String) + } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfViewModel.kt b/app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfViewModel.kt index 463c0b015..b1fd24966 100644 --- a/app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfViewModel.kt @@ -39,14 +39,18 @@ class BookshelfViewModel(application: Application) : BaseViewModel(application) updateList.add(book.bookUrl) postEvent(Bus.UP_BOOK, book.bookUrl) } - WebBook(bookSource).getChapterList(book).onSuccess(IO) { + WebBook(bookSource).getChapterList(book) + .onSuccess(IO) { it?.let { App.db.bookDao().update(book) - if (it.size > App.db.bookChapterDao().getChapterCount(book.bookUrl)) { - App.db.bookChapterDao().insert(*it.toTypedArray()) - } + App.db.bookChapterDao().delByBook(book.bookUrl) + App.db.bookChapterDao().insert(*it.toTypedArray()) } - }.onFinally { + } + .onError { + it.printStackTrace() + } + .onFinally { synchronized(this) { updateList.remove(book.bookUrl) postEvent(Bus.UP_BOOK, book.bookUrl)