pull/47/head
kunfei 5 years ago
parent 071e7c4248
commit 973a5ea163
  1. 34
      app/src/main/java/io/legado/app/ui/audio/AudioPlayActivity.kt
  2. 64
      app/src/main/java/io/legado/app/ui/audio/AudioPlayViewModel.kt
  3. 12
      app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt
  4. 96
      app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt

@ -1,5 +1,7 @@
package io.legado.app.ui.audio package io.legado.app.ui.audio
import android.app.Activity
import android.content.Intent
import android.os.Bundle import android.os.Bundle
import android.widget.SeekBar import android.widget.SeekBar
import androidx.lifecycle.Observer import androidx.lifecycle.Observer
@ -11,6 +13,7 @@ import io.legado.app.data.entities.BookChapter
import io.legado.app.help.BlurTransformation import io.legado.app.help.BlurTransformation
import io.legado.app.help.ImageLoader import io.legado.app.help.ImageLoader
import io.legado.app.service.help.AudioPlay import io.legado.app.service.help.AudioPlay
import io.legado.app.ui.chapterlist.ChapterListActivity
import io.legado.app.utils.getViewModel import io.legado.app.utils.getViewModel
import io.legado.app.utils.observeEvent import io.legado.app.utils.observeEvent
import kotlinx.android.synthetic.main.activity_audio_play.* import kotlinx.android.synthetic.main.activity_audio_play.*
@ -18,6 +21,7 @@ import kotlinx.android.synthetic.main.view_title_bar.*
import org.apache.commons.lang3.time.DateFormatUtils import org.apache.commons.lang3.time.DateFormatUtils
import org.jetbrains.anko.sdk27.listeners.onClick import org.jetbrains.anko.sdk27.listeners.onClick
import org.jetbrains.anko.sdk27.listeners.onLongClick import org.jetbrains.anko.sdk27.listeners.onLongClick
import org.jetbrains.anko.startActivityForResult
class AudioPlayActivity : VMBaseActivity<AudioPlayViewModel>(R.layout.activity_audio_play), class AudioPlayActivity : VMBaseActivity<AudioPlayViewModel>(R.layout.activity_audio_play),
AudioPlayViewModel.CallBack { AudioPlayViewModel.CallBack {
@ -25,6 +29,7 @@ class AudioPlayActivity : VMBaseActivity<AudioPlayViewModel>(R.layout.activity_a
override val viewModel: AudioPlayViewModel override val viewModel: AudioPlayViewModel
get() = getViewModel(AudioPlayViewModel::class.java) get() = getViewModel(AudioPlayViewModel::class.java)
private var requestCodeChapter = 8461
private var adjustProgress = false private var adjustProgress = false
private var status = Status.STOP private var status = Status.STOP
@ -65,6 +70,14 @@ class AudioPlayActivity : VMBaseActivity<AudioPlayViewModel>(R.layout.activity_a
AudioPlay.adjustProgress(this@AudioPlayActivity, player_progress.progress) AudioPlay.adjustProgress(this@AudioPlayActivity, player_progress.progress)
} }
}) })
iv_chapter.onClick {
viewModel.book?.let {
startActivityForResult<ChapterListActivity>(
requestCodeChapter,
Pair("bookUrl", it.bookUrl)
)
}
}
} }
private fun upCover(path: String) { private fun upCover(path: String) {
@ -85,9 +98,7 @@ class AudioPlayActivity : VMBaseActivity<AudioPlayViewModel>(R.layout.activity_a
when (status) { when (status) {
Status.PLAY -> AudioPlay.pause(this) Status.PLAY -> AudioPlay.pause(this)
Status.PAUSE -> AudioPlay.resume(this) Status.PAUSE -> AudioPlay.resume(this)
else -> viewModel.book?.let { else -> viewModel.loadContent(viewModel.durChapterIndex)
viewModel.loadContent(it, viewModel.durChapterIndex)
}
} }
} }
@ -99,9 +110,7 @@ class AudioPlayActivity : VMBaseActivity<AudioPlayViewModel>(R.layout.activity_a
content, content,
viewModel.durPageIndex viewModel.durPageIndex
) )
viewModel.book?.let { viewModel.loadContent(viewModel.durChapterIndex + 1)
viewModel.loadContent(it, viewModel.durChapterIndex + 1)
}
} }
override fun observeLiveBus() { override fun observeLiveBus() {
@ -128,4 +137,17 @@ class AudioPlayActivity : VMBaseActivity<AudioPlayViewModel>(R.layout.activity_a
tv_all_time.text = DateFormatUtils.format(it.toLong(), "mm:ss") tv_all_time.text = DateFormatUtils.format(it.toLong(), "mm:ss")
} }
} }
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (resultCode == Activity.RESULT_OK) {
when (requestCode) {
requestCodeChapter -> data?.getIntExtra("index", viewModel.durChapterIndex)?.let {
if (it != viewModel.durChapterIndex) {
viewModel.loadContent(it)
}
}
}
}
}
} }

@ -98,36 +98,40 @@ class AudioPlayViewModel(application: Application) : BaseViewModel(application)
} }
} }
fun loadContent(book: Book, index: Int) { fun loadContent(index: Int) {
if (addLoading(index)) { book?.let { book ->
execute { if (addLoading(index)) {
App.db.bookChapterDao().getChapter(book.bookUrl, index)?.let { chapter -> execute {
BookHelp.getContent(book, chapter)?.let { App.db.bookChapterDao().getChapter(book.bookUrl, index)?.let { chapter ->
contentLoadFinish(chapter, it) BookHelp.getContent(book, chapter)?.let {
removeLoading(chapter.index) contentLoadFinish(chapter, it)
} ?: download(book, chapter) removeLoading(chapter.index)
} ?: removeLoading(index) } ?: download(chapter)
}.onError { } ?: removeLoading(index)
removeLoading(index) }.onError {
removeLoading(index)
}
} }
} }
} }
private fun download(book: Book, chapter: BookChapter) { private fun download(chapter: BookChapter) {
webBook?.getContent(book, chapter, scope = this) book?.let { book ->
?.onSuccess(Dispatchers.IO) { content -> webBook?.getContent(book, chapter, scope = this)
if (content.isNullOrEmpty()) { ?.onSuccess(Dispatchers.IO) { content ->
contentLoadFinish(chapter, context.getString(R.string.content_empty)) if (content.isNullOrEmpty()) {
removeLoading(chapter.index) contentLoadFinish(chapter, context.getString(R.string.content_empty))
} else { removeLoading(chapter.index)
BookHelp.saveContent(book, chapter, content) } else {
contentLoadFinish(chapter, content) BookHelp.saveContent(book, chapter, content)
contentLoadFinish(chapter, content)
removeLoading(chapter.index)
}
}?.onError {
contentLoadFinish(chapter, it.localizedMessage)
removeLoading(chapter.index) removeLoading(chapter.index)
} }
}?.onError { }
contentLoadFinish(chapter, it.localizedMessage)
removeLoading(chapter.index)
}
} }
private fun addLoading(index: Int): Boolean { private fun addLoading(index: Int): Boolean {
@ -191,20 +195,16 @@ class AudioPlayViewModel(application: Application) : BaseViewModel(application)
durChapterIndex-- durChapterIndex--
book?.durChapterIndex = durChapterIndex book?.durChapterIndex = durChapterIndex
saveRead() saveRead()
book?.let { loadContent(durChapterIndex)
loadContent(it, durChapterIndex)
}
} }
} }
fun moveToNext() { fun moveToNext() {
if (durChapterIndex < chapterSize - 1) { if (durChapterIndex < chapterSize - 1) {
durChapterIndex++ durChapterIndex++
book?.let { book?.durChapterIndex = durChapterIndex
it.durChapterIndex = durChapterIndex saveRead()
saveRead() loadContent(durChapterIndex)
loadContent(it, durChapterIndex)
}
} else { } else {
AudioPlay.stop(context) AudioPlay.stop(context)
} }

@ -255,20 +255,16 @@ class ReadBookActivity : VMBaseActivity<ReadBookViewModel>(R.layout.activity_boo
* 加载章节内容 * 加载章节内容
*/ */
override fun loadContent() { override fun loadContent() {
viewModel.book?.let { viewModel.loadContent(viewModel.durChapterIndex)
viewModel.loadContent(it, viewModel.durChapterIndex) viewModel.loadContent(viewModel.durChapterIndex + 1)
viewModel.loadContent(it, viewModel.durChapterIndex + 1) viewModel.loadContent(viewModel.durChapterIndex - 1)
viewModel.loadContent(it, viewModel.durChapterIndex - 1)
}
} }
/** /**
* 加载章节内容, index章节序号 * 加载章节内容, index章节序号
*/ */
override fun loadContent(index: Int) { override fun loadContent(index: Int) {
viewModel.book?.let { viewModel.loadContent(index)
viewModel.loadContent(it, index)
}
} }
/** /**

@ -121,17 +121,15 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
nextTextChapter = null nextTextChapter = null
book?.let { book?.let {
if (curTextChapter == null) { if (curTextChapter == null) {
loadContent(it, durChapterIndex) loadContent(durChapterIndex)
} else if (upContent) { } else if (upContent) {
callBack?.upContent() callBack?.upContent()
} }
loadContent(it, durChapterIndex.plus(1)) loadContent(durChapterIndex.plus(1))
launch(IO) { launch(IO) {
for (i in 2..10) { for (i in 2..10) {
delay(100) delay(100)
book?.let { book -> download(durChapterIndex + i)
download(book, durChapterIndex + i)
}
} }
} }
} }
@ -144,68 +142,72 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
prevTextChapter = null prevTextChapter = null
book?.let { book?.let {
if (curTextChapter == null) { if (curTextChapter == null) {
loadContent(it, durChapterIndex) loadContent(durChapterIndex)
} else if (upContent) { } else if (upContent) {
callBack?.upContent() callBack?.upContent()
} }
loadContent(it, durChapterIndex.minus(1)) loadContent(durChapterIndex.minus(1))
launch(IO) { launch(IO) {
for (i in -5..-2) { for (i in -5..-2) {
delay(100) delay(100)
book?.let { book -> download(durChapterIndex + i)
download(book, durChapterIndex + i)
}
} }
} }
} }
} }
fun loadContent(book: Book, index: Int) { fun loadContent(index: Int) {
if (addLoading(index)) { book?.let { book ->
execute { if (addLoading(index)) {
App.db.bookChapterDao().getChapter(book.bookUrl, index)?.let { chapter -> execute {
BookHelp.getContent(book, chapter)?.let { App.db.bookChapterDao().getChapter(book.bookUrl, index)?.let { chapter ->
contentLoadFinish(chapter, it) BookHelp.getContent(book, chapter)?.let {
removeLoading(chapter.index) contentLoadFinish(chapter, it)
} ?: download(book, chapter) removeLoading(chapter.index)
} ?: removeLoading(index) } ?: download(chapter)
}.onError { } ?: removeLoading(index)
removeLoading(index) }.onError {
removeLoading(index)
}
} }
} }
} }
private fun download(book: Book, index: Int) { private fun download(index: Int) {
if (addLoading(index)) { book?.let { book ->
execute { if (addLoading(index)) {
App.db.bookChapterDao().getChapter(book.bookUrl, index)?.let { chapter -> execute {
if (BookHelp.hasContent(book, chapter)) { App.db.bookChapterDao().getChapter(book.bookUrl, index)?.let { chapter ->
removeLoading(chapter.index) if (BookHelp.hasContent(book, chapter)) {
} else { removeLoading(chapter.index)
download(book, chapter) } else {
} download(chapter)
} ?: removeLoading(index) }
}.onError { } ?: removeLoading(index)
removeLoading(index) }.onError {
removeLoading(index)
}
} }
} }
} }
private fun download(book: Book, chapter: BookChapter) { private fun download(chapter: BookChapter) {
webBook?.getContent(book, chapter, scope = this) book?.let { book ->
?.onSuccess(IO) { content -> webBook?.getContent(book, chapter, scope = this)
if (content.isNullOrEmpty()) { ?.onSuccess(IO) { content ->
contentLoadFinish(chapter, context.getString(R.string.content_empty)) if (content.isNullOrEmpty()) {
removeLoading(chapter.index) contentLoadFinish(chapter, context.getString(R.string.content_empty))
} else { removeLoading(chapter.index)
BookHelp.saveContent(book, chapter, content) } else {
contentLoadFinish(chapter, content) BookHelp.saveContent(book, chapter, content)
contentLoadFinish(chapter, content)
removeLoading(chapter.index)
}
}?.onError {
contentLoadFinish(chapter, it.localizedMessage)
removeLoading(chapter.index) removeLoading(chapter.index)
} }
}?.onError { }
contentLoadFinish(chapter, it.localizedMessage)
removeLoading(chapter.index)
}
} }
private fun addLoading(index: Int): Boolean { private fun addLoading(index: Int): Boolean {
@ -332,7 +334,7 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
execute { execute {
App.db.bookChapterDao().getChapter(book.bookUrl, durChapterIndex)?.let { chapter -> App.db.bookChapterDao().getChapter(book.bookUrl, durChapterIndex)?.let { chapter ->
BookHelp.delContent(book, chapter) BookHelp.delContent(book, chapter)
loadContent(book, durChapterIndex) loadContent(durChapterIndex)
} }
} }
} }

Loading…
Cancel
Save