From 7ced66282e8ec278009143462760666d2bdfad0a Mon Sep 17 00:00:00 2001 From: kunfei Date: Tue, 27 Aug 2019 18:05:21 +0800 Subject: [PATCH] up --- .../app/ui/readbook/ReadBookViewModel.kt | 82 +++++++++---------- 1 file changed, 37 insertions(+), 45 deletions(-) diff --git a/app/src/main/java/io/legado/app/ui/readbook/ReadBookViewModel.kt b/app/src/main/java/io/legado/app/ui/readbook/ReadBookViewModel.kt index b02beea94..9b9893b81 100644 --- a/app/src/main/java/io/legado/app/ui/readbook/ReadBookViewModel.kt +++ b/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) { - synchronized(loadingLock) { - if (loadingChapters.contains(index)) return - loadingChapters.add(index) - } - execute { - App.db.bookChapterDao().getChapter(book.bookUrl, index)?.let { chapter -> - BookHelp.getContent(book, chapter)?.let { - contentLoadFinish(chapter, it) - synchronized(loadingLock) { - loadingChapters.remove(index) - } - } ?: download(book, chapter) - } ?: synchronized(loadingLock) { - loadingChapters.remove(index) - } - }.onError { - synchronized(loadingLock) { - loadingChapters.remove(index) + if (addLoading(index)) { + execute { + App.db.bookChapterDao().getChapter(book.bookUrl, index)?.let { chapter -> + BookHelp.getContent(book, chapter)?.let { + contentLoadFinish(chapter, it) + removeLoading(chapter.index) + } ?: download(book, chapter) + } ?: removeLoading(index) + }.onError { + removeLoading(index) } } } private fun download(book: Book, index: Int) { - synchronized(loadingLock) { - if (loadingChapters.contains(index)) return - loadingChapters.add(index) - } - execute { - App.db.bookChapterDao().getChapter(book.bookUrl, index)?.let { chapter -> - if (!BookHelp.hasContent(book, chapter)) { - download(book, chapter) - } else { - synchronized(loadingLock) { - loadingChapters.remove(index) + if (addLoading(index)) { + execute { + App.db.bookChapterDao().getChapter(book.bookUrl, index)?.let { chapter -> + if (BookHelp.hasContent(book, chapter)) { + removeLoading(chapter.index) + } else { + download(book, chapter) } - } - } ?: synchronized(loadingLock) { - loadingChapters.remove(index) - } - }.onError { - synchronized(loadingLock) { - loadingChapters.remove(index) + } ?: removeLoading(index) + }.onError { + removeLoading(index) } } } @@ -187,24 +171,32 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) { ?.onSuccess(IO) { content -> if (content.isNullOrEmpty()) { contentLoadFinish(chapter, context.getString(R.string.content_empty)) - synchronized(loadingLock) { - loadingChapters.remove(chapter.index) - } + removeLoading(chapter.index) } else { BookHelp.saveContent(book, chapter, content) contentLoadFinish(chapter, content) - synchronized(loadingLock) { - loadingChapters.remove(chapter.index) - } + removeLoading(chapter.index) } }?.onError { contentLoadFinish(chapter, it.localizedMessage) - synchronized(loadingLock) { - loadingChapters.remove(chapter.index) - } + removeLoading(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) { if (chapter.index in durChapterIndex - 1..durChapterIndex + 1) { callBack?.contentLoadFinish(chapter, content)