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 eca15ba9c..4ea63281d 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 @@ -11,6 +11,7 @@ import io.legado.app.base.VMBaseActivity import io.legado.app.data.entities.Book import io.legado.app.data.entities.BookChapter import io.legado.app.help.ImageLoader +import io.legado.app.help.IntentDataHelp import io.legado.app.lib.theme.ATH import io.legado.app.ui.book.info.edit.BookInfoEditActivity import io.legado.app.ui.book.read.ReadBookActivity @@ -46,7 +47,7 @@ class BookInfoActivity : VMBaseActivity(R.layout.activity_boo viewModel.chapterListData.value?.let { chapters -> showChapter(chapters) } - } ?: viewModel.loadBook(intent) + } ?: viewModel.initData(intent) initOnClick() } @@ -210,19 +211,24 @@ class BookInfoActivity : VMBaseActivity(R.layout.activity_boo if (!viewModel.inBookshelf) { viewModel.saveBook { viewModel.saveChapterList { - startActivity( - Pair("bookUrl", book.bookUrl), - Pair("inBookshelf", false) - ) + startReadActivity(book) } } } else { viewModel.saveBook { - startActivity(Pair("bookUrl", book.bookUrl)) + startReadActivity(book) } } } + private fun startReadActivity(book: Book) { + startActivity( + Pair("bookUrl", book.bookUrl), + Pair("inBookshelf", viewModel.inBookshelf), + Pair("key", IntentDataHelp.putData(book)) + ) + } + override val curOrigin: String? get() = viewModel.bookData.value?.origin 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 9cd9ed887..e643b0b91 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 @@ -21,19 +21,16 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) { var durChapterIndex = 0 var inBookshelf = false - fun loadBook(intent: Intent) { + fun initData(intent: Intent) { execute { IntentDataHelp.getData(intent.getStringExtra("key"))?.let { book -> - inBookshelf = App.db.bookDao().getBook(book.bookUrl) != null setBook(book) } ?: intent.getStringExtra("bookUrl")?.let { App.db.bookDao().getBook(it)?.let { book -> - inBookshelf = true setBook(book) } } ?: intent.getStringExtra("searchBookUrl")?.let { App.db.searchBookDao().getSearchBook(it)?.toBook()?.let { book -> - inBookshelf = App.db.bookDao().getBook(book.bookUrl) != null setBook(book) } } @@ -41,6 +38,7 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) { } private fun setBook(book: Book) { + inBookshelf = App.db.bookDao().getBook(book.bookUrl) != null durChapterIndex = book.durChapterIndex bookData.postValue(book) if (book.tocUrl.isEmpty()) { 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 d6d6ae004..667afb8ef 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 @@ -10,6 +10,7 @@ import io.legado.app.constant.BookType import io.legado.app.data.entities.Book import io.legado.app.data.entities.BookChapter import io.legado.app.help.BookHelp +import io.legado.app.help.IntentDataHelp import io.legado.app.help.ReadAloud import io.legado.app.model.WebBook import io.legado.app.ui.widget.page.TextChapter @@ -38,35 +39,41 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) { fun initData(intent: Intent) { execute { inBookshelf = intent.getBooleanExtra("inBookshelf", true) - val bookUrl = intent.getStringExtra("bookUrl") - val book = if (!bookUrl.isNullOrEmpty()) { - App.db.bookDao().getBook(bookUrl) + IntentDataHelp.getData(intent.getStringExtra("key"))?.let { + setBook(it) + } ?: intent.getStringExtra("bookUrl")?.let { + App.db.bookDao().getBook(it)?.let { book -> + setBook(book) + } + } ?: App.db.bookDao().lastReadBook?.let { + setBook(it) + } + } + } + + private fun setBook(book: Book) { + durChapterIndex = book.durChapterIndex + durPageIndex = book.durChapterPos + isLocalBook = book.origin == BookType.local + bookData.postValue(book) + App.db.bookSourceDao().getBookSource(book.origin)?.let { + webBook = WebBook(it) + } + val count = App.db.bookChapterDao().getChapterCount(book.bookUrl) + if (count == 0) { + if (book.tocUrl.isEmpty()) { + loadBookInfo(book) } else { - App.db.bookDao().lastReadBook + loadChapterList(book) } - book?.let { - durChapterIndex = book.durChapterIndex - durPageIndex = book.durChapterPos - isLocalBook = book.origin == BookType.local - bookData.postValue(book) - App.db.bookSourceDao().getBookSource(book.origin)?.let { - webBook = WebBook(it) - } - val count = App.db.bookChapterDao().getChapterCount(book.bookUrl) - if (count == 0) { - if (book.tocUrl.isEmpty()) { - loadBookInfo(book) - } else { - loadChapterList(book) - } - } else { - if (durChapterIndex > count - 1) { - durChapterIndex = count - 1 - } - chapterSize = count - chapterListFinish.postValue(true) - } + } else { + if (durChapterIndex > count - 1) { + durChapterIndex = count - 1 } + chapterSize = count + chapterListFinish.postValue(true) + } + if (inBookshelf) { saveRead(book) } } diff --git a/app/src/main/java/io/legado/app/ui/main/bookshelf/BooksFragment.kt b/app/src/main/java/io/legado/app/ui/main/bookshelf/BooksFragment.kt index 2bef1ad1d..1cf3e17de 100644 --- a/app/src/main/java/io/legado/app/ui/main/bookshelf/BooksFragment.kt +++ b/app/src/main/java/io/legado/app/ui/main/bookshelf/BooksFragment.kt @@ -14,6 +14,7 @@ import io.legado.app.base.VMBaseFragment import io.legado.app.constant.BookType import io.legado.app.constant.Bus import io.legado.app.data.entities.Book +import io.legado.app.help.IntentDataHelp import io.legado.app.lib.theme.ATH import io.legado.app.lib.theme.accentColor import io.legado.app.ui.audio.AudioPlayActivity @@ -98,7 +99,10 @@ class BooksFragment : VMBaseFragment(R.layout.fragment_books), when (book.type) { BookType.audio -> context?.startActivity(Pair("bookUrl", book.bookUrl)) - else -> context?.startActivity(Pair("bookUrl", book.bookUrl)) + else -> context?.startActivity( + Pair("bookUrl", book.bookUrl), + Pair("key", IntentDataHelp.putData(book)) + ) } }