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

View File

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