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 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,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)) { if (addLoading(index)) {
execute { execute {
App.db.bookChapterDao().getChapter(book.bookUrl, index)?.let { chapter -> App.db.bookChapterDao().getChapter(book.bookUrl, index)?.let { chapter ->
BookHelp.getContent(book, chapter)?.let { BookHelp.getContent(book, chapter)?.let {
contentLoadFinish(chapter, it) contentLoadFinish(chapter, it)
removeLoading(chapter.index) removeLoading(chapter.index)
} ?: download(book, chapter) } ?: download(chapter)
} ?: removeLoading(index) } ?: removeLoading(index)
}.onError { }.onError {
removeLoading(index) removeLoading(index)
} }
} }
} }
}
private fun download(book: Book, chapter: BookChapter) { private fun download(chapter: BookChapter) {
book?.let { book ->
webBook?.getContent(book, chapter, scope = this) webBook?.getContent(book, chapter, scope = this)
?.onSuccess(Dispatchers.IO) { content -> ?.onSuccess(Dispatchers.IO) { content ->
if (content.isNullOrEmpty()) { if (content.isNullOrEmpty()) {
@ -129,6 +132,7 @@ class AudioPlayViewModel(application: Application) : BaseViewModel(application)
removeLoading(chapter.index) removeLoading(chapter.index)
} }
} }
}
private fun addLoading(index: Int): Boolean { private fun addLoading(index: Int): Boolean {
synchronized(this) { synchronized(this) {
@ -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(it, durChapterIndex) loadContent(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,45 +142,46 @@ 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) {
book?.let { book ->
if (addLoading(index)) { if (addLoading(index)) {
execute { execute {
App.db.bookChapterDao().getChapter(book.bookUrl, index)?.let { chapter -> App.db.bookChapterDao().getChapter(book.bookUrl, index)?.let { chapter ->
BookHelp.getContent(book, chapter)?.let { BookHelp.getContent(book, chapter)?.let {
contentLoadFinish(chapter, it) contentLoadFinish(chapter, it)
removeLoading(chapter.index) removeLoading(chapter.index)
} ?: download(book, chapter) } ?: download(chapter)
} ?: removeLoading(index) } ?: removeLoading(index)
}.onError { }.onError {
removeLoading(index) removeLoading(index)
} }
} }
} }
}
private fun download(book: Book, index: Int) { private fun download(index: Int) {
book?.let { book ->
if (addLoading(index)) { if (addLoading(index)) {
execute { execute {
App.db.bookChapterDao().getChapter(book.bookUrl, index)?.let { chapter -> App.db.bookChapterDao().getChapter(book.bookUrl, index)?.let { chapter ->
if (BookHelp.hasContent(book, chapter)) { if (BookHelp.hasContent(book, chapter)) {
removeLoading(chapter.index) removeLoading(chapter.index)
} else { } else {
download(book, chapter) download(chapter)
} }
} ?: removeLoading(index) } ?: removeLoading(index)
}.onError { }.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) webBook?.getContent(book, chapter, scope = this)
?.onSuccess(IO) { content -> ?.onSuccess(IO) { content ->
if (content.isNullOrEmpty()) { if (content.isNullOrEmpty()) {
@ -207,6 +208,7 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
removeLoading(chapter.index) removeLoading(chapter.index)
} }
} }
}
private fun addLoading(index: Int): Boolean { private fun addLoading(index: Int): Boolean {
synchronized(this) { synchronized(this) {
@ -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