Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable searching by English quest titles #5284

Merged
merged 4 commits into from
Oct 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) }