diff --git a/app/src/main/java/io/legado/app/api/controller/BookController.kt b/app/src/main/java/io/legado/app/api/controller/BookController.kt index 60e7c9ec8..daccc43a7 100644 --- a/app/src/main/java/io/legado/app/api/controller/BookController.kt +++ b/app/src/main/java/io/legado/app/api/controller/BookController.kt @@ -139,7 +139,7 @@ object BookController { val book = GSON.fromJsonObject(postData) val returnData = ReturnData() if (book != null) { - appDb.bookDao.insert(book) + book.save() if (ReadBook.book?.bookUrl == book.bookUrl) { ReadBook.book = book ReadBook.durChapterIndex = book.durChapterIndex 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 aedc577f7..9a2a5a0db 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 @@ -65,6 +65,9 @@ interface BookDao { @get:Query("select max(`order`) from books") val maxOrder: Int + @Query("select 1 from books where bookUrl = :bookUrl") + fun has(bookUrl: String): Boolean? + @Insert(onConflict = OnConflictStrategy.REPLACE) fun insert(vararg book: Book) diff --git a/app/src/main/java/io/legado/app/data/entities/Book.kt b/app/src/main/java/io/legado/app/data/entities/Book.kt index b192b9a1b..ba33ba78e 100644 --- a/app/src/main/java/io/legado/app/data/entities/Book.kt +++ b/app/src/main/java/io/legado/app/data/entities/Book.kt @@ -240,6 +240,14 @@ data class Book( ) } + fun save() { + if (appDb.bookDao.has(bookUrl) == true) { + appDb.bookDao.update(this) + } else { + appDb.bookDao.insert(this) + } + } + companion object { const val hTag = 2L const val rubyTag = 4L diff --git a/app/src/main/java/io/legado/app/ui/book/info/BookInfoViewModel.kt b/app/src/main/java/io/legado/app/ui/book/info/BookInfoViewModel.kt index 0db62f64a..202081760 100644 --- a/app/src/main/java/io/legado/app/ui/book/info/BookInfoViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/info/BookInfoViewModel.kt @@ -201,7 +201,7 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) { book.durChapterPos = it.durChapterPos book.durChapterTitle = it.durChapterTitle } - appDb.bookDao.insert(book) + book.save() if (ReadBook.book?.name == book.name && ReadBook.book?.author == book.author) { ReadBook.book = book } @@ -231,7 +231,7 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) { book.durChapterPos = it.durChapterPos book.durChapterTitle = it.durChapterTitle } - appDb.bookDao.insert(book) + book.save() } chapterListData.value?.let { appDb.bookChapterDao.insert(*it.toTypedArray()) 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 ac9f91190..64ab4efb7 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 @@ -49,7 +49,7 @@ class BookshelfViewModel(application: Application) : BaseViewModel(application) WebBook(bookSource).getBookInfo(this, book) .onSuccess(IO) { it.order = appDb.bookDao.maxOrder + 1 - appDb.bookDao.insert(it) + it.save() successCount++ }.onError { throw Exception(it.localizedMessage) @@ -120,7 +120,7 @@ class BookshelfViewModel(application: Application) : BaseViewModel(application) if (groupId > 0) { book.group = groupId } - appDb.bookDao.insert(book) + book.save() } } }