pull/32/head
kunfei 5 years ago
parent 9b93cfade5
commit 7ced66282e
  1. 58
      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
loadingChapters.add(index)
}
execute { execute {
App.db.bookChapterDao().getChapter(book.bookUrl, index)?.let { chapter -> App.db.bookChapterDao().getChapter(book.bookUrl, index)?.let { chapter ->
BookHelp.getContent(book, chapter)?.let { BookHelp.getContent(book, chapter)?.let {
contentLoadFinish(chapter, it) contentLoadFinish(chapter, it)
synchronized(loadingLock) { removeLoading(chapter.index)
loadingChapters.remove(index)
}
} ?: download(book, chapter) } ?: download(book, chapter)
} ?: synchronized(loadingLock) { } ?: removeLoading(index)
loadingChapters.remove(index)
}
}.onError { }.onError {
synchronized(loadingLock) { removeLoading(index)
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
loadingChapters.add(index)
}
execute { execute {
App.db.bookChapterDao().getChapter(book.bookUrl, index)?.let { chapter -> App.db.bookChapterDao().getChapter(book.bookUrl, index)?.let { chapter ->
if (!BookHelp.hasContent(book, chapter)) { if (BookHelp.hasContent(book, chapter)) {
download(book, chapter) removeLoading(chapter.index)
} else { } else {
synchronized(loadingLock) { download(book, chapter)
loadingChapters.remove(index)
}
}
} ?: synchronized(loadingLock) {
loadingChapters.remove(index)
} }
} ?: removeLoading(index)
}.onError { }.onError {
synchronized(loadingLock) { removeLoading(index)
loadingChapters.remove(index)
} }
} }
} }
@ -187,22 +171,30 @@ 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)
removeLoading(chapter.index)
}
}
private fun addLoading(index: Int): Boolean {
synchronized(loadingLock) { synchronized(loadingLock) {
loadingChapters.remove(chapter.index) 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) {

Loading…
Cancel
Save