pull/32/head
kunfei 5 years ago
parent 9b93cfade5
commit 7ced66282e
  1. 82
      app/src/main/java/io/legado/app/ui/readbook/ReadBookViewModel.kt

@ -136,48 +136,32 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
} }
fun loadContent(book: Book, index: Int) { fun loadContent(book: Book, index: Int) {
synchronized(loadingLock) { if (addLoading(index)) {
if (loadingChapters.contains(index)) return execute {
loadingChapters.add(index) App.db.bookChapterDao().getChapter(book.bookUrl, index)?.let { chapter ->
} BookHelp.getContent(book, chapter)?.let {
execute { contentLoadFinish(chapter, it)
App.db.bookChapterDao().getChapter(book.bookUrl, index)?.let { chapter -> removeLoading(chapter.index)
BookHelp.getContent(book, chapter)?.let { } ?: download(book, chapter)
contentLoadFinish(chapter, it) } ?: removeLoading(index)
synchronized(loadingLock) { }.onError {
loadingChapters.remove(index) removeLoading(index)
}
} ?: download(book, chapter)
} ?: synchronized(loadingLock) {
loadingChapters.remove(index)
}
}.onError {
synchronized(loadingLock) {
loadingChapters.remove(index)
} }
} }
} }
private fun download(book: Book, index: Int) { private fun download(book: Book, index: Int) {
synchronized(loadingLock) { if (addLoading(index)) {
if (loadingChapters.contains(index)) return execute {
loadingChapters.add(index) App.db.bookChapterDao().getChapter(book.bookUrl, index)?.let { chapter ->
} if (BookHelp.hasContent(book, chapter)) {
execute { removeLoading(chapter.index)
App.db.bookChapterDao().getChapter(book.bookUrl, index)?.let { chapter -> } else {
if (!BookHelp.hasContent(book, chapter)) { download(book, chapter)
download(book, chapter)
} else {
synchronized(loadingLock) {
loadingChapters.remove(index)
} }
} } ?: removeLoading(index)
} ?: synchronized(loadingLock) { }.onError {
loadingChapters.remove(index) removeLoading(index)
}
}.onError {
synchronized(loadingLock) {
loadingChapters.remove(index)
} }
} }
} }
@ -187,24 +171,32 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
?.onSuccess(IO) { content -> ?.onSuccess(IO) { content ->
if (content.isNullOrEmpty()) { if (content.isNullOrEmpty()) {
contentLoadFinish(chapter, context.getString(R.string.content_empty)) contentLoadFinish(chapter, context.getString(R.string.content_empty))
synchronized(loadingLock) { removeLoading(chapter.index)
loadingChapters.remove(chapter.index)
}
} else { } else {
BookHelp.saveContent(book, chapter, content) BookHelp.saveContent(book, chapter, content)
contentLoadFinish(chapter, content) contentLoadFinish(chapter, content)
synchronized(loadingLock) { removeLoading(chapter.index)
loadingChapters.remove(chapter.index)
}
} }
}?.onError { }?.onError {
contentLoadFinish(chapter, it.localizedMessage) contentLoadFinish(chapter, it.localizedMessage)
synchronized(loadingLock) { removeLoading(chapter.index)
loadingChapters.remove(chapter.index)
}
} }
} }
private fun addLoading(index: Int): Boolean {
synchronized(loadingLock) {
if (loadingChapters.contains(index)) return false
loadingChapters.add(index)
return true
}
}
private fun removeLoading(index: Int) {
synchronized(loadingLock) {
loadingChapters.remove(index)
}
}
private fun contentLoadFinish(chapter: BookChapter, content: String) { private fun contentLoadFinish(chapter: BookChapter, content: String) {
if (chapter.index in durChapterIndex - 1..durChapterIndex + 1) { if (chapter.index in durChapterIndex - 1..durChapterIndex + 1) {
callBack?.contentLoadFinish(chapter, content) callBack?.contentLoadFinish(chapter, content)

Loading…
Cancel
Save