|
|
@ -7,13 +7,16 @@ import io.legado.app.App |
|
|
|
import io.legado.app.base.BaseViewModel |
|
|
|
import io.legado.app.base.BaseViewModel |
|
|
|
import io.legado.app.data.entities.Book |
|
|
|
import io.legado.app.data.entities.Book |
|
|
|
import io.legado.app.data.entities.BookSource |
|
|
|
import io.legado.app.data.entities.BookSource |
|
|
|
|
|
|
|
import io.legado.app.help.BookHelp |
|
|
|
import io.legado.app.model.WebBook |
|
|
|
import io.legado.app.model.WebBook |
|
|
|
|
|
|
|
import kotlinx.coroutines.Dispatchers.IO |
|
|
|
|
|
|
|
|
|
|
|
class ReadBookViewModel(application: Application) : BaseViewModel(application) { |
|
|
|
class ReadBookViewModel(application: Application) : BaseViewModel(application) { |
|
|
|
|
|
|
|
|
|
|
|
var book: Book? = null |
|
|
|
var book: Book? = null |
|
|
|
var bookSource: BookSource? = null |
|
|
|
var bookSource: BookSource? = null |
|
|
|
var chapterMaxIndex = MediatorLiveData<Int>() |
|
|
|
var chapterMaxIndex = MediatorLiveData<Int>() |
|
|
|
|
|
|
|
var webBook: WebBook? = null |
|
|
|
|
|
|
|
|
|
|
|
fun initData(intent: Intent) { |
|
|
|
fun initData(intent: Intent) { |
|
|
|
val bookUrl = intent.getStringExtra("bookUrl") |
|
|
|
val bookUrl = intent.getStringExtra("bookUrl") |
|
|
@ -21,20 +24,20 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) { |
|
|
|
execute { |
|
|
|
execute { |
|
|
|
book = App.db.bookDao().getBook(bookUrl) |
|
|
|
book = App.db.bookDao().getBook(bookUrl) |
|
|
|
book?.let { book -> |
|
|
|
book?.let { book -> |
|
|
|
val count = App.db.bookChapterDao().getChapterCount(bookUrl) |
|
|
|
bookSource = App.db.bookSourceDao().getBookSource(book.origin) |
|
|
|
if (count == 0) { |
|
|
|
bookSource?.let { |
|
|
|
App.db.bookSourceDao().getBookSource(book.origin)?.let { |
|
|
|
webBook = WebBook(it) |
|
|
|
WebBook(it).getChapterList(book) |
|
|
|
val count = App.db.bookChapterDao().getChapterCount(bookUrl) |
|
|
|
.onSuccess { cList -> |
|
|
|
if (count == 0) { |
|
|
|
|
|
|
|
webBook?.getChapterList(book) |
|
|
|
|
|
|
|
?.onSuccess { cList -> |
|
|
|
cList?.let { |
|
|
|
cList?.let { |
|
|
|
App.db.bookChapterDao().insert(*cList.toTypedArray()) |
|
|
|
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) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |