pull/43/head
kunfei 5 years ago
parent 2bed9af097
commit ded1afe546
  1. 6
      app/src/main/java/io/legado/app/help/IntentDataHelp.kt
  2. 2
      app/src/main/java/io/legado/app/service/BaseReadAloudService.kt
  3. 33
      app/src/main/java/io/legado/app/ui/book/info/BookInfoViewModel.kt
  4. 9
      app/src/main/java/io/legado/app/ui/explore/ExploreShowActivity.kt
  5. 5
      app/src/main/java/io/legado/app/ui/explore/ExploreShowAdapter.kt

@ -10,9 +10,11 @@ object IntentDataHelp {
return key return key
} }
fun getData(key: String): Any? { @Suppress("UNCHECKED_CAST")
fun <T> getData(key: String?): T? {
if (key == null) return null
val data = bigData[key] val data = bigData[key]
bigData.remove(key) bigData.remove(key)
return data return data as? T
} }
} }

@ -104,7 +104,7 @@ abstract class BaseReadAloudService : BaseService(),
@CallSuper @CallSuper
open fun newReadAloud(dataKey: String?, play: Boolean) { open fun newReadAloud(dataKey: String?, play: Boolean) {
dataKey?.let { dataKey?.let {
textChapter = IntentDataHelp.getData(dataKey) as? TextChapter textChapter = IntentDataHelp.getData<TextChapter>(dataKey)
textChapter?.let { textChapter -> textChapter?.let { textChapter ->
nowSpeak = 0 nowSpeak = 0
readAloudNumber = textChapter.getReadLength(pageIndex) readAloudNumber = textChapter.getReadLength(pageIndex)

@ -9,6 +9,7 @@ import io.legado.app.base.BaseViewModel
import io.legado.app.data.entities.Book 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.BookHelp import io.legado.app.help.BookHelp
import io.legado.app.help.IntentDataHelp
import io.legado.app.model.WebBook import io.legado.app.model.WebBook
import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.Dispatchers.IO
@ -22,32 +23,34 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) {
fun loadBook(intent: Intent) { fun loadBook(intent: Intent) {
execute { execute {
intent.getStringExtra("bookUrl")?.let { IntentDataHelp.getData<Book>(intent.getStringExtra("key"))?.let { book ->
inBookshelf = App.db.bookDao().getBook(book.bookUrl) != null
setBook(book)
} ?: intent.getStringExtra("bookUrl")?.let {
App.db.bookDao().getBook(it)?.let { book -> App.db.bookDao().getBook(it)?.let { book ->
inBookshelf = true inBookshelf = true
durChapterIndex = book.durChapterIndex setBook(book)
bookData.postValue(book)
val chapterList = App.db.bookChapterDao().getChapterList(it)
if (chapterList.isNotEmpty()) {
chapterListData.postValue(chapterList)
isLoadingData.postValue(false)
} else {
loadChapter(book)
}
} }
} ?: intent.getStringExtra("searchBookUrl")?.let { } ?: intent.getStringExtra("searchBookUrl")?.let {
App.db.searchBookDao().getSearchBook(it)?.toBook()?.let { book -> App.db.searchBookDao().getSearchBook(it)?.toBook()?.let { book ->
inBookshelf = App.db.bookDao().getBook(book.bookUrl) != null
setBook(book)
}
}
}
}
private fun setBook(book: Book) {
durChapterIndex = book.durChapterIndex durChapterIndex = book.durChapterIndex
bookData.postValue(book) bookData.postValue(book)
if (book.tocUrl.isEmpty()) { val chapterList = App.db.bookChapterDao().getChapterList(book.bookUrl)
loadBookInfo(book) if (chapterList.isNotEmpty()) {
chapterListData.postValue(chapterList)
isLoadingData.postValue(false)
} else { } else {
loadChapter(book) loadChapter(book)
} }
} }
}
}
}
fun loadBookInfo( fun loadBookInfo(
book: Book, book: Book,

@ -9,7 +9,9 @@ import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView
import io.legado.app.R import io.legado.app.R
import io.legado.app.base.VMBaseActivity import io.legado.app.base.VMBaseActivity
import io.legado.app.data.entities.Book
import io.legado.app.data.entities.SearchBook import io.legado.app.data.entities.SearchBook
import io.legado.app.help.IntentDataHelp
import io.legado.app.ui.book.info.BookInfoActivity import io.legado.app.ui.book.info.BookInfoActivity
import io.legado.app.utils.getViewModel import io.legado.app.utils.getViewModel
import io.legado.app.utils.visible import io.legado.app.utils.visible
@ -83,7 +85,10 @@ class ExploreShowActivity : VMBaseActivity<ExploreShowViewModel>(R.layout.activi
} }
} }
override fun showBookInfo(bookUrl: String) { override fun showBookInfo(book: Book) {
startActivity<BookInfoActivity>(Pair("searchBookUrl", bookUrl)) startActivity<BookInfoActivity>(
Pair("searchBookUrl", book.bookUrl),
Pair("key", IntentDataHelp.putData(book, System.currentTimeMillis().toString()))
)
} }
} }

@ -4,6 +4,7 @@ import android.content.Context
import io.legado.app.R import io.legado.app.R
import io.legado.app.base.adapter.ItemViewHolder import io.legado.app.base.adapter.ItemViewHolder
import io.legado.app.base.adapter.SimpleRecyclerAdapter import io.legado.app.base.adapter.SimpleRecyclerAdapter
import io.legado.app.data.entities.Book
import io.legado.app.data.entities.SearchBook import io.legado.app.data.entities.SearchBook
import io.legado.app.help.ImageLoader import io.legado.app.help.ImageLoader
import io.legado.app.utils.gone import io.legado.app.utils.gone
@ -65,11 +66,11 @@ class ExploreShowAdapter(context: Context, val callBack: CallBack) :
.setAsDrawable(iv_cover) .setAsDrawable(iv_cover)
} }
onClick { onClick {
callBack.showBookInfo(item.bookUrl) callBack.showBookInfo(item.toBook())
} }
} }
interface CallBack { interface CallBack {
fun showBookInfo(bookUrl: String) fun showBookInfo(book: Book)
} }
} }
Loading…
Cancel
Save