From f0c6a6c0b81ab5d2bb1ba5f6e210088a28f2d4cf Mon Sep 17 00:00:00 2001 From: Flo Edelmann Date: Sun, 1 Oct 2023 11:45:10 +0200 Subject: [PATCH 1/4] Enable searching by English quest titles --- .../questselection/QuestSelectionAdapter.kt | 24 +++++++++++++++---- .../streetcomplete/util/ktx/String.kt | 2 ++ 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/de/westnordost/streetcomplete/screens/settings/questselection/QuestSelectionAdapter.kt b/app/src/main/java/de/westnordost/streetcomplete/screens/settings/questselection/QuestSelectionAdapter.kt index aef09ee030..8111c5f052 100644 --- a/app/src/main/java/de/westnordost/streetcomplete/screens/settings/questselection/QuestSelectionAdapter.kt +++ b/app/src/main/java/de/westnordost/streetcomplete/screens/settings/questselection/QuestSelectionAdapter.kt @@ -2,6 +2,7 @@ package de.westnordost.streetcomplete.screens.settings.questselection import android.content.Context import android.content.SharedPreferences +import android.content.res.Configuration import android.view.LayoutInflater import android.view.MotionEvent import android.view.ViewGroup @@ -35,6 +36,7 @@ import de.westnordost.streetcomplete.data.visiblequests.VisibleQuestTypeControll import de.westnordost.streetcomplete.data.visiblequests.VisibleQuestTypeSource import de.westnordost.streetcomplete.databinding.RowQuestSelectionBinding import de.westnordost.streetcomplete.screens.settings.genericQuestTitle +import de.westnordost.streetcomplete.util.ktx.containsAll import de.westnordost.streetcomplete.util.ktx.containsAny import de.westnordost.streetcomplete.util.ktx.getDouble import kotlinx.coroutines.CoroutineScope @@ -62,6 +64,13 @@ class QuestSelectionAdapter( .getIds(prefs.getDouble(Prefs.MAP_LONGITUDE), prefs.getDouble(Prefs.MAP_LATITUDE)) private val itemTouchHelper by lazy { ItemTouchHelper(TouchHelperCallback()) } + private val englishResources by lazy { + val conf = Configuration(context.resources.configuration) + conf.setLocale(Locale.ENGLISH) + val localizedContext = context.createConfigurationContext(conf) + localizedContext.resources + } + private val viewLifecycleScope = CoroutineScope(SupervisorJob() + Dispatchers.Main) /** all quest types */ @@ -80,15 +89,22 @@ class QuestSelectionAdapter( } } + private fun questTypeMatchesSearchWords(questType: QuestType, words: List): Boolean { + val question = genericQuestTitle(context.resources, questType).lowercase() + if (question.containsAll(words)) { + return true + } + + val englishQuestion = genericQuestTitle(englishResources, questType).lowercase() + return englishQuestion.containsAll(words) + } + private fun filterQuestTypes(f: String) { if (f.isEmpty()) { submitList(questTypes) } else { val words = f.lowercase().split(' ') - submitList(questTypes.filter { questVisibility -> - val question = genericQuestTitle(context.resources, questVisibility.questType).lowercase() - words.all { question.contains(it) } - }) + submitList(questTypes.filter { questTypeMatchesSearchWords(it.questType, words) }) } } diff --git a/app/src/main/java/de/westnordost/streetcomplete/util/ktx/String.kt b/app/src/main/java/de/westnordost/streetcomplete/util/ktx/String.kt index c7497114d7..ba8ce47b79 100644 --- a/app/src/main/java/de/westnordost/streetcomplete/util/ktx/String.kt +++ b/app/src/main/java/de/westnordost/streetcomplete/util/ktx/String.kt @@ -2,3 +2,5 @@ package de.westnordost.streetcomplete.util.ktx fun String.truncate(length: Int): String = if (this.length > length) substring(0, length - 1) + "…" else this + +fun String.containsAll(words: List) = words.all { this.contains(it) } From b5f386302d30ec5a6bed3f5120dc5f52d9137286 Mon Sep 17 00:00:00 2001 From: Flo Edelmann Date: Sun, 1 Oct 2023 11:45:50 +0200 Subject: [PATCH 2/4] Fix minor IDE warnings --- .../screens/settings/questselection/QuestSelectionAdapter.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/de/westnordost/streetcomplete/screens/settings/questselection/QuestSelectionAdapter.kt b/app/src/main/java/de/westnordost/streetcomplete/screens/settings/questselection/QuestSelectionAdapter.kt index 8111c5f052..733b06b247 100644 --- a/app/src/main/java/de/westnordost/streetcomplete/screens/settings/questselection/QuestSelectionAdapter.kt +++ b/app/src/main/java/de/westnordost/streetcomplete/screens/settings/questselection/QuestSelectionAdapter.kt @@ -137,7 +137,7 @@ class QuestSelectionAdapter( } override fun onQuestTypeOrdersChanged() { - // all/many quest orders have been changed - reinit list + // all/many quest orders have been changed - re-init list viewLifecycleScope.launch { questTypes = createQuestTypeVisibilityList() } } } @@ -319,7 +319,7 @@ class QuestSelectionAdapter( AlertDialog.Builder(compoundButton.context) .setTitle(R.string.enable_quest_confirmation_title) .setMessage(item.questType.defaultDisabledMessage) - .setPositiveButton(android.R.string.ok) { _, _ -> applyChecked(b) } + .setPositiveButton(android.R.string.ok) { _, _ -> applyChecked(true) } .setNegativeButton(android.R.string.cancel) { _, _ -> compoundButton.isChecked = false } .setOnCancelListener { compoundButton.isChecked = false } .show() From ef564cd53e0d76a44730e4a8c253b7974a1e4563 Mon Sep 17 00:00:00 2001 From: Flo Edelmann Date: Tue, 3 Oct 2023 23:57:44 +0200 Subject: [PATCH 3/4] Simplify `questTypeMatchesSearchWords` function Co-authored-by: Tobias Zwick --- .../settings/questselection/QuestSelectionAdapter.kt | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/de/westnordost/streetcomplete/screens/settings/questselection/QuestSelectionAdapter.kt b/app/src/main/java/de/westnordost/streetcomplete/screens/settings/questselection/QuestSelectionAdapter.kt index 733b06b247..5423a6bd9b 100644 --- a/app/src/main/java/de/westnordost/streetcomplete/screens/settings/questselection/QuestSelectionAdapter.kt +++ b/app/src/main/java/de/westnordost/streetcomplete/screens/settings/questselection/QuestSelectionAdapter.kt @@ -89,15 +89,9 @@ class QuestSelectionAdapter( } } - private fun questTypeMatchesSearchWords(questType: QuestType, words: List): Boolean { - val question = genericQuestTitle(context.resources, questType).lowercase() - if (question.containsAll(words)) { - return true - } - - val englishQuestion = genericQuestTitle(englishResources, questType).lowercase() - return englishQuestion.containsAll(words) - } + private fun questTypeMatchesSearchWords(questType: QuestType, words: List) = + genericQuestTitle(context.resources, questType).lowercase().containsAll(words) + || genericQuestTitle(englishResources, questType).lowercase().containsAll(words) private fun filterQuestTypes(f: String) { if (f.isEmpty()) { From 0d90dfbc279b4b7a8a055b88b872b68e4d861b19 Mon Sep 17 00:00:00 2001 From: Flo Edelmann Date: Thu, 5 Oct 2023 14:57:25 +0200 Subject: [PATCH 4/4] Restore `applyChecked(b)` --- .../screens/settings/questselection/QuestSelectionAdapter.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/java/de/westnordost/streetcomplete/screens/settings/questselection/QuestSelectionAdapter.kt b/app/src/main/java/de/westnordost/streetcomplete/screens/settings/questselection/QuestSelectionAdapter.kt index 5423a6bd9b..9305e26440 100644 --- a/app/src/main/java/de/westnordost/streetcomplete/screens/settings/questselection/QuestSelectionAdapter.kt +++ b/app/src/main/java/de/westnordost/streetcomplete/screens/settings/questselection/QuestSelectionAdapter.kt @@ -313,7 +313,7 @@ class QuestSelectionAdapter( AlertDialog.Builder(compoundButton.context) .setTitle(R.string.enable_quest_confirmation_title) .setMessage(item.questType.defaultDisabledMessage) - .setPositiveButton(android.R.string.ok) { _, _ -> applyChecked(true) } + .setPositiveButton(android.R.string.ok) { _, _ -> applyChecked(b) } .setNegativeButton(android.R.string.cancel) { _, _ -> compoundButton.isChecked = false } .setOnCancelListener { compoundButton.isChecked = false } .show()