This commit is contained in:
kunfei
2023-07-10 23:14:26 +08:00
parent 09d1372e38
commit 496b8e3b01
3 changed files with 37 additions and 4 deletions
@@ -3,6 +3,8 @@ package io.legado.app.ui.book.searchContent
import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import android.view.MotionEvent
import android.widget.EditText
import androidx.activity.viewModels
@@ -18,7 +20,6 @@ import io.legado.app.data.entities.BookChapter
import io.legado.app.databinding.ActivitySearchContentBinding
import io.legado.app.help.IntentData
import io.legado.app.help.book.BookHelp
import io.legado.app.help.book.ContentProcessor
import io.legado.app.help.book.isLocal
import io.legado.app.lib.theme.bottomBackground
import io.legado.app.lib.theme.getPrimaryTextColor
@@ -71,6 +72,26 @@ class SearchContentActivity :
}
}
override fun onCompatCreateOptionsMenu(menu: Menu): Boolean {
menuInflater.inflate(R.menu.content_search, menu)
return super.onCompatCreateOptionsMenu(menu)
}
override fun onMenuOpened(featureId: Int, menu: Menu): Boolean {
menu.findItem(R.id.menu_enable_replace)?.isChecked = viewModel.replaceEnabled
return super.onMenuOpened(featureId, menu)
}
override fun onCompatOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) {
R.id.menu_enable_replace -> {
viewModel.replaceEnabled = !viewModel.replaceEnabled
item.isChecked = viewModel.replaceEnabled
}
}
return super.onCompatOptionsItemSelected(item)
}
override fun dispatchTouchEvent(ev: MotionEvent): Boolean {
if (ev.action == MotionEvent.ACTION_DOWN) {
currentFocus?.let {
@@ -10,9 +10,7 @@ import io.legado.app.data.entities.BookChapter
import io.legado.app.help.book.BookHelp
import io.legado.app.help.book.ContentProcessor
import io.legado.app.help.config.AppConfig
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ensureActive
import kotlinx.coroutines.withContext
import kotlin.coroutines.coroutineContext
class SearchContentViewModel(application: Application) : BaseViewModel(application) {
@@ -23,6 +21,7 @@ class SearchContentViewModel(application: Application) : BaseViewModel(applicati
var searchResultCounts = 0
val cacheChapterNames = hashSetOf<String>()
val searchResultList: MutableList<SearchResult> = mutableListOf()
var replaceEnabled = false
fun initBook(bookUrl: String, success: () -> Unit) {
this.bookUrl = bookUrl
@@ -51,7 +50,7 @@ class SearchContentViewModel(application: Application) : BaseViewModel(applicati
}
coroutineContext.ensureActive()
val mContent = contentProcessor!!.getContent(
book, chapter, chapterContent
book, chapter, chapterContent, useReplace = replaceEnabled
).toString()
val positions = searchPosition(mContent, query)
positions.forEachIndexed { index, position ->