This commit is contained in:
gedoor 2022-01-06 23:17:59 +08:00
parent b5670d8339
commit c702df43c1
4 changed files with 20 additions and 16 deletions

View File

@ -5,10 +5,11 @@ object IntentData {
private val bigData: MutableMap<String, Any> = mutableMapOf()
@Synchronized
fun put(key: String, data: Any?) {
fun put(key: String, data: Any?): String {
data?.let {
bigData[key] = data
}
return key
}
@Synchronized

View File

@ -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>("searchResult$key")
val searchResultList = IntentData.get<List<SearchResult>>("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()

View File

@ -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<List<SearchResult>>(EventBus.SEARCH_RESULT).observe(owner, {
searchResultList.clear()
searchResultList.addAll(it)
updateSearchInfo()
})
}
fun upSearchResultList(resultList: List<SearchResult>) {
searchResultList.clear()
searchResultList.addAll(resultList)
updateSearchInfo()
}
private fun initView() = binding.run {

View File

@ -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<SearchResult>)
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)