From c702df43c1459f0011f39458a3353b89bbc8e9ac Mon Sep 17 00:00:00 2001 From: gedoor Date: Thu, 6 Jan 2022 23:17:59 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/io/legado/app/help/IntentData.kt | 3 ++- .../legado/app/ui/book/read/ReadBookActivity.kt | 11 +++++++---- .../io/legado/app/ui/book/read/SearchMenu.kt | 16 +++++----------- .../book/searchContent/SearchContentActivity.kt | 6 ++++++ 4 files changed, 20 insertions(+), 16 deletions(-) diff --git a/app/src/main/java/io/legado/app/help/IntentData.kt b/app/src/main/java/io/legado/app/help/IntentData.kt index 2ce48bde3..474a88bc8 100644 --- a/app/src/main/java/io/legado/app/help/IntentData.kt +++ b/app/src/main/java/io/legado/app/help/IntentData.kt @@ -5,10 +5,11 @@ object IntentData { private val bigData: MutableMap = mutableMapOf() @Synchronized - fun put(key: String, data: Any?) { + fun put(key: String, data: Any?): String { data?.let { bigData[key] = data } + return key } @Synchronized diff --git a/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt b/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt index cdfabc26a..b8df716c3 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt @@ -99,11 +99,14 @@ class ReadBookActivity : BaseReadBookActivity(), private val searchContentActivity = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { it ?: return@registerForActivityResult it.data?.let { data -> - data.getIntExtra("chapterIndex", ReadBook.durChapterIndex).let { - viewModel.searchContentQuery = data.getStringExtra("query") ?: "" - val searchResultIndex = data.getIntExtra("searchResultIndex", 0) + val key = data.getLongExtra("key", System.currentTimeMillis()) + val searchResult = IntentData.get("searchResult$key") + val searchResultList = IntentData.get>("searchResultList$key") + if (searchResult != null && searchResultList != null) { + viewModel.searchContentQuery = searchResult.query + binding.searchMenu.upSearchResultList(searchResultList) isShowingSearchResult = true - binding.searchMenu.updateSearchResultIndex(searchResultIndex) + binding.searchMenu.updateSearchResultIndex(searchResultList.indexOf(searchResult)) binding.searchMenu.selectedSearchResult?.let { currentResult -> skipToSearch(currentResult) showActionMenu() diff --git a/app/src/main/java/io/legado/app/ui/book/read/SearchMenu.kt b/app/src/main/java/io/legado/app/ui/book/read/SearchMenu.kt index fd89dc2e6..7a9519109 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/SearchMenu.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/SearchMenu.kt @@ -10,7 +10,6 @@ import android.view.animation.Animation import android.widget.FrameLayout import androidx.core.view.isVisible import io.legado.app.R -import io.legado.app.constant.EventBus import io.legado.app.databinding.ViewSearchMenuBinding import io.legado.app.help.* import io.legado.app.lib.theme.* @@ -46,7 +45,7 @@ class SearchMenu @JvmOverloads constructor( private val hasSearchResult: Boolean get() = searchResultList.isNotEmpty() val selectedSearchResult: SearchResult? - get() = if (searchResultList.isNotEmpty()) searchResultList[currentSearchResultIndex] else null + get() = searchResultList.getOrNull(currentSearchResultIndex) val previousSearchResult: SearchResult get() = searchResultList[lastSearchResultIndex] @@ -55,17 +54,12 @@ class SearchMenu @JvmOverloads constructor( initView() bindEvent() updateSearchInfo() - observeSearchResultList() } - private fun observeSearchResultList() { - activity?.let { owner -> - eventObservable>(EventBus.SEARCH_RESULT).observe(owner, { - searchResultList.clear() - searchResultList.addAll(it) - updateSearchInfo() - }) - } + fun upSearchResultList(resultList: List) { + searchResultList.clear() + searchResultList.addAll(resultList) + updateSearchInfo() } private fun initView() = binding.run { diff --git a/app/src/main/java/io/legado/app/ui/book/searchContent/SearchContentActivity.kt b/app/src/main/java/io/legado/app/ui/book/searchContent/SearchContentActivity.kt index 274cc4fd3..d91dc2719 100644 --- a/app/src/main/java/io/legado/app/ui/book/searchContent/SearchContentActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/searchContent/SearchContentActivity.kt @@ -13,6 +13,7 @@ import io.legado.app.data.entities.Book import io.legado.app.data.entities.BookChapter import io.legado.app.databinding.ActivitySearchContentBinding import io.legado.app.help.BookHelp +import io.legado.app.help.IntentData import io.legado.app.lib.theme.bottomBackground import io.legado.app.lib.theme.getPrimaryTextColor import io.legado.app.lib.theme.primaryTextColor @@ -165,6 +166,11 @@ class SearchContentActivity : override fun openSearchResult(searchResult: SearchResult) { postEvent(EventBus.SEARCH_RESULT, viewModel.searchResultList as List) val searchData = Intent() + val key = System.currentTimeMillis() + IntentData.put("searchResult$key", searchResult) + IntentData.put("searchResultList$key", viewModel.searchResultList) + searchData.putExtra("key", key) + searchData.putExtra("searchResultIndex", viewModel.searchResultList.indexOf(searchResult)) searchData.putExtra("chapterIndex", searchResult.chapterIndex) searchData.putExtra("contentPosition", searchResult.queryIndexInChapter)