pull/1759/head
kunfei 3 years ago
parent aa6039273d
commit 3a2ed37f1f
  1. 4
      app/src/main/java/io/legado/app/data/entities/Book.kt
  2. 2
      app/src/main/java/io/legado/app/ui/book/audio/AudioPlayActivity.kt
  3. 4
      app/src/main/java/io/legado/app/ui/book/audio/AudioPlayViewModel.kt
  4. 11
      app/src/main/java/io/legado/app/ui/book/info/BookInfoViewModel.kt
  5. 1
      app/src/main/java/io/legado/app/ui/book/manage/BookshelfManageViewModel.kt
  6. 1
      app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt
  7. 3
      app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt

@ -287,10 +287,6 @@ data class Book(
newBook.customTag = customTag newBook.customTag = customTag
newBook.canUpdate = canUpdate newBook.canUpdate = canUpdate
newBook.readConfig = readConfig newBook.readConfig = readConfig
if (appDb.bookDao.has(bookUrl) == true) {
delete()
appDb.bookDao.insert(newBook)
}
return newBook return newBook
} }

@ -16,6 +16,7 @@ import io.legado.app.constant.BookType
import io.legado.app.constant.EventBus import io.legado.app.constant.EventBus
import io.legado.app.constant.Status import io.legado.app.constant.Status
import io.legado.app.constant.Theme import io.legado.app.constant.Theme
import io.legado.app.data.appDb
import io.legado.app.data.entities.Book import io.legado.app.data.entities.Book
import io.legado.app.data.entities.BookChapter import io.legado.app.data.entities.BookChapter
import io.legado.app.data.entities.BookSource import io.legado.app.data.entities.BookSource
@ -212,6 +213,7 @@ class AudioPlayActivity :
launch { launch {
withContext(IO) { withContext(IO) {
AudioPlay.book?.changeTo(book, toc) AudioPlay.book?.changeTo(book, toc)
appDb.bookDao.insert(book)
} }
startActivity<ReadBookActivity> { startActivity<ReadBookActivity> {
putExtra("bookUrl", book.bookUrl) putExtra("bookUrl", book.bookUrl)

@ -78,7 +78,9 @@ class AudioPlayViewModel(application: Application) : BaseViewModel(application)
fun changeTo(source: BookSource, book: Book, toc: List<BookChapter>) { fun changeTo(source: BookSource, book: Book, toc: List<BookChapter>) {
execute { execute {
AudioPlay.book = AudioPlay.book!!.changeTo(book, toc) AudioPlay.book?.changeTo(book, toc)
appDb.bookDao.insert(book)
AudioPlay.book = book
AudioPlay.bookSource = source AudioPlay.bookSource = source
appDb.bookChapterDao.insert(*toc.toTypedArray()) appDb.bookChapterDao.insert(*toc.toTypedArray())
AudioPlay.upDurChapter(book) AudioPlay.upDurChapter(book)

@ -178,18 +178,19 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) {
} }
} }
fun changeTo(source: BookSource, newBook: Book, toc: List<BookChapter>) { fun changeTo(source: BookSource, book: Book, toc: List<BookChapter>) {
changeSourceCoroutine?.cancel() changeSourceCoroutine?.cancel()
changeSourceCoroutine = execute { changeSourceCoroutine = execute {
bookSource = source bookSource = source
bookData.value!!.changeTo(newBook, toc) bookData.value?.changeTo(book, toc)
bookData.postValue(newBook)
chapterListData.postValue(toc)
if (inBookshelf) { if (inBookshelf) {
appDb.bookDao.insert(book)
appDb.bookChapterDao.insert(*toc.toTypedArray()) appDb.bookChapterDao.insert(*toc.toTypedArray())
} }
bookData.postValue(book)
chapterListData.postValue(toc)
}.onFinally { }.onFinally {
postEvent(EventBus.SOURCE_CHANGED, newBook.bookUrl) postEvent(EventBus.SOURCE_CHANGED, book.bookUrl)
} }
} }

@ -50,6 +50,7 @@ class BookshelfManageViewModel(application: Application) : BaseViewModel(applica
.getOrNull()?.let { newBook -> .getOrNull()?.let { newBook ->
val toc = WebBook.getChapterListAwait(this, source, newBook) val toc = WebBook.getChapterListAwait(this, source, newBook)
book.changeTo(newBook, toc) book.changeTo(newBook, toc)
appDb.bookDao.insert(newBook)
appDb.bookChapterDao.insert(*toc.toTypedArray()) appDb.bookChapterDao.insert(*toc.toTypedArray())
} }
} }

@ -724,6 +724,7 @@ class ReadBookActivity : BaseReadBookActivity(),
ReadAloud.stop(this) ReadAloud.stop(this)
launch { launch {
ReadBook.book?.changeTo(book, toc) ReadBook.book?.changeTo(book, toc)
appDb.bookDao.insert(book)
} }
startActivity<AudioPlayActivity> { startActivity<AudioPlayActivity> {
putExtra("bookUrl", book.bookUrl) putExtra("bookUrl", book.bookUrl)

@ -195,7 +195,7 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
changeSourceCoroutine?.cancel() changeSourceCoroutine?.cancel()
changeSourceCoroutine = execute { changeSourceCoroutine = execute {
ReadBook.upMsg(context.getString(R.string.loading)) ReadBook.upMsg(context.getString(R.string.loading))
ReadBook.book!!.changeTo(book, toc) ReadBook.book?.changeTo(book, toc)
val nextChapter = toc.getOrElse(book.durChapterIndex) { val nextChapter = toc.getOrElse(book.durChapterIndex) {
toc.first() toc.first()
} }
@ -206,6 +206,7 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
bookChapter = toc[book.durChapterIndex], bookChapter = toc[book.durChapterIndex],
nextChapterUrl = nextChapter.url nextChapterUrl = nextChapter.url
) )
appDb.bookDao.insert(book)
appDb.bookChapterDao.insert(*toc.toTypedArray()) appDb.bookChapterDao.insert(*toc.toTypedArray())
ReadBook.resetData(book) ReadBook.resetData(book)
ReadBook.upMsg(null) ReadBook.upMsg(null)

Loading…
Cancel
Save