Skip to content

Commit

Permalink
Enable searching by English quest titles (#5284)
Browse files Browse the repository at this point in the history
* Enable searching by English quest titles

* Fix minor IDE warnings

* Simplify `questTypeMatchesSearchWords` function

Co-authored-by: Tobias Zwick <newton@westnordost.de>

* Restore `applyChecked(b)`

---------

Co-authored-by: Tobias Zwick <newton@westnordost.de>
  • Loading branch information
FloEdelmann and westnordost authored Oct 5, 2023
1 parent dbff98b commit c6feb46
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 */
Expand All @@ -80,15 +89,16 @@ class QuestSelectionAdapter(
}
}

private fun questTypeMatchesSearchWords(questType: QuestType, words: List<String>) =
genericQuestTitle(context.resources, questType).lowercase().containsAll(words)
|| genericQuestTitle(englishResources, questType).lowercase().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) })
}
}

Expand Down Expand Up @@ -121,7 +131,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() }
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>) = words.all { this.contains(it) }

0 comments on commit c6feb46

Please sign in to comment.