From ded1afe5460ec4bb5abe70e689ac418bf8f75c6d Mon Sep 17 00:00:00 2001 From: kunfei Date: Sat, 26 Oct 2019 22:20:32 +0800 Subject: [PATCH] up --- .../java/io/legado/app/help/IntentDataHelp.kt | 6 ++- .../app/service/BaseReadAloudService.kt | 2 +- .../app/ui/book/info/BookInfoViewModel.kt | 37 ++++++++++--------- .../app/ui/explore/ExploreShowActivity.kt | 9 ++++- .../app/ui/explore/ExploreShowAdapter.kt | 5 ++- 5 files changed, 35 insertions(+), 24 deletions(-) diff --git a/app/src/main/java/io/legado/app/help/IntentDataHelp.kt b/app/src/main/java/io/legado/app/help/IntentDataHelp.kt index e7484949e..dc2f16ee9 100644 --- a/app/src/main/java/io/legado/app/help/IntentDataHelp.kt +++ b/app/src/main/java/io/legado/app/help/IntentDataHelp.kt @@ -10,9 +10,11 @@ object IntentDataHelp { return key } - fun getData(key: String): Any? { + @Suppress("UNCHECKED_CAST") + fun getData(key: String?): T? { + if (key == null) return null val data = bigData[key] bigData.remove(key) - return data + return data as? T } } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/service/BaseReadAloudService.kt b/app/src/main/java/io/legado/app/service/BaseReadAloudService.kt index 8c46f11c8..db9df4596 100644 --- a/app/src/main/java/io/legado/app/service/BaseReadAloudService.kt +++ b/app/src/main/java/io/legado/app/service/BaseReadAloudService.kt @@ -104,7 +104,7 @@ abstract class BaseReadAloudService : BaseService(), @CallSuper open fun newReadAloud(dataKey: String?, play: Boolean) { dataKey?.let { - textChapter = IntentDataHelp.getData(dataKey) as? TextChapter + textChapter = IntentDataHelp.getData(dataKey) textChapter?.let { textChapter -> nowSpeak = 0 readAloudNumber = textChapter.getReadLength(pageIndex) diff --git a/app/src/main/java/io/legado/app/ui/book/info/BookInfoViewModel.kt b/app/src/main/java/io/legado/app/ui/book/info/BookInfoViewModel.kt index c1b992275..91e412afb 100644 --- a/app/src/main/java/io/legado/app/ui/book/info/BookInfoViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/info/BookInfoViewModel.kt @@ -9,6 +9,7 @@ import io.legado.app.base.BaseViewModel import io.legado.app.data.entities.Book import io.legado.app.data.entities.BookChapter import io.legado.app.help.BookHelp +import io.legado.app.help.IntentDataHelp import io.legado.app.model.WebBook import kotlinx.coroutines.Dispatchers.IO @@ -22,33 +23,35 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) { fun loadBook(intent: Intent) { execute { - intent.getStringExtra("bookUrl")?.let { + IntentDataHelp.getData(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 -> inBookshelf = true - durChapterIndex = book.durChapterIndex - bookData.postValue(book) - val chapterList = App.db.bookChapterDao().getChapterList(it) - if (chapterList.isNotEmpty()) { - chapterListData.postValue(chapterList) - isLoadingData.postValue(false) - } else { - loadChapter(book) - } + setBook(book) } } ?: intent.getStringExtra("searchBookUrl")?.let { App.db.searchBookDao().getSearchBook(it)?.toBook()?.let { book -> - durChapterIndex = book.durChapterIndex - bookData.postValue(book) - if (book.tocUrl.isEmpty()) { - loadBookInfo(book) - } else { - loadChapter(book) - } + inBookshelf = App.db.bookDao().getBook(book.bookUrl) != null + setBook(book) } } } } + private fun setBook(book: Book) { + durChapterIndex = book.durChapterIndex + bookData.postValue(book) + val chapterList = App.db.bookChapterDao().getChapterList(book.bookUrl) + if (chapterList.isNotEmpty()) { + chapterListData.postValue(chapterList) + isLoadingData.postValue(false) + } else { + loadChapter(book) + } + } + fun loadBookInfo( book: Book, changeDruChapterIndex: ((chapters: List) -> Unit)? = null diff --git a/app/src/main/java/io/legado/app/ui/explore/ExploreShowActivity.kt b/app/src/main/java/io/legado/app/ui/explore/ExploreShowActivity.kt index 34c30be8c..57f2c6fca 100644 --- a/app/src/main/java/io/legado/app/ui/explore/ExploreShowActivity.kt +++ b/app/src/main/java/io/legado/app/ui/explore/ExploreShowActivity.kt @@ -9,7 +9,9 @@ import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView import io.legado.app.R import io.legado.app.base.VMBaseActivity +import io.legado.app.data.entities.Book 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.utils.getViewModel import io.legado.app.utils.visible @@ -83,7 +85,10 @@ class ExploreShowActivity : VMBaseActivity(R.layout.activi } } - override fun showBookInfo(bookUrl: String) { - startActivity(Pair("searchBookUrl", bookUrl)) + override fun showBookInfo(book: Book) { + startActivity( + Pair("searchBookUrl", book.bookUrl), + Pair("key", IntentDataHelp.putData(book, System.currentTimeMillis().toString())) + ) } } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/explore/ExploreShowAdapter.kt b/app/src/main/java/io/legado/app/ui/explore/ExploreShowAdapter.kt index 1a248e736..5daa76472 100644 --- a/app/src/main/java/io/legado/app/ui/explore/ExploreShowAdapter.kt +++ b/app/src/main/java/io/legado/app/ui/explore/ExploreShowAdapter.kt @@ -4,6 +4,7 @@ import android.content.Context import io.legado.app.R import io.legado.app.base.adapter.ItemViewHolder 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.help.ImageLoader import io.legado.app.utils.gone @@ -65,11 +66,11 @@ class ExploreShowAdapter(context: Context, val callBack: CallBack) : .setAsDrawable(iv_cover) } onClick { - callBack.showBookInfo(item.bookUrl) + callBack.showBookInfo(item.toBook()) } } interface CallBack { - fun showBookInfo(bookUrl: String) + fun showBookInfo(book: Book) } } \ No newline at end of file