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

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

Loading…
Cancel
Save