pull/1072/head
gedoor 3 years ago
parent e1b38ac694
commit bbc3dd3d55
  1. 2
      app/src/main/java/io/legado/app/api/controller/BookController.kt
  2. 3
      app/src/main/java/io/legado/app/data/dao/BookDao.kt
  3. 8
      app/src/main/java/io/legado/app/data/entities/Book.kt
  4. 4
      app/src/main/java/io/legado/app/ui/book/info/BookInfoViewModel.kt
  5. 4
      app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfViewModel.kt

@ -139,7 +139,7 @@ object BookController {
val book = GSON.fromJsonObject<Book>(postData) val book = GSON.fromJsonObject<Book>(postData)
val returnData = ReturnData() val returnData = ReturnData()
if (book != null) { if (book != null) {
appDb.bookDao.insert(book) book.save()
if (ReadBook.book?.bookUrl == book.bookUrl) { if (ReadBook.book?.bookUrl == book.bookUrl) {
ReadBook.book = book ReadBook.book = book
ReadBook.durChapterIndex = book.durChapterIndex ReadBook.durChapterIndex = book.durChapterIndex

@ -65,6 +65,9 @@ interface BookDao {
@get:Query("select max(`order`) from books") @get:Query("select max(`order`) from books")
val maxOrder: Int val maxOrder: Int
@Query("select 1 from books where bookUrl = :bookUrl")
fun has(bookUrl: String): Boolean?
@Insert(onConflict = OnConflictStrategy.REPLACE) @Insert(onConflict = OnConflictStrategy.REPLACE)
fun insert(vararg book: Book) fun insert(vararg book: Book)

@ -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 { companion object {
const val hTag = 2L const val hTag = 2L
const val rubyTag = 4L const val rubyTag = 4L

@ -201,7 +201,7 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) {
book.durChapterPos = it.durChapterPos book.durChapterPos = it.durChapterPos
book.durChapterTitle = it.durChapterTitle book.durChapterTitle = it.durChapterTitle
} }
appDb.bookDao.insert(book) book.save()
if (ReadBook.book?.name == book.name && ReadBook.book?.author == book.author) { if (ReadBook.book?.name == book.name && ReadBook.book?.author == book.author) {
ReadBook.book = book ReadBook.book = book
} }
@ -231,7 +231,7 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) {
book.durChapterPos = it.durChapterPos book.durChapterPos = it.durChapterPos
book.durChapterTitle = it.durChapterTitle book.durChapterTitle = it.durChapterTitle
} }
appDb.bookDao.insert(book) book.save()
} }
chapterListData.value?.let { chapterListData.value?.let {
appDb.bookChapterDao.insert(*it.toTypedArray()) appDb.bookChapterDao.insert(*it.toTypedArray())

@ -49,7 +49,7 @@ class BookshelfViewModel(application: Application) : BaseViewModel(application)
WebBook(bookSource).getBookInfo(this, book) WebBook(bookSource).getBookInfo(this, book)
.onSuccess(IO) { .onSuccess(IO) {
it.order = appDb.bookDao.maxOrder + 1 it.order = appDb.bookDao.maxOrder + 1
appDb.bookDao.insert(it) it.save()
successCount++ successCount++
}.onError { }.onError {
throw Exception(it.localizedMessage) throw Exception(it.localizedMessage)
@ -120,7 +120,7 @@ class BookshelfViewModel(application: Application) : BaseViewModel(application)
if (groupId > 0) { if (groupId > 0) {
book.group = groupId book.group = groupId
} }
appDb.bookDao.insert(book) book.save()
} }
} }
} }

Loading…
Cancel
Save