Explorar o código

Fix search inputs accepting formatted text (#6501)

* Fix Global and extension search input accepts formatted text #6495

* Code change as requested because of performance issue

* code changes as requested

* minor code changes
Rajat %!s(int64=3) %!d(string=hai) anos
pai
achega
77a3acf5cc

+ 19 - 0
app/src/main/java/eu/kanade/tachiyomi/ui/base/controller/SearchableNucleusController.kt

@@ -2,12 +2,16 @@ package eu.kanade.tachiyomi.ui.base.controller
 
 import android.app.Activity
 import android.os.Bundle
+import android.text.Editable
+import android.text.TextWatcher
+import android.text.style.StyleSpan
 import android.view.Menu
 import android.view.MenuInflater
 import android.view.MenuItem
 import androidx.annotation.StringRes
 import androidx.appcompat.widget.SearchView
 import androidx.viewbinding.ViewBinding
+import eu.kanade.tachiyomi.R
 import eu.kanade.tachiyomi.ui.base.presenter.BasePresenter
 import kotlinx.coroutines.flow.launchIn
 import kotlinx.coroutines.flow.onEach
@@ -52,6 +56,21 @@ abstract class SearchableNucleusController<VB : ViewBinding, P : BasePresenter<*
         searchItem.fixExpand(onExpand = { invalidateMenuOnExpand() })
         searchView.maxWidth = Int.MAX_VALUE
 
+        // Remove formatting from pasted text
+        val searchAutoComplete: SearchView.SearchAutoComplete = searchView.findViewById(
+            R.id.search_src_text
+        )
+        searchAutoComplete.addTextChangedListener(object : TextWatcher {
+            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}
+
+            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {}
+
+            override fun afterTextChanged(editable: Editable) {
+                editable.getSpans(0, editable.length, StyleSpan::class.java)
+                    .forEach { editable.removeSpan(it) }
+            }
+        })
+
         searchView.queryTextEvents()
             .onEach {
                 val newText = it.queryText.toString()