Skip to content

Commit

Permalink
优化
Browse files Browse the repository at this point in the history
  • Loading branch information
gedoor committed Apr 7, 2022
1 parent aa60392 commit 3a2ed37
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 11 deletions.
4 changes: 0 additions & 4 deletions app/src/main/java/io/legado/app/data/entities/Book.kt
Original file line number Diff line number Diff line change
Expand Up @@ -287,10 +287,6 @@ data class Book(
newBook.customTag = customTag
newBook.canUpdate = canUpdate
newBook.readConfig = readConfig
if (appDb.bookDao.has(bookUrl) == true) {
delete()
appDb.bookDao.insert(newBook)
}
return newBook
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import io.legado.app.constant.BookType
import io.legado.app.constant.EventBus
import io.legado.app.constant.Status
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.BookChapter
import io.legado.app.data.entities.BookSource
Expand Down Expand Up @@ -212,6 +213,7 @@ class AudioPlayActivity :
launch {
withContext(IO) {
AudioPlay.book?.changeTo(book, toc)
appDb.bookDao.insert(book)
}
startActivity<ReadBookActivity> {
putExtra("bookUrl", book.bookUrl)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,9 @@ class AudioPlayViewModel(application: Application) : BaseViewModel(application)

fun changeTo(source: BookSource, book: Book, toc: List<BookChapter>) {
execute {
AudioPlay.book = AudioPlay.book!!.changeTo(book, toc)
AudioPlay.book?.changeTo(book, toc)
appDb.bookDao.insert(book)
AudioPlay.book = book
AudioPlay.bookSource = source
appDb.bookChapterDao.insert(*toc.toTypedArray())
AudioPlay.upDurChapter(book)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = execute {
bookSource = source
bookData.value!!.changeTo(newBook, toc)
bookData.postValue(newBook)
chapterListData.postValue(toc)
bookData.value?.changeTo(book, toc)
if (inBookshelf) {
appDb.bookDao.insert(book)
appDb.bookChapterDao.insert(*toc.toTypedArray())
}
bookData.postValue(book)
chapterListData.postValue(toc)
}.onFinally {
postEvent(EventBus.SOURCE_CHANGED, newBook.bookUrl)
postEvent(EventBus.SOURCE_CHANGED, book.bookUrl)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class BookshelfManageViewModel(application: Application) : BaseViewModel(applica
.getOrNull()?.let { newBook ->
val toc = WebBook.getChapterListAwait(this, source, newBook)
book.changeTo(newBook, toc)
appDb.bookDao.insert(newBook)
appDb.bookChapterDao.insert(*toc.toTypedArray())
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -724,6 +724,7 @@ class ReadBookActivity : BaseReadBookActivity(),
ReadAloud.stop(this)
launch {
ReadBook.book?.changeTo(book, toc)
appDb.bookDao.insert(book)
}
startActivity<AudioPlayActivity> {
putExtra("bookUrl", book.bookUrl)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
changeSourceCoroutine?.cancel()
changeSourceCoroutine = execute {
ReadBook.upMsg(context.getString(R.string.loading))
ReadBook.book!!.changeTo(book, toc)
ReadBook.book?.changeTo(book, toc)
val nextChapter = toc.getOrElse(book.durChapterIndex) {
toc.first()
}
Expand All @@ -206,6 +206,7 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
bookChapter = toc[book.durChapterIndex],
nextChapterUrl = nextChapter.url
)
appDb.bookDao.insert(book)
appDb.bookChapterDao.insert(*toc.toTypedArray())
ReadBook.resetData(book)
ReadBook.upMsg(null)
Expand Down

0 comments on commit 3a2ed37

Please sign in to comment.