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
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,36 +98,40 @@ 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)
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(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)
private fun download(chapter: BookChapter) {
book?.let { book ->
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)
}
}?.onError {
contentLoadFinish(chapter, it.localizedMessage)
removeLoading(chapter.index)
}
}
}
private fun addLoading(index: Int): Boolean {
@ -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
saveRead()
loadContent(it, durChapterIndex)
}
book?.durChapterIndex = durChapterIndex
saveRead()
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,68 +142,72 @@ 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) {
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)
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(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(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(chapter)
}
} ?: removeLoading(index)
}.onError {
removeLoading(index)
}
}
}
}
private fun download(book: Book, chapter: BookChapter) {
webBook?.getContent(book, chapter, scope = this)
?.onSuccess(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)
private fun download(chapter: BookChapter) {
book?.let { book ->
webBook?.getContent(book, chapter, scope = this)
?.onSuccess(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)
}
}?.onError {
contentLoadFinish(chapter, it.localizedMessage)
removeLoading(chapter.index)
}
}
}
private fun addLoading(index: Int): Boolean {
@ -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