diff --git a/app/src/main/java/io/legado/app/ui/book/info/BookInfoActivity.kt b/app/src/main/java/io/legado/app/ui/book/info/BookInfoActivity.kt index 18872071e..0efdf63d5 100644 --- a/app/src/main/java/io/legado/app/ui/book/info/BookInfoActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/info/BookInfoActivity.kt @@ -270,7 +270,7 @@ class BookInfoActivity : showDialogFragment( ChangeCoverDialog(it.name, it.author) ) - } + } ?: toastOnUi("Book is null") } ivCover.setOnLongClickListener { viewModel.bookData.value?.getDisplayCover()?.let { path -> @@ -281,7 +281,7 @@ class BookInfoActivity : tvRead.setOnClickListener { viewModel.bookData.value?.let { readBook(it) - } + } ?: toastOnUi("Book is null") } tvShelf.setOnClickListener { if (viewModel.inBookshelf) { @@ -297,10 +297,12 @@ class BookInfoActivity : startActivity { putExtra("sourceUrl", it.origin) } - } + } ?: toastOnUi("Book is null") } tvChangeSource.setOnClickListener { - showDialogFragment(ChangeBookSourceDialog(viewModel.name, viewModel.author)) + viewModel.bookData.value?.let { book -> + showDialogFragment(ChangeBookSourceDialog(book.name, book.author)) + } ?: toastOnUi("Book is null") } tvTocView.setOnClickListener { if (!viewModel.inBookshelf) { @@ -318,7 +320,7 @@ class BookInfoActivity : showDialogFragment( GroupSelectDialog(it.group) ) - } + } ?: toastOnUi("Book is null") } tvAuthor.setOnClickListener { startActivity { 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 f127f754b..9eeda4018 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 @@ -27,8 +27,6 @@ import kotlinx.coroutines.Dispatchers.IO class BookInfoViewModel(application: Application) : BaseViewModel(application) { val bookData = MutableLiveData() val chapterListData = MutableLiveData>() - var name = "" - var author = "" var durChapterIndex = 0 var inBookshelf = false var bookSource: BookSource? = null @@ -36,22 +34,22 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) { fun initData(intent: Intent) { execute { - name = intent.getStringExtra("name") ?: "" - author = intent.getStringExtra("author") ?: "" + val name = intent.getStringExtra("name") ?: "" + val author = intent.getStringExtra("author") ?: "" val bookUrl = intent.getStringExtra("bookUrl") ?: "" appDb.bookDao.getBook(name, author)?.let { inBookshelf = true - setBook(it) + upBook(it) return@execute } if (bookUrl.isNotBlank()) { appDb.searchBookDao.getSearchBook(bookUrl)?.toBook()?.let { - setBook(it) + upBook(it) return@execute } } appDb.searchBookDao.getFirstByNameAuthor(name, author)?.toBook()?.let { - setBook(it) + upBook(it) return@execute } throw NoStackTraceException("未找到书籍") @@ -65,12 +63,12 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) { val name = intent.getStringExtra("name") ?: "" val author = intent.getStringExtra("author") ?: "" appDb.bookDao.getBook(name, author)?.let { book -> - setBook(book) + upBook(book) } } } - private fun setBook(book: Book) { + private fun upBook(book: Book) { execute { durChapterIndex = book.durChapterIndex bookData.postValue(book)