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 io.legado.app.App
import io.legado.app.R
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
@ -34,6 +35,20 @@ object ReadBook {
var webBook: WebBook? = null
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 {
if (durChapterIndex < chapterSize - 1) {
@ -138,7 +153,7 @@ object ReadBook {
}
}
fun download(index: Int) {
private fun download(index: Int) {
book?.let { book ->
if (addLoading(index)) {
Coroutine.async {
@ -156,7 +171,7 @@ object ReadBook {
}
}
fun download(chapter: BookChapter) {
private fun download(chapter: BookChapter) {
book?.let { book ->
webBook?.getContent(book, chapter)
?.onSuccess(Dispatchers.IO) { content ->

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