diff --git a/app/src/main/java/io/legado/app/data/dao/BookDao.kt b/app/src/main/java/io/legado/app/data/dao/BookDao.kt index 2e30f937d..21bad031b 100644 --- a/app/src/main/java/io/legado/app/data/dao/BookDao.kt +++ b/app/src/main/java/io/legado/app/data/dao/BookDao.kt @@ -47,4 +47,7 @@ interface BookDao { @Update fun update(vararg books: Book) + + @Query("delete from books where bookUrl = :bookUrl") + fun delete(bookUrl: String) } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/bookinfo/BookInfoActivity.kt b/app/src/main/java/io/legado/app/ui/bookinfo/BookInfoActivity.kt index 34d923d3d..36ed1d254 100644 --- a/app/src/main/java/io/legado/app/ui/bookinfo/BookInfoActivity.kt +++ b/app/src/main/java/io/legado/app/ui/bookinfo/BookInfoActivity.kt @@ -190,12 +190,7 @@ class BookInfoActivity : VMBaseActivity(R.layout.activity_boo } override fun changeTo(book: Book) { - viewModel.bookData.postValue(book) - if (book.tocUrl.isEmpty()) { - viewModel.loadBookInfo(book) - } else { - viewModel.loadChapter(book) - } + viewModel.changeTo(book) } override fun skipToChapter(index: Int) { diff --git a/app/src/main/java/io/legado/app/ui/bookinfo/BookInfoViewModel.kt b/app/src/main/java/io/legado/app/ui/bookinfo/BookInfoViewModel.kt index 77af4ea0d..a4f7a1b53 100644 --- a/app/src/main/java/io/legado/app/ui/bookinfo/BookInfoViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/bookinfo/BookInfoViewModel.kt @@ -81,6 +81,23 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) { } } + fun changeTo(book: Book) { + execute { + if (inBookshelf) { + bookData.value?.let { + App.db.bookDao().delete(it.bookUrl) + } + App.db.bookDao().insert(book) + } + bookData.postValue(book) + if (book.tocUrl.isEmpty()) { + loadBookInfo(book) + } else { + loadChapter(book) + } + } + } + fun saveBook(success: (() -> Unit)?) { execute { bookData.value?.let { book ->