pull/32/head
kunfei 5 years ago
parent 6a3b93ef8a
commit 0d6f013f9b
  1. 6
      app/src/main/java/io/legado/app/help/BookHelp.kt
  2. 32
      app/src/main/java/io/legado/app/ui/readbook/ReadBookViewModel.kt

@ -12,7 +12,11 @@ import java.nio.charset.StandardCharsets
object BookHelp {
var downloadPath = App.INSTANCE.getPrefString("downloadPath") ?: App.INSTANCE.getExternalFilesDir(null)
private var downloadPath = App.INSTANCE.getPrefString("downloadPath") ?: App.INSTANCE.getExternalFilesDir(null)
fun upDownloadPath() {
downloadPath = App.INSTANCE.getPrefString("downloadPath") ?: App.INSTANCE.getExternalFilesDir(null)
}
fun saveContent(book: Book, bookChapter: BookChapter, content: String) {
if (content.isEmpty()) {

@ -7,13 +7,16 @@ import io.legado.app.App
import io.legado.app.base.BaseViewModel
import io.legado.app.data.entities.Book
import io.legado.app.data.entities.BookSource
import io.legado.app.help.BookHelp
import io.legado.app.model.WebBook
import kotlinx.coroutines.Dispatchers.IO
class ReadBookViewModel(application: Application) : BaseViewModel(application) {
var book: Book? = null
var bookSource: BookSource? = null
var chapterMaxIndex = MediatorLiveData<Int>()
var webBook: WebBook? = null
fun initData(intent: Intent) {
val bookUrl = intent.getStringExtra("bookUrl")
@ -21,20 +24,20 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
execute {
book = App.db.bookDao().getBook(bookUrl)
book?.let { book ->
val count = App.db.bookChapterDao().getChapterCount(bookUrl)
if (count == 0) {
App.db.bookSourceDao().getBookSource(book.origin)?.let {
WebBook(it).getChapterList(book)
.onSuccess { cList ->
bookSource = App.db.bookSourceDao().getBookSource(book.origin)
bookSource?.let {
webBook = WebBook(it)
val count = App.db.bookChapterDao().getChapterCount(bookUrl)
if (count == 0) {
webBook?.getChapterList(book)
?.onSuccess { cList ->
cList?.let {
App.db.bookChapterDao().insert(*cList.toTypedArray())
}
}
} ?: let {
} else {
chapterMaxIndex.postValue(count)
}
} else {
chapterMaxIndex.postValue(count)
}
}
@ -42,4 +45,15 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
}
}
fun download(book: Book, index: Int) {
App.db.bookChapterDao().getChapter(book.bookUrl, index)?.let { chapter ->
webBook?.getContent(book, chapter)
?.onSuccess(IO) { content ->
content?.let {
BookHelp.saveContent(book, chapter, it)
}
}
}
}
}
Loading…
Cancel
Save