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