From 33a0424a333ce0e62390fe5756f3de3d0a63e844 Mon Sep 17 00:00:00 2001 From: kunfei Date: Mon, 19 Aug 2019 16:59:35 +0800 Subject: [PATCH] up --- .../main/java/io/legado/app/data/dao/BookDao.kt | 3 +++ .../legado/app/ui/bookinfo/BookInfoActivity.kt | 7 +------ .../legado/app/ui/bookinfo/BookInfoViewModel.kt | 17 +++++++++++++++++ 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/io/legado/app/data/dao/BookDao.kt b/app/src/main/java/io/legado/app/data/dao/BookDao.kt index 2e30f937d..21bad031b 100644 --- a/app/src/main/java/io/legado/app/data/dao/BookDao.kt +++ b/app/src/main/java/io/legado/app/data/dao/BookDao.kt @@ -47,4 +47,7 @@ interface BookDao { @Update fun update(vararg books: Book) + + @Query("delete from books where bookUrl = :bookUrl") + fun delete(bookUrl: String) } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/bookinfo/BookInfoActivity.kt b/app/src/main/java/io/legado/app/ui/bookinfo/BookInfoActivity.kt index 34d923d3d..36ed1d254 100644 --- a/app/src/main/java/io/legado/app/ui/bookinfo/BookInfoActivity.kt +++ b/app/src/main/java/io/legado/app/ui/bookinfo/BookInfoActivity.kt @@ -190,12 +190,7 @@ class BookInfoActivity : VMBaseActivity(R.layout.activity_boo } override fun changeTo(book: Book) { - viewModel.bookData.postValue(book) - if (book.tocUrl.isEmpty()) { - viewModel.loadBookInfo(book) - } else { - viewModel.loadChapter(book) - } + viewModel.changeTo(book) } override fun skipToChapter(index: Int) { diff --git a/app/src/main/java/io/legado/app/ui/bookinfo/BookInfoViewModel.kt b/app/src/main/java/io/legado/app/ui/bookinfo/BookInfoViewModel.kt index 77af4ea0d..a4f7a1b53 100644 --- a/app/src/main/java/io/legado/app/ui/bookinfo/BookInfoViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/bookinfo/BookInfoViewModel.kt @@ -81,6 +81,23 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) { } } + fun changeTo(book: Book) { + execute { + if (inBookshelf) { + bookData.value?.let { + App.db.bookDao().delete(it.bookUrl) + } + App.db.bookDao().insert(book) + } + bookData.postValue(book) + if (book.tocUrl.isEmpty()) { + loadBookInfo(book) + } else { + loadChapter(book) + } + } + } + fun saveBook(success: (() -> Unit)?) { execute { bookData.value?.let { book ->