This commit is contained in:
Horis 2024-11-07 17:21:44 +08:00
parent 436628c3a8
commit 5f8d44818e
3 changed files with 17 additions and 2 deletions

View File

@ -276,6 +276,9 @@ interface BookSourceDao {
@Query("select count(*) from book_sources")
fun allCount(): Int
@Query("SELECT EXISTS(select 1 from book_sources where bookSourceUrl = :key)")
fun has(key: String): Boolean
@Insert(onConflict = OnConflictStrategy.REPLACE)
fun insert(vararg bookSource: BookSource)

View File

@ -429,6 +429,10 @@ class BookInfoActivity :
tvOrigin.setOnClickListener {
viewModel.getBook()?.let { book ->
if (book.isLocal) return@let
if (!appDb.bookSourceDao.has(book.origin)) {
toastOnUi(R.string.error_no_source)
return@let
}
editSourceResult.launch {
putExtra("sourceUrl", book.origin)
}

View File

@ -73,8 +73,16 @@ class BookshelfViewModel(application: Application) : BaseViewModel(application)
kotlin.runCatching {
WebBook.getBookInfoAwait(bookSource, book)
}.onSuccess {
it.order = appDb.bookDao.minOrder - 1
it.save()
val dbBook = appDb.bookDao.getBook(it.name, it.author)
if (dbBook != null) {
val toc = WebBook.getChapterListAwait(bookSource, it).getOrThrow()
dbBook.migrateTo(it, toc)
appDb.bookDao.insert(it)
appDb.bookChapterDao.insert(*toc.toTypedArray())
} else {
it.order = appDb.bookDao.minOrder - 1
it.save()
}
successCount++
addBookProgressLiveData.postValue(successCount)
}