From fdf4f9cf5f8f7e4dd469ee2e8d8afc0b884bab5e Mon Sep 17 00:00:00 2001 From: LeanBitLab Date: Fri, 20 Nov 2026 14:02:54 +0530 Subject: [PATCH] fix(settings): match search against translated card text Settings search only matched hardcoded English keywords, so searching in any other app language found nothing. Also match the visible text of each card, case- and accent-insensitively, keeping the English keywords as aliases. --- .../com/leanbitlab/lwidget/SettingsSearch.kt | 26 +++++++- .../leanbitlab/lwidget/SettingsSearchTest.kt | 61 +++++++++++++++++++ 2 files changed, 85 insertions(+), 2 deletions(-) create mode 100644 app/src/test/java/com/leanbitlab/lwidget/SettingsSearchTest.kt diff --git a/app/src/main/java/com/leanbitlab/lwidget/SettingsSearch.kt b/app/src/main/java/com/leanbitlab/lwidget/SettingsSearch.kt index defd349..51d4f8a 100644 --- a/app/src/main/java/com/leanbitlab/lwidget/SettingsSearch.kt +++ b/app/src/main/java/com/leanbitlab/lwidget/SettingsSearch.kt @@ -121,11 +121,31 @@ internal fun MainActivity.setupGlobalSettingsSearch() { findViewById(R.id.label_category_stats) ) + // Case- and accent-insensitive form, so "Événement" matches "evenement" and Turkish I/ı fold correctly. + val locale = resources.configuration.locales[0] + val combiningMarks = Regex("\\p{Mn}+") + fun normalize(text: CharSequence): String = + java.text.Normalizer.normalize(text.toString().lowercase(locale), java.text.Normalizer.Form.NFD) + .replace(combiningMarks, "") + + // Visible (already translated) text of each card, so search works in every app language. + // Built when a search starts and dropped when it ends, so it reflects current values. + var cardTextIndex: Map? = null + fun buildCardTextIndex(): Map = cardMap.keys.associateWith { cardId -> + val text = StringBuilder() + fun collect(view: View) { + if (view is android.widget.TextView && view !is android.widget.EditText) text.append(view.text).append('\n') + if (view is android.view.ViewGroup) for (i in 0 until view.childCount) collect(view.getChildAt(i)) + } + findViewById(cardId)?.let { collect(it) } + normalize(text) + } + searchInput.addTextChangedListener(object : android.text.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(s: android.text.Editable?) { - val query = s?.toString()?.trim()?.lowercase() ?: "" + val query = normalize(s?.toString()?.trim() ?: "") val isSearching = query.isNotEmpty() btnSearchClear?.visibility = if (isSearching) View.VISIBLE else View.GONE @@ -134,15 +154,17 @@ internal fun MainActivity.setupGlobalSettingsSearch() { tabContainers.forEach { container -> container?.visibility = View.VISIBLE } tabLayout?.visibility = View.GONE + val index = cardTextIndex ?: buildCardTextIndex().also { cardTextIndex = it } for ((cardId, keywords) in cardMap) { val card = findViewById(cardId) ?: continue - val matches = keywords.any { it.contains(query) } + val matches = keywords.any { it.contains(query) } || index[cardId]?.contains(query) == true card.visibility = if (matches) View.VISIBLE else View.GONE } headerLabels.forEach { label -> label.visibility = View.GONE } } else { + cardTextIndex = null // Restore tabs and active container tabLayout?.visibility = View.VISIBLE val selectedTab = tabLayout?.selectedTabPosition?.coerceIn(0, 3) ?: 0 diff --git a/app/src/test/java/com/leanbitlab/lwidget/SettingsSearchTest.kt b/app/src/test/java/com/leanbitlab/lwidget/SettingsSearchTest.kt new file mode 100644 index 0000000..c5aec16 --- /dev/null +++ b/app/src/test/java/com/leanbitlab/lwidget/SettingsSearchTest.kt @@ -0,0 +1,61 @@ +package com.leanbitlab.lwidget + +import android.content.Context +import android.view.View +import android.widget.EditText +import androidx.test.core.app.ApplicationProvider +import org.junit.Assert.assertEquals +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.Robolectric +import org.robolectric.RobolectricTestRunner +import org.robolectric.annotation.Config + +/** Settings search must find cards by their translated on-screen text, not only English keywords. */ +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [33]) +class SettingsSearchTest { + + @Before + fun setUp() { + ApplicationProvider.getApplicationContext() + .getSharedPreferences("com.leanbitlab.lwidget.PREFS", Context.MODE_PRIVATE) + .edit().clear().putBoolean("is_first_launch", false).commit() + } + + private fun search(query: String): MainActivity { + val activity = Robolectric.buildActivity(MainActivity::class.java).setup().get() + activity.findViewById(R.id.search_settings_input).setText(query) + return activity + } + + @Test + @Config(qualifiers = "ru") + fun findsCardByTranslatedText() { + val activity = search("событи") + assertEquals(View.VISIBLE, activity.findViewById(R.id.card_events).visibility) + assertEquals(View.GONE, activity.findViewById(R.id.card_language).visibility) + } + + @Test + @Config(qualifiers = "ru") + fun englishKeywordsStillMatch() { + val activity = search("calendar") + assertEquals(View.VISIBLE, activity.findViewById(R.id.card_events).visibility) + } + + @Test + fun ignoresCaseAndAccents() { + val activity = search("LANGUÁGE") + assertEquals(View.VISIBLE, activity.findViewById(R.id.card_language).visibility) + assertEquals(View.GONE, activity.findViewById(R.id.card_events).visibility) + } + + @Test + fun clearingSearchRestoresCards() { + val activity = search("событи") + activity.findViewById(R.id.search_settings_input).setText("") + assertEquals(View.VISIBLE, activity.findViewById(R.id.card_language).visibility) + } +}