From 3a2ed37f1f48ffb3f9348a6a6a6d50ec2fb57293 Mon Sep 17 00:00:00 2001 From: kunfei Date: Thu, 7 Apr 2022 15:46:14 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/java/io/legado/app/data/entities/Book.kt | 4 ---- .../io/legado/app/ui/book/audio/AudioPlayActivity.kt | 2 ++ .../io/legado/app/ui/book/audio/AudioPlayViewModel.kt | 4 +++- .../io/legado/app/ui/book/info/BookInfoViewModel.kt | 11 ++++++----- .../app/ui/book/manage/BookshelfManageViewModel.kt | 1 + .../io/legado/app/ui/book/read/ReadBookActivity.kt | 1 + .../io/legado/app/ui/book/read/ReadBookViewModel.kt | 3 ++- 7 files changed, 15 insertions(+), 11 deletions(-) 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 8a3a632d5..568778d14 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 @@ -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 } diff --git a/app/src/main/java/io/legado/app/ui/book/audio/AudioPlayActivity.kt b/app/src/main/java/io/legado/app/ui/book/audio/AudioPlayActivity.kt index 4f9b53240..778aef4d2 100644 --- a/app/src/main/java/io/legado/app/ui/book/audio/AudioPlayActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/audio/AudioPlayActivity.kt @@ -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 @@ -212,6 +213,7 @@ class AudioPlayActivity : launch { withContext(IO) { AudioPlay.book?.changeTo(book, toc) + appDb.bookDao.insert(book) } startActivity { putExtra("bookUrl", book.bookUrl) diff --git a/app/src/main/java/io/legado/app/ui/book/audio/AudioPlayViewModel.kt b/app/src/main/java/io/legado/app/ui/book/audio/AudioPlayViewModel.kt index 4ea1acad5..4938728a8 100644 --- a/app/src/main/java/io/legado/app/ui/book/audio/AudioPlayViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/audio/AudioPlayViewModel.kt @@ -78,7 +78,9 @@ class AudioPlayViewModel(application: Application) : BaseViewModel(application) fun changeTo(source: BookSource, book: Book, toc: List) { 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) 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 35069c8ec..f127f754b 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 @@ -178,18 +178,19 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) { } } - fun changeTo(source: BookSource, newBook: Book, toc: List) { + fun changeTo(source: BookSource, book: Book, toc: List) { 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) } } diff --git a/app/src/main/java/io/legado/app/ui/book/manage/BookshelfManageViewModel.kt b/app/src/main/java/io/legado/app/ui/book/manage/BookshelfManageViewModel.kt index c404851e3..340363742 100644 --- a/app/src/main/java/io/legado/app/ui/book/manage/BookshelfManageViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/manage/BookshelfManageViewModel.kt @@ -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()) } } diff --git a/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt b/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt index 431b73029..b19e7325b 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt @@ -724,6 +724,7 @@ class ReadBookActivity : BaseReadBookActivity(), ReadAloud.stop(this) launch { ReadBook.book?.changeTo(book, toc) + appDb.bookDao.insert(book) } startActivity { putExtra("bookUrl", book.bookUrl) diff --git a/app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt b/app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt index 3293dba25..ccdf872e7 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt @@ -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() } @@ -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)