Skip to content

Commit

Permalink
Fix spell search not working with special chars (#297)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandregpereira authored Jul 13, 2024
1 parent 9b0ed5a commit d0e6a2d
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 16 deletions.
11 changes: 11 additions & 0 deletions core/search/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
plugins {
kotlin("multiplatform")
}

multiplatform {
commonMain {
implementation(libs.kotlin.coroutines.core)
}
jvmMain()
iosMain()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package br.alexandregpereira.hunter.search

fun String.removeAccents(): String {
val accentMappings = mapOf(
'á' to 'a', 'é' to 'e', 'í' to 'i', 'ó' to 'o', 'ú' to 'u',
'â' to 'a', 'ê' to 'e', 'î' to 'i', 'ô' to 'o', 'û' to 'u',
'ã' to 'a', 'õ' to 'o', 'ç' to 'c',
'à' to 'a', 'è' to 'e', 'ì' to 'i', 'ò' to 'o', 'ù' to 'u',
'Á' to 'A', 'É' to 'E', 'Í' to 'I', 'Ó' to 'O', 'Ú' to 'U',
'Â' to 'A', 'Ê' to 'E', 'Î' to 'I', 'Ô' to 'O', 'Û' to 'U',
'Ã' to 'A', 'Õ' to 'O', 'Ç' to 'C',
'À' to 'A', 'È' to 'E', 'Ì' to 'I', 'Ò' to 'O', 'Ù' to 'U'
)

return map { char -> accentMappings[char] ?: char }.joinToString("")
}
1 change: 1 addition & 0 deletions feature/search/compose/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ multiplatform {
implementation(project(":core:analytics"))
implementation(project(":core:localization"))
implementation(project(":core:state-holder"))
implementation(project(":core:search"))
implementation(project(":domain:monster:core"))
implementation(project(":domain:spell:core"))
implementation(project(":feature:folder-preview:event"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import br.alexandregpereira.hunter.domain.spell.GetSpellsByIdsUseCase
import br.alexandregpereira.hunter.domain.spell.model.Spell
import br.alexandregpereira.hunter.domain.usecase.GetMonsterPreviewsCacheUseCase
import br.alexandregpereira.hunter.domain.usecase.GetMonstersUseCase
import br.alexandregpereira.hunter.search.removeAccents
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.catch
Expand Down Expand Up @@ -179,21 +180,6 @@ internal class SearchMonstersByUseCase internal constructor(
} ?: false
}

private fun String.removeAccents(): String {
val accentMappings = mapOf(
'á' to 'a', 'é' to 'e', 'í' to 'i', 'ó' to 'o', 'ú' to 'u',
'â' to 'a', 'ê' to 'e', 'î' to 'i', 'ô' to 'o', 'û' to 'u',
'ã' to 'a', 'õ' to 'o', 'ç' to 'c',
'à' to 'a', 'è' to 'e', 'ì' to 'i', 'ò' to 'o', 'ù' to 'u',
'Á' to 'A', 'É' to 'E', 'Í' to 'I', 'Ó' to 'O', 'Ú' to 'U',
'Â' to 'A', 'Ê' to 'E', 'Î' to 'I', 'Ô' to 'O', 'Û' to 'U',
'Ã' to 'A', 'Õ' to 'O', 'Ç' to 'C',
'À' to 'A', 'È' to 'E', 'Ì' to 'I', 'Ò' to 'O', 'Ù' to 'U'
)

return map { char -> accentMappings[char] ?: char }.joinToString("")
}

private enum class KeySearch {
NAME, TYPE, CHALLENGE_RATING, SPELL
}
Expand Down
1 change: 1 addition & 0 deletions feature/spell-compendium/state-holder/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ multiplatform {
implementation(project(":core:state-holder"))
implementation(project(":core:event"))
implementation(project(":core:localization"))
implementation(project(":core:search"))
implementation(project(":domain:spell:core"))
implementation(project(":feature:spell-compendium:event"))
implementation(libs.kotlin.coroutines.core)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import br.alexandregpereira.hunter.domain.spell.model.Spell
import br.alexandregpereira.hunter.event.EventDispatcher
import br.alexandregpereira.hunter.event.EventListener
import br.alexandregpereira.hunter.localization.AppLocalization
import br.alexandregpereira.hunter.search.removeAccents
import br.alexandregpereira.hunter.spell.compendium.domain.GetSpellsUseCase
import br.alexandregpereira.hunter.spell.compendium.event.SpellCompendiumEvent
import br.alexandregpereira.hunter.spell.compendium.event.SpellCompendiumResult
Expand All @@ -14,6 +15,7 @@ import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.debounce
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.onEach

@OptIn(FlowPreview::class)
Expand Down Expand Up @@ -67,11 +69,14 @@ class SpellCompendiumStateHolder internal constructor(

private fun debounceSearch() {
searchQuery.debounce(500L)
.map {
it.removeAccents().trim()
}
.onEach { text ->
setState {
val spellsGroupByLevel = if (text.isNotBlank()){
val spellsFiltered = originalSpellsGroupByLevel.values.flatten()
.filter { it.name.contains(text, ignoreCase = true) }
.filter { it.name.removeAccents().contains(text, ignoreCase = true) }
mapOf(strings.searchResults(spellsFiltered.size) to spellsFiltered)
} else originalSpellsGroupByLevel

Expand Down
1 change: 1 addition & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ include ':core:localization'
include ':core:state-holder'
include ':core:ui:state-recovery'
include ':core:uuid'
include ':core:search'

include ':domain:app:core'
include ':domain:monster:core'
Expand Down

0 comments on commit d0e6a2d

Please sign in to comment.