From 8c663917037bd9db836cd7372468c8be6109a5b2 Mon Sep 17 00:00:00 2001 From: kunfei Date: Sat, 26 Oct 2019 08:52:14 +0800 Subject: [PATCH] up --- .../legado/app/ui/audio/AudioPlayActivity.kt | 4 +- .../legado/app/ui/audio/AudioPlayViewModel.kt | 101 ++++++++++++++++++ 2 files changed, 104 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/io/legado/app/ui/audio/AudioPlayActivity.kt b/app/src/main/java/io/legado/app/ui/audio/AudioPlayActivity.kt index 23e2dd03b..3fd5a8965 100644 --- a/app/src/main/java/io/legado/app/ui/audio/AudioPlayActivity.kt +++ b/app/src/main/java/io/legado/app/ui/audio/AudioPlayActivity.kt @@ -4,13 +4,15 @@ import android.os.Bundle import io.legado.app.R import io.legado.app.base.VMBaseActivity import io.legado.app.utils.getViewModel +import kotlinx.android.synthetic.main.view_title_bar.* class AudioPlayActivity : VMBaseActivity(R.layout.activity_audio_play) { override val viewModel: AudioPlayViewModel get() = getViewModel(AudioPlayViewModel::class.java) override fun onActivityCreated(savedInstanceState: Bundle?) { - + setSupportActionBar(toolbar) + viewModel.initData(intent) } diff --git a/app/src/main/java/io/legado/app/ui/audio/AudioPlayViewModel.kt b/app/src/main/java/io/legado/app/ui/audio/AudioPlayViewModel.kt index aebf4da98..9367b5922 100644 --- a/app/src/main/java/io/legado/app/ui/audio/AudioPlayViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/audio/AudioPlayViewModel.kt @@ -1,9 +1,110 @@ package io.legado.app.ui.audio import android.app.Application +import android.content.Intent +import androidx.lifecycle.MutableLiveData +import io.legado.app.App +import io.legado.app.R import io.legado.app.base.BaseViewModel +import io.legado.app.constant.BookType +import io.legado.app.data.entities.Book +import io.legado.app.data.entities.BookChapter +import io.legado.app.model.WebBook +import io.legado.app.ui.book.read.ReadBookViewModel +import kotlinx.coroutines.Dispatchers class AudioPlayViewModel(application: Application) : BaseViewModel(application) { + var inBookshelf = false + var bookData = MutableLiveData() + val chapterListFinish = MutableLiveData() + var chapterSize = 0 + var callBack: ReadBookViewModel.CallBack? = null + var durChapterIndex = 0 + var durPageIndex = 0 + var isLocalBook = true + var webBook: WebBook? = null + fun initData(intent: Intent) { + execute { + inBookshelf = intent.getBooleanExtra("inBookshelf", true) + val bookUrl = intent.getStringExtra("bookUrl") + val book = if (!bookUrl.isNullOrEmpty()) { + App.db.bookDao().getBook(bookUrl) + } else { + App.db.bookDao().lastReadBook + } + book?.let { + durChapterIndex = book.durChapterIndex + durPageIndex = book.durChapterPos + isLocalBook = book.origin == BookType.local + bookData.postValue(book) + App.db.bookSourceDao().getBookSource(book.origin)?.let { + webBook = WebBook(it) + } + val count = App.db.bookChapterDao().getChapterCount(book.bookUrl) + if (count == 0) { + if (book.tocUrl.isEmpty()) { + loadBookInfo(book) + } else { + loadChapterList(book) + } + } else { + if (durChapterIndex > count - 1) { + durChapterIndex = count - 1 + } + chapterSize = count + chapterListFinish.postValue(true) + } + } + saveRead(book) + } + } + private fun loadBookInfo( + book: Book, + changeDruChapterIndex: ((chapters: List) -> Unit)? = null + ) { + execute { + webBook?.getBookInfo(book, this) + ?.onSuccess { + loadChapterList(book, changeDruChapterIndex) + } + } + } + + private fun loadChapterList( + book: Book, + changeDruChapterIndex: ((chapters: List) -> Unit)? = null + ) { + execute { + webBook?.getChapterList(book, this) + ?.onSuccess(Dispatchers.IO) { cList -> + if (!cList.isNullOrEmpty()) { + if (changeDruChapterIndex == null) { + App.db.bookChapterDao().insert(*cList.toTypedArray()) + chapterSize = cList.size + chapterListFinish.postValue(true) + } else { + changeDruChapterIndex(cList) + } + } else { + toast(R.string.error_load_toc) + } + }?.onError { + toast(R.string.error_load_toc) + } + } + } + + fun saveRead(book: Book? = bookData.value) { + execute { + book?.let { book -> + book.lastCheckCount = 0 + book.durChapterTime = System.currentTimeMillis() + book.durChapterIndex = durChapterIndex + book.durChapterPos = durPageIndex + App.db.bookDao().update(book) + } + } + } } \ No newline at end of file