pull/49/head
kunfei 5 years ago
parent 31061cfbb8
commit ceea9334b6
  1. 19
      app/src/main/java/io/legado/app/service/help/ReadBook.kt
  2. 49
      app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt

@ -3,6 +3,7 @@ package io.legado.app.service.help
import androidx.lifecycle.MutableLiveData import androidx.lifecycle.MutableLiveData
import io.legado.app.App import io.legado.app.App
import io.legado.app.R import io.legado.app.R
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
@ -34,6 +35,20 @@ object ReadBook {
var webBook: WebBook? = null var webBook: WebBook? = null
private val loadingChapters = arrayListOf<Int>() private val loadingChapters = arrayListOf<Int>()
fun resetData(book: Book) {
this.book = book
titleDate.postValue(book.name)
durChapterIndex = book.durChapterIndex
durPageIndex = book.durChapterPos
isLocalBook = book.origin == BookType.local
App.db.bookSourceDao().getBookSource(book.origin)?.let {
webBook = WebBook(it)
}
chapterSize = 0
prevTextChapter = null
curTextChapter = null
nextTextChapter = null
}
fun moveToNextChapter(upContent: Boolean): Boolean { fun moveToNextChapter(upContent: Boolean): Boolean {
if (durChapterIndex < chapterSize - 1) { if (durChapterIndex < chapterSize - 1) {
@ -138,7 +153,7 @@ object ReadBook {
} }
} }
fun download(index: Int) { private fun download(index: Int) {
book?.let { book -> book?.let { book ->
if (addLoading(index)) { if (addLoading(index)) {
Coroutine.async { Coroutine.async {
@ -156,7 +171,7 @@ object ReadBook {
} }
} }
fun download(chapter: BookChapter) { private fun download(chapter: BookChapter) {
book?.let { book -> book?.let { book ->
webBook?.getContent(book, chapter) webBook?.getContent(book, chapter)
?.onSuccess(Dispatchers.IO) { content -> ?.onSuccess(Dispatchers.IO) { content ->

@ -5,7 +5,6 @@ import android.content.Intent
import io.legado.app.App import io.legado.app.App
import io.legado.app.R import io.legado.app.R
import io.legado.app.base.BaseViewModel import io.legado.app.base.BaseViewModel
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
@ -34,30 +33,38 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
} }
private fun initBook(book: Book) { private fun initBook(book: Book) {
ReadBook.book = book if (ReadBook.book?.bookUrl != book.bookUrl) {
ReadBook.titleDate.postValue(book.name) ReadBook.resetData(book)
ReadBook.durChapterIndex = book.durChapterIndex val count = App.db.bookChapterDao().getChapterCount(book.bookUrl)
ReadBook.durPageIndex = book.durChapterPos if (count == 0) {
ReadBook.isLocalBook = book.origin == BookType.local if (book.tocUrl.isEmpty()) {
App.db.bookSourceDao().getBookSource(book.origin)?.let { loadBookInfo(book)
ReadBook.webBook = WebBook(it) } else {
} loadChapterList(book)
val count = App.db.bookChapterDao().getChapterCount(book.bookUrl) }
if (count == 0) {
if (book.tocUrl.isEmpty()) {
loadBookInfo(book)
} else { } else {
loadChapterList(book) if (ReadBook.durChapterIndex > count - 1) {
ReadBook.durChapterIndex = count - 1
}
ReadBook.chapterSize = count
ReadBook.callBack?.loadContent()
}
if (ReadBook.inBookshelf) {
ReadBook.saveRead()
} }
} else { } else {
if (ReadBook.durChapterIndex > count - 1) { ReadBook.titleDate.postValue(book.name)
ReadBook.durChapterIndex = count - 1 if (ReadBook.chapterSize == 0) {
if (book.tocUrl.isEmpty()) {
loadBookInfo(book)
} else {
loadChapterList(book)
}
} else {
if (ReadBook.curTextChapter != null) {
ReadBook.callBack?.upContent()
}
} }
ReadBook.chapterSize = count
ReadBook.callBack?.loadContent()
}
if (ReadBook.inBookshelf) {
ReadBook.saveRead()
} }
} }

Loading…
Cancel
Save