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.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<BookInfoViewModel>(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<BookInfoViewModel>(R.layout.activity_boo
if (!viewModel.inBookshelf) {
viewModel.saveBook {
viewModel.saveChapterList {
startActivity<ReadBookActivity>(
Pair("bookUrl", book.bookUrl),
Pair("inBookshelf", false)
)
startReadActivity(book)
}
}
} else {
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?
get() = viewModel.bookData.value?.origin

@ -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<Book>(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()) {

@ -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<Book>(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)
}
}

@ -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<BooksViewModel>(R.layout.fragment_books),
when (book.type) {
BookType.audio ->
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