|
|
|
@ -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) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |