pull/43/head
kunfei 5 years ago
parent e2c2d9e408
commit 875e4b218b
  1. 18
      app/src/main/java/io/legado/app/ui/book/info/BookInfoActivity.kt
  2. 6
      app/src/main/java/io/legado/app/ui/book/info/BookInfoViewModel.kt
  3. 59
      app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt
  4. 6
      app/src/main/java/io/legado/app/ui/main/bookshelf/BooksFragment.kt

@ -11,6 +11,7 @@ import io.legado.app.base.VMBaseActivity
import io.legado.app.data.entities.Book import io.legado.app.data.entities.Book
import io.legado.app.data.entities.BookChapter import io.legado.app.data.entities.BookChapter
import io.legado.app.help.ImageLoader import io.legado.app.help.ImageLoader
import io.legado.app.help.IntentDataHelp
import io.legado.app.lib.theme.ATH import io.legado.app.lib.theme.ATH
import io.legado.app.ui.book.info.edit.BookInfoEditActivity import io.legado.app.ui.book.info.edit.BookInfoEditActivity
import io.legado.app.ui.book.read.ReadBookActivity import io.legado.app.ui.book.read.ReadBookActivity
@ -46,7 +47,7 @@ class BookInfoActivity : VMBaseActivity<BookInfoViewModel>(R.layout.activity_boo
viewModel.chapterListData.value?.let { chapters -> viewModel.chapterListData.value?.let { chapters ->
showChapter(chapters) showChapter(chapters)
} }
} ?: viewModel.loadBook(intent) } ?: viewModel.initData(intent)
initOnClick() initOnClick()
} }
@ -210,19 +211,24 @@ class BookInfoActivity : VMBaseActivity<BookInfoViewModel>(R.layout.activity_boo
if (!viewModel.inBookshelf) { if (!viewModel.inBookshelf) {
viewModel.saveBook { viewModel.saveBook {
viewModel.saveChapterList { viewModel.saveChapterList {
startActivity<ReadBookActivity>( startReadActivity(book)
Pair("bookUrl", book.bookUrl),
Pair("inBookshelf", false)
)
} }
} }
} else { } else {
viewModel.saveBook { viewModel.saveBook {
startActivity<ReadBookActivity>(Pair("bookUrl", book.bookUrl)) startReadActivity(book)
} }
} }
} }
private fun startReadActivity(book: Book) {
startActivity<ReadBookActivity>(
Pair("bookUrl", book.bookUrl),
Pair("inBookshelf", viewModel.inBookshelf),
Pair("key", IntentDataHelp.putData(book))
)
}
override val curOrigin: String? override val curOrigin: String?
get() = viewModel.bookData.value?.origin get() = viewModel.bookData.value?.origin

@ -21,19 +21,16 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) {
var durChapterIndex = 0 var durChapterIndex = 0
var inBookshelf = false var inBookshelf = false
fun loadBook(intent: Intent) { fun initData(intent: Intent) {
execute { execute {
IntentDataHelp.getData<Book>(intent.getStringExtra("key"))?.let { book -> IntentDataHelp.getData<Book>(intent.getStringExtra("key"))?.let { book ->
inBookshelf = App.db.bookDao().getBook(book.bookUrl) != null
setBook(book) setBook(book)
} ?: intent.getStringExtra("bookUrl")?.let { } ?: intent.getStringExtra("bookUrl")?.let {
App.db.bookDao().getBook(it)?.let { book -> App.db.bookDao().getBook(it)?.let { book ->
inBookshelf = true
setBook(book) setBook(book)
} }
} ?: intent.getStringExtra("searchBookUrl")?.let { } ?: intent.getStringExtra("searchBookUrl")?.let {
App.db.searchBookDao().getSearchBook(it)?.toBook()?.let { book -> App.db.searchBookDao().getSearchBook(it)?.toBook()?.let { book ->
inBookshelf = App.db.bookDao().getBook(book.bookUrl) != null
setBook(book) setBook(book)
} }
} }
@ -41,6 +38,7 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) {
} }
private fun setBook(book: Book) { private fun setBook(book: Book) {
inBookshelf = App.db.bookDao().getBook(book.bookUrl) != null
durChapterIndex = book.durChapterIndex durChapterIndex = book.durChapterIndex
bookData.postValue(book) bookData.postValue(book)
if (book.tocUrl.isEmpty()) { if (book.tocUrl.isEmpty()) {

@ -10,6 +10,7 @@ import io.legado.app.constant.BookType
import io.legado.app.data.entities.Book import io.legado.app.data.entities.Book
import io.legado.app.data.entities.BookChapter import io.legado.app.data.entities.BookChapter
import io.legado.app.help.BookHelp import io.legado.app.help.BookHelp
import io.legado.app.help.IntentDataHelp
import io.legado.app.help.ReadAloud import io.legado.app.help.ReadAloud
import io.legado.app.model.WebBook import io.legado.app.model.WebBook
import io.legado.app.ui.widget.page.TextChapter import io.legado.app.ui.widget.page.TextChapter
@ -38,35 +39,41 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
fun initData(intent: Intent) { fun initData(intent: Intent) {
execute { execute {
inBookshelf = intent.getBooleanExtra("inBookshelf", true) inBookshelf = intent.getBooleanExtra("inBookshelf", true)
val bookUrl = intent.getStringExtra("bookUrl") IntentDataHelp.getData<Book>(intent.getStringExtra("key"))?.let {
val book = if (!bookUrl.isNullOrEmpty()) { setBook(it)
App.db.bookDao().getBook(bookUrl) } ?: 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 { } else {
App.db.bookDao().lastReadBook loadChapterList(book)
} }
book?.let { } else {
durChapterIndex = book.durChapterIndex if (durChapterIndex > count - 1) {
durPageIndex = book.durChapterPos durChapterIndex = count - 1
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)
}
} }
chapterSize = count
chapterListFinish.postValue(true)
}
if (inBookshelf) {
saveRead(book) saveRead(book)
} }
} }

@ -14,6 +14,7 @@ import io.legado.app.base.VMBaseFragment
import io.legado.app.constant.BookType import io.legado.app.constant.BookType
import io.legado.app.constant.Bus import io.legado.app.constant.Bus
import io.legado.app.data.entities.Book 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.ATH
import io.legado.app.lib.theme.accentColor import io.legado.app.lib.theme.accentColor
import io.legado.app.ui.audio.AudioPlayActivity import io.legado.app.ui.audio.AudioPlayActivity
@ -98,7 +99,10 @@ class BooksFragment : VMBaseFragment<BooksViewModel>(R.layout.fragment_books),
when (book.type) { when (book.type) {
BookType.audio -> BookType.audio ->
context?.startActivity<AudioPlayActivity>(Pair("bookUrl", book.bookUrl)) context?.startActivity<AudioPlayActivity>(Pair("bookUrl", book.bookUrl))
else -> context?.startActivity<ReadBookActivity>(Pair("bookUrl", book.bookUrl)) else -> context?.startActivity<ReadBookActivity>(
Pair("bookUrl", book.bookUrl),
Pair("key", IntentDataHelp.putData(book))
)
} }
} }

Loading…
Cancel
Save