From 07dc68c3f767c022624290079428fe733e25d0f5 Mon Sep 17 00:00:00 2001 From: kunfei Date: Sun, 3 Nov 2019 18:44:00 +0800 Subject: [PATCH] up --- .../legado/app/ui/audio/AudioPlayViewModel.kt | 108 ++++++++++++++++++ .../app/ui/book/read/ReadBookViewModel.kt | 5 +- 2 files changed, 110 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/io/legado/app/ui/audio/AudioPlayViewModel.kt b/app/src/main/java/io/legado/app/ui/audio/AudioPlayViewModel.kt index 4a455ab53..48bfde649 100644 --- a/app/src/main/java/io/legado/app/ui/audio/AudioPlayViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/audio/AudioPlayViewModel.kt @@ -9,9 +9,11 @@ import io.legado.app.base.BaseViewModel import io.legado.app.constant.BookType import io.legado.app.data.entities.Book import io.legado.app.data.entities.BookChapter +import io.legado.app.help.BookHelp import io.legado.app.model.WebBook import io.legado.app.ui.book.read.ReadBookViewModel import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.withContext class AudioPlayViewModel(application: Application) : BaseViewModel(application) { var inBookshelf = false @@ -22,6 +24,7 @@ class AudioPlayViewModel(application: Application) : BaseViewModel(application) var durPageIndex = 0 var isLocalBook = true var webBook: WebBook? = null + private val loadingChapters = arrayListOf() fun initData(intent: Intent) { execute { @@ -95,6 +98,111 @@ class AudioPlayViewModel(application: Application) : BaseViewModel(application) } } + fun loadContent(book: Book, index: Int) { + 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) { + 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) + } + } ?: removeLoading(index) + }.onError { + removeLoading(index) + } + } + } + + private fun download(book: Book, chapter: BookChapter) { + webBook?.getContent(book, chapter, scope = this) + ?.onSuccess(Dispatchers.IO) { content -> + if (content.isNullOrEmpty()) { + contentLoadFinish(chapter, context.getString(R.string.content_empty)) + removeLoading(chapter.index) + } else { + BookHelp.saveContent(book, chapter, content) + contentLoadFinish(chapter, content) + removeLoading(chapter.index) + } + }?.onError { + contentLoadFinish(chapter, it.localizedMessage) + removeLoading(chapter.index) + } + } + + private fun addLoading(index: Int): Boolean { + synchronized(this) { + if (loadingChapters.contains(index)) return false + loadingChapters.add(index) + return true + } + } + + private fun removeLoading(index: Int) { + synchronized(this) { + loadingChapters.remove(index) + } + } + + private fun contentLoadFinish(chapter: BookChapter, content: String) { + if (chapter.index == durChapterIndex) { + callBack?.contentLoadFinish(chapter, content) + } + } + + fun changeTo(book: Book) { + execute { + bookData.value?.let { + App.db.bookDao().delete(it.bookUrl) + } + withContext(Dispatchers.Main) { + callBack?.upContent() + } + App.db.bookDao().insert(book) + bookData.postValue(book) + App.db.bookSourceDao().getBookSource(book.origin)?.let { + webBook = WebBook(it) + } + if (book.tocUrl.isEmpty()) { + loadBookInfo(book) { upChangeDurChapterIndex(book, it) } + } else { + loadChapterList(book) { upChangeDurChapterIndex(book, it) } + } + } + } + + private fun upChangeDurChapterIndex(book: Book, chapters: List) { + execute { + durChapterIndex = BookHelp.getDurChapterIndexByChapterTitle( + book.durChapterTitle, + book.durChapterIndex, + chapters + ) + book.durChapterIndex = durChapterIndex + book.durChapterTitle = chapters[durChapterIndex].title + App.db.bookDao().update(book) + App.db.bookChapterDao().insert(*chapters.toTypedArray()) + chapterSize = chapters.size + callBack?.loadContent() + } + } + fun saveRead(book: Book? = bookData.value) { execute { book?.let { book -> diff --git a/app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt b/app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt index 3403a741a..d12e89deb 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt @@ -33,7 +33,6 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) { var nextTextChapter: TextChapter? = null var webBook: WebBook? = null private val loadingChapters = arrayListOf() - private val loadingLock = "loadingLock" fun initData(intent: Intent) { execute { @@ -208,7 +207,7 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) { } private fun addLoading(index: Int): Boolean { - synchronized(loadingLock) { + synchronized(this) { if (loadingChapters.contains(index)) return false loadingChapters.add(index) return true @@ -216,7 +215,7 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) { } private fun removeLoading(index: Int) { - synchronized(loadingLock) { + synchronized(this) { loadingChapters.remove(index) } }