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. 20
      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. 34
      app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt

@ -1,5 +1,7 @@
package io.legado.app.ui.audio
import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.widget.SeekBar
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.ImageLoader
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.observeEvent
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.jetbrains.anko.sdk27.listeners.onClick
import org.jetbrains.anko.sdk27.listeners.onLongClick
import org.jetbrains.anko.startActivityForResult
class AudioPlayActivity : VMBaseActivity<AudioPlayViewModel>(R.layout.activity_audio_play),
AudioPlayViewModel.CallBack {
@ -25,6 +29,7 @@ class AudioPlayActivity : VMBaseActivity<AudioPlayViewModel>(R.layout.activity_a
override val viewModel: AudioPlayViewModel
get() = getViewModel(AudioPlayViewModel::class.java)
private var requestCodeChapter = 8461
private var adjustProgress = false
private var status = Status.STOP
@ -65,6 +70,14 @@ class AudioPlayActivity : VMBaseActivity<AudioPlayViewModel>(R.layout.activity_a
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) {
@ -85,9 +98,7 @@ class AudioPlayActivity : VMBaseActivity<AudioPlayViewModel>(R.layout.activity_a
when (status) {
Status.PLAY -> AudioPlay.pause(this)
Status.PAUSE -> AudioPlay.resume(this)
else -> viewModel.book?.let {
viewModel.loadContent(it, viewModel.durChapterIndex)
}
else -> viewModel.loadContent(viewModel.durChapterIndex)
}
}
@ -99,9 +110,7 @@ class AudioPlayActivity : VMBaseActivity<AudioPlayViewModel>(R.layout.activity_a
content,
viewModel.durPageIndex
)
viewModel.book?.let {
viewModel.loadContent(it, viewModel.durChapterIndex + 1)
}
viewModel.loadContent(viewModel.durChapterIndex + 1)
}
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")
}
}
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,22 +98,25 @@ class AudioPlayViewModel(application: Application) : BaseViewModel(application)
}
}
fun loadContent(book: Book, index: Int) {
fun loadContent(index: Int) {
book?.let { book ->
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)
} ?: download(chapter)
} ?: removeLoading(index)
}.onError {
removeLoading(index)
}
}
}
}
private fun download(book: Book, chapter: BookChapter) {
private fun download(chapter: BookChapter) {
book?.let { book ->
webBook?.getContent(book, chapter, scope = this)
?.onSuccess(Dispatchers.IO) { content ->
if (content.isNullOrEmpty()) {
@ -129,6 +132,7 @@ class AudioPlayViewModel(application: Application) : BaseViewModel(application)
removeLoading(chapter.index)
}
}
}
private fun addLoading(index: Int): Boolean {
synchronized(this) {
@ -191,20 +195,16 @@ class AudioPlayViewModel(application: Application) : BaseViewModel(application)
durChapterIndex--
book?.durChapterIndex = durChapterIndex
saveRead()
book?.let {
loadContent(it, durChapterIndex)
}
loadContent(durChapterIndex)
}
}
fun moveToNext() {
if (durChapterIndex < chapterSize - 1) {
durChapterIndex++
book?.let {
it.durChapterIndex = durChapterIndex
book?.durChapterIndex = durChapterIndex
saveRead()
loadContent(it, durChapterIndex)
}
loadContent(durChapterIndex)
} else {
AudioPlay.stop(context)
}

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

@ -121,17 +121,15 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
nextTextChapter = null
book?.let {
if (curTextChapter == null) {
loadContent(it, durChapterIndex)
loadContent(durChapterIndex)
} else if (upContent) {
callBack?.upContent()
}
loadContent(it, durChapterIndex.plus(1))
loadContent(durChapterIndex.plus(1))
launch(IO) {
for (i in 2..10) {
delay(100)
book?.let { book ->
download(book, durChapterIndex + i)
}
download(durChapterIndex + i)
}
}
}
@ -144,45 +142,46 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
prevTextChapter = null
book?.let {
if (curTextChapter == null) {
loadContent(it, durChapterIndex)
loadContent(durChapterIndex)
} else if (upContent) {
callBack?.upContent()
}
loadContent(it, durChapterIndex.minus(1))
loadContent(durChapterIndex.minus(1))
launch(IO) {
for (i in -5..-2) {
delay(100)
book?.let { book ->
download(book, durChapterIndex + i)
}
download(durChapterIndex + i)
}
}
}
}
fun loadContent(book: Book, index: Int) {
fun loadContent(index: Int) {
book?.let { book ->
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)
} ?: download(chapter)
} ?: removeLoading(index)
}.onError {
removeLoading(index)
}
}
}
}
private fun download(book: Book, index: Int) {
private fun download(index: Int) {
book?.let { book ->
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)
download(chapter)
}
} ?: removeLoading(index)
}.onError {
@ -190,8 +189,10 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
}
}
}
}
private fun download(book: Book, chapter: BookChapter) {
private fun download(chapter: BookChapter) {
book?.let { book ->
webBook?.getContent(book, chapter, scope = this)
?.onSuccess(IO) { content ->
if (content.isNullOrEmpty()) {
@ -207,6 +208,7 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
removeLoading(chapter.index)
}
}
}
private fun addLoading(index: Int): Boolean {
synchronized(this) {
@ -332,7 +334,7 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
execute {
App.db.bookChapterDao().getChapter(book.bookUrl, durChapterIndex)?.let { chapter ->
BookHelp.delContent(book, chapter)
loadContent(book, durChapterIndex)
loadContent(durChapterIndex)
}
}
}

Loading…
Cancel
Save