Skip to content

Commit

Permalink
优化
Browse files Browse the repository at this point in the history
  • Loading branch information
gedoor committed Jul 10, 2023
1 parent bc90a9c commit 03d1447
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
Expand Down Expand Up @@ -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 ->
Expand Down
13 changes: 13 additions & 0 deletions app/src/main/res/menu/content_search.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:ignore="AlwaysShowAction">

<item
android:id="@+id/menu_enable_replace"
android:title="@string/replace"
android:checkable="true"
app:showAsAction="never" />

</menu>

0 comments on commit 03d1447

Please sign in to comment.