pull/84/head
kunfei 5 years ago
parent 2f3e74c66f
commit 46d9acbf03
  1. 19
      app/src/main/java/io/legado/app/ui/book/info/BookInfoViewModel.kt

@ -11,6 +11,7 @@ import io.legado.app.data.entities.BookChapter
import io.legado.app.data.entities.BookGroup import io.legado.app.data.entities.BookGroup
import io.legado.app.help.BookHelp import io.legado.app.help.BookHelp
import io.legado.app.model.WebBook import io.legado.app.model.WebBook
import io.legado.app.model.localBook.AnalyzeTxtFile
import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.Dispatchers.IO
class BookInfoViewModel(application: Application) : BaseViewModel(application) { class BookInfoViewModel(application: Application) : BaseViewModel(application) {
@ -55,8 +56,10 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) {
book: Book, book: Book,
changeDruChapterIndex: ((chapters: List<BookChapter>) -> Unit)? = null changeDruChapterIndex: ((chapters: List<BookChapter>) -> Unit)? = null
) { ) {
if (book.isLocalBook()) return
execute { execute {
if (book.isLocalBook()) {
loadChapter(book, changeDruChapterIndex)
} else {
isLoadingData.postValue(true) isLoadingData.postValue(true)
App.db.bookSourceDao().getBookSource(book.origin)?.let { bookSource -> App.db.bookSourceDao().getBookSource(book.origin)?.let { bookSource ->
WebBook(bookSource).getBookInfo(book, this) WebBook(bookSource).getBookInfo(book, this)
@ -78,6 +81,7 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) {
} }
} }
} }
}
private fun loadChapter( private fun loadChapter(
book: Book, book: Book,
@ -85,6 +89,18 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) {
) { ) {
execute { execute {
isLoadingData.postValue(true) isLoadingData.postValue(true)
if (book.isLocalBook()) {
AnalyzeTxtFile.analyze(context, book).let {
App.db.bookDao().update(book)
App.db.bookChapterDao().insert(*it.toTypedArray())
if (changeDruChapterIndex == null) {
chapterListData.postValue(it)
isLoadingData.postValue(false)
} else {
changeDruChapterIndex(it)
}
}
} else {
App.db.bookSourceDao().getBookSource(book.origin)?.let { bookSource -> App.db.bookSourceDao().getBookSource(book.origin)?.let { bookSource ->
WebBook(bookSource).getChapterList(book, this) WebBook(bookSource).getChapterList(book, this)
.onSuccess(IO) { .onSuccess(IO) {
@ -115,6 +131,7 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) {
} }
} }
} }
}
fun changeTo(book: Book) { fun changeTo(book: Book) {
execute { execute {

Loading…
Cancel
Save