pull/46/head
kunfei 5 years ago
parent 39dd84369c
commit 07dc68c3f7
  1. 108
      app/src/main/java/io/legado/app/ui/audio/AudioPlayViewModel.kt
  2. 5
      app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt

@ -9,9 +9,11 @@ import io.legado.app.base.BaseViewModel
import io.legado.app.constant.BookType import io.legado.app.constant.BookType
import io.legado.app.data.entities.Book import io.legado.app.data.entities.Book
import io.legado.app.data.entities.BookChapter import io.legado.app.data.entities.BookChapter
import io.legado.app.help.BookHelp
import io.legado.app.model.WebBook import io.legado.app.model.WebBook
import io.legado.app.ui.book.read.ReadBookViewModel import io.legado.app.ui.book.read.ReadBookViewModel
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
class AudioPlayViewModel(application: Application) : BaseViewModel(application) { class AudioPlayViewModel(application: Application) : BaseViewModel(application) {
var inBookshelf = false var inBookshelf = false
@ -22,6 +24,7 @@ class AudioPlayViewModel(application: Application) : BaseViewModel(application)
var durPageIndex = 0 var durPageIndex = 0
var isLocalBook = true var isLocalBook = true
var webBook: WebBook? = null var webBook: WebBook? = null
private val loadingChapters = arrayListOf<Int>()
fun initData(intent: Intent) { fun initData(intent: Intent) {
execute { 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<BookChapter>) {
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) { fun saveRead(book: Book? = bookData.value) {
execute { execute {
book?.let { book -> book?.let { book ->

@ -33,7 +33,6 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
var nextTextChapter: TextChapter? = null var nextTextChapter: TextChapter? = null
var webBook: WebBook? = null var webBook: WebBook? = null
private val loadingChapters = arrayListOf<Int>() private val loadingChapters = arrayListOf<Int>()
private val loadingLock = "loadingLock"
fun initData(intent: Intent) { fun initData(intent: Intent) {
execute { execute {
@ -208,7 +207,7 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
} }
private fun addLoading(index: Int): Boolean { private fun addLoading(index: Int): Boolean {
synchronized(loadingLock) { synchronized(this) {
if (loadingChapters.contains(index)) return false if (loadingChapters.contains(index)) return false
loadingChapters.add(index) loadingChapters.add(index)
return true return true
@ -216,7 +215,7 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
} }
private fun removeLoading(index: Int) { private fun removeLoading(index: Int) {
synchronized(loadingLock) { synchronized(this) {
loadingChapters.remove(index) loadingChapters.remove(index)
} }
} }

Loading…
Cancel
Save