pull/47/head
kunfei 5 years ago
parent 37feed6768
commit 539164a949
  1. 17
      app/src/main/java/io/legado/app/ui/audio/AudioPlayActivity.kt
  2. 40
      app/src/main/java/io/legado/app/ui/audio/AudioPlayViewModel.kt

@ -7,7 +7,6 @@ import io.legado.app.R
import io.legado.app.base.VMBaseActivity import io.legado.app.base.VMBaseActivity
import io.legado.app.constant.Bus import io.legado.app.constant.Bus
import io.legado.app.constant.Status import io.legado.app.constant.Status
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.BlurTransformation import io.legado.app.help.BlurTransformation
import io.legado.app.help.ImageLoader import io.legado.app.help.ImageLoader
@ -32,7 +31,8 @@ class AudioPlayActivity : VMBaseActivity<AudioPlayViewModel>(R.layout.activity_a
override fun onActivityCreated(savedInstanceState: Bundle?) { override fun onActivityCreated(savedInstanceState: Bundle?) {
setSupportActionBar(toolbar) setSupportActionBar(toolbar)
viewModel.callBack = this viewModel.callBack = this
viewModel.bookData.observe(this, Observer { upView(it) }) viewModel.titleData.observe(this, Observer { title_bar.title = it })
viewModel.coverData.observe(this, Observer { upCover(it) })
viewModel.initData(intent) viewModel.initData(intent)
initView() initView()
} }
@ -67,14 +67,13 @@ class AudioPlayActivity : VMBaseActivity<AudioPlayViewModel>(R.layout.activity_a
}) })
} }
private fun upView(book: Book) { private fun upCover(path: String) {
title_bar.title = book.name ImageLoader.load(this, path)
ImageLoader.load(this, book.getDisplayCover())
.placeholder(R.drawable.image_cover_default) .placeholder(R.drawable.image_cover_default)
.error(R.drawable.image_cover_default) .error(R.drawable.image_cover_default)
.centerCrop() .centerCrop()
.setAsDrawable(iv_cover) .setAsDrawable(iv_cover)
ImageLoader.load(this, book.getDisplayCover()) ImageLoader.load(this, path)
.placeholder(R.drawable.image_cover_default) .placeholder(R.drawable.image_cover_default)
.error(R.drawable.image_cover_default) .error(R.drawable.image_cover_default)
.bitmapTransform(BlurTransformation(this, 25)) .bitmapTransform(BlurTransformation(this, 25))
@ -86,7 +85,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.bookData.value?.let { else -> viewModel.book?.let {
viewModel.loadContent(it, viewModel.durChapterIndex) viewModel.loadContent(it, viewModel.durChapterIndex)
} }
} }
@ -95,12 +94,12 @@ class AudioPlayActivity : VMBaseActivity<AudioPlayViewModel>(R.layout.activity_a
override fun contentLoadFinish(bookChapter: BookChapter, content: String) { override fun contentLoadFinish(bookChapter: BookChapter, content: String) {
AudioPlay.play( AudioPlay.play(
this, this,
viewModel.bookData.value?.name, viewModel.book?.name,
bookChapter.title, bookChapter.title,
content, content,
viewModel.durPageIndex viewModel.durPageIndex
) )
viewModel.bookData.value?.let { viewModel.book?.let {
viewModel.loadContent(it, viewModel.durChapterIndex + 1) viewModel.loadContent(it, viewModel.durChapterIndex + 1)
} }
} }

@ -16,8 +16,10 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
class AudioPlayViewModel(application: Application) : BaseViewModel(application) { class AudioPlayViewModel(application: Application) : BaseViewModel(application) {
var titleData = MutableLiveData<String>()
var coverData = MutableLiveData<String>()
var book: Book? = null
var inBookshelf = false var inBookshelf = false
var bookData = MutableLiveData<Book>()
var chapterSize = 0 var chapterSize = 0
var callBack: CallBack? = null var callBack: CallBack? = null
var durChapterIndex = 0 var durChapterIndex = 0
@ -30,16 +32,16 @@ class AudioPlayViewModel(application: Application) : BaseViewModel(application)
execute { execute {
inBookshelf = intent.getBooleanExtra("inBookshelf", true) inBookshelf = intent.getBooleanExtra("inBookshelf", true)
val bookUrl = intent.getStringExtra("bookUrl") val bookUrl = intent.getStringExtra("bookUrl")
val book = if (!bookUrl.isNullOrEmpty()) { book = if (!bookUrl.isNullOrEmpty()) {
App.db.bookDao().getBook(bookUrl) App.db.bookDao().getBook(bookUrl)
} else { } else {
App.db.bookDao().lastReadBook App.db.bookDao().lastReadBook
} }
book?.let { book?.let { book ->
titleData.postValue(book.name)
durChapterIndex = book.durChapterIndex durChapterIndex = book.durChapterIndex
durPageIndex = book.durChapterPos durPageIndex = book.durChapterPos
isLocalBook = book.origin == BookType.local isLocalBook = book.origin == BookType.local
bookData.postValue(book)
App.db.bookSourceDao().getBookSource(book.origin)?.let { App.db.bookSourceDao().getBookSource(book.origin)?.let {
webBook = WebBook(it) webBook = WebBook(it)
} }
@ -57,7 +59,7 @@ class AudioPlayViewModel(application: Application) : BaseViewModel(application)
chapterSize = count chapterSize = count
} }
} }
saveRead(book) saveRead()
} }
} }
@ -148,23 +150,23 @@ class AudioPlayViewModel(application: Application) : BaseViewModel(application)
} }
} }
fun changeTo(book: Book) { fun changeTo(book1: Book) {
execute { execute {
bookData.value?.let { book?.let {
App.db.bookDao().delete(it.bookUrl) App.db.bookDao().delete(it.bookUrl)
} }
withContext(Dispatchers.Main) { withContext(Dispatchers.Main) {
} }
App.db.bookDao().insert(book) App.db.bookDao().insert(book1)
bookData.postValue(book) book = book1
App.db.bookSourceDao().getBookSource(book.origin)?.let { App.db.bookSourceDao().getBookSource(book1.origin)?.let {
webBook = WebBook(it) webBook = WebBook(it)
} }
if (book.tocUrl.isEmpty()) { if (book1.tocUrl.isEmpty()) {
loadBookInfo(book) { upChangeDurChapterIndex(book, it) } loadBookInfo(book1) { upChangeDurChapterIndex(book1, it) }
} else { } else {
loadChapterList(book) { upChangeDurChapterIndex(book, it) } loadChapterList(book1) { upChangeDurChapterIndex(book1, it) }
} }
} }
} }
@ -187,9 +189,9 @@ class AudioPlayViewModel(application: Application) : BaseViewModel(application)
fun moveToPrev() { fun moveToPrev() {
if (durChapterIndex > 0) { if (durChapterIndex > 0) {
durChapterIndex-- durChapterIndex--
bookData.value?.let { book?.durChapterIndex = durChapterIndex
it.durChapterIndex = durChapterIndex saveRead()
saveRead(it) book?.let {
loadContent(it, durChapterIndex) loadContent(it, durChapterIndex)
} }
} }
@ -198,9 +200,9 @@ class AudioPlayViewModel(application: Application) : BaseViewModel(application)
fun moveToNext() { fun moveToNext() {
if (durChapterIndex < chapterSize - 1) { if (durChapterIndex < chapterSize - 1) {
durChapterIndex++ durChapterIndex++
bookData.value?.let { book?.let {
it.durChapterIndex = durChapterIndex it.durChapterIndex = durChapterIndex
saveRead(it) saveRead()
loadContent(it, durChapterIndex) loadContent(it, durChapterIndex)
} }
} else { } else {
@ -208,7 +210,7 @@ class AudioPlayViewModel(application: Application) : BaseViewModel(application)
} }
} }
fun saveRead(book: Book? = bookData.value) { fun saveRead() {
execute { execute {
book?.let { book -> book?.let { book ->
book.lastCheckCount = 0 book.lastCheckCount = 0

Loading…
Cancel
Save