[skip ci] 优化

This commit is contained in:
Xwite 2023-03-11 09:23:45 +08:00
parent 252073188e
commit 670e3198d9
2 changed files with 14 additions and 8 deletions

View File

@ -74,10 +74,16 @@ class EpubFile(var book: Book) {
private var fileDescriptor: ParcelFileDescriptor? = null
private var epubBook: EpubBook? = null
get() {
if (field != null && fileDescriptor != null) {
return field
if (field == null || fileDescriptor == null) {
field = readEpub()
}
return field
}
private var epubBookContents: List<Resource>?
get() {
if (field == null || fileDescriptor == null) {
field = epubBook.contents
}
field = readEpub()
return field
}
@ -147,7 +153,7 @@ class EpubFile(var book: Book) {
val includeNextChapterResource = !endFragmentId.isNullOrBlank()
/*一些书籍依靠href索引的resource会包含多个章节需要依靠fragmentId来截取到当前章节的内容*/
/*注:这里较大增加了内容加载的时间所以首次获取内容后可存储到本地cache减少重复加载*/
for (res in epubBook.contents) {
for (res in epubBookContents) {
if (!findChapterFirstSource) {
if (currentChapterFirstResourceHref != res.href) continue
findChapterFirstSource = true

View File

@ -155,12 +155,12 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) {
appDb.bookDao.getBook(book.name, book.author)?.let {
inBookshelf = true
}
bookData.postValue(book)
bookData.postValue(it)
if (inBookshelf) {
appDb.bookDao.update(book)
appDb.bookDao.update(it)
}
if (it.isWebFile) {
loadWebFile(book, bookSource, scope)
loadWebFile(it, scope)
} else {
loadChapter(it, scope)
}
@ -234,7 +234,6 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) {
private fun loadWebFile(
book: Book,
bookSource: BookSource,
scope: CoroutineScope = viewModelScope
) {
execute(scope) {
@ -263,6 +262,7 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) {
LocalBook.saveBookFile(webFile.url, webFile.name, bookSource)
}
}.onSuccess {
@Suppress("unchecked_cast")
success?.invoke(it as T)
}.onError {
context.toastOnUi("ImportWebFileError\n${it.localizedMessage}")