Skip to content
Merged
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 @@ -4,6 +4,7 @@ import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.glossostudio.transitos.core.repository.FavoritesRepository
import com.glossostudio.transitos.core.repository.TransitRepository
import java.text.Normalizer
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharingStarted
Expand All @@ -21,6 +22,7 @@ class SearchViewModel(
) : ViewModel() {

private val _query = MutableStateFlow("")
private val normalizedNames = mutableMapOf<String, String>()
val query: StateFlow<String> = _query.asStateFlow()

val allStops: StateFlow<List<com.glossostudio.transitos.core.model.Stop>> = repository.observeStops()
Expand All @@ -34,7 +36,10 @@ class SearchViewModel(
val filteredStops: StateFlow<List<com.glossostudio.transitos.core.model.Stop>> =
combine(allStops, query) { stops, q ->
if (q.isBlank()) stops
else stops.filter { it.name.contains(q, ignoreCase = true) }
else {
val normalizedQuery = q.normalized()
stops.filter { it.name.normalized().contains(normalizedQuery, ignoreCase = true) }
}
}.stateIn(viewModelScope, SharingStarted.WhileSubscribed(5_000), emptyList())

fun onQueryChange(newQuery: String) {
Expand All @@ -50,4 +55,10 @@ class SearchViewModel(
}
}
}

private fun String.normalized(): String =
normalizedNames.getOrPut(this) {
Normalizer.normalize(this, Normalizer.Form.NFD)
.replace(Regex("\\p{M}"), "")
}
}
Loading