use buildin functions instaid
This commit is contained in:
parent
7b36f08a26
commit
8c69116171
@ -14,9 +14,9 @@ import kotlinx.coroutines.launch
|
|||||||
import java.io.IOException
|
import java.io.IOException
|
||||||
|
|
||||||
|
|
||||||
private const val SHOW_LIMIT = 20;
|
private const val SHOW_LIMIT = 20
|
||||||
class PokeSearchViewModel(private val favouritesRepository: FavouritesRepository) : ViewModel() {
|
class PokeSearchViewModel(private val favouritesRepository: FavouritesRepository) : ViewModel() {
|
||||||
private val service = PokeApi.retrofitService;
|
private val service = PokeApi.retrofitService
|
||||||
|
|
||||||
private val _pokemonDetails = MutableStateFlow<List<PokemonDetails>?>(null)
|
private val _pokemonDetails = MutableStateFlow<List<PokemonDetails>?>(null)
|
||||||
val pokemonDetails: StateFlow<List<PokemonDetails>?> = _pokemonDetails.asStateFlow()
|
val pokemonDetails: StateFlow<List<PokemonDetails>?> = _pokemonDetails.asStateFlow()
|
||||||
@ -43,10 +43,16 @@ class PokeSearchViewModel(private val favouritesRepository: FavouritesRepository
|
|||||||
_isLoading.value = true
|
_isLoading.value = true
|
||||||
viewModelScope.launch {
|
viewModelScope.launch {
|
||||||
try {
|
try {
|
||||||
val filteredList = _initialPokemonList.value?.binarySearch(query)
|
val firstIndex = _initialPokemonList.value?.indexOfFirst { it.name.startsWith(query, ignoreCase = true) }
|
||||||
|
val lastIndex = _initialPokemonList.value?.indexOfLast { it.name.startsWith(query, ignoreCase = true) }
|
||||||
|
|
||||||
val detailsList = mutableListOf<PokemonDetails>()
|
val detailsList = mutableListOf<PokemonDetails>()
|
||||||
if (filteredList != null) {
|
|
||||||
for (index in filteredList until minOf(_initialPokemonList.value?.size ?: 0, filteredList + SHOW_LIMIT)) {
|
if (firstIndex != null && lastIndex != null) {
|
||||||
|
val endIndex = minOf(firstIndex + SHOW_LIMIT, lastIndex + 1)
|
||||||
|
val startIndex = maxOf(firstIndex, endIndex - SHOW_LIMIT)
|
||||||
|
|
||||||
|
for (index in startIndex until endIndex) {
|
||||||
val pokemonSpecies = _initialPokemonList.value!![index]
|
val pokemonSpecies = _initialPokemonList.value!![index]
|
||||||
val details = service.getPokemonDetails(extractPokemonId(pokemonSpecies.url))
|
val details = service.getPokemonDetails(extractPokemonId(pokemonSpecies.url))
|
||||||
val isFavorite = favouritesRepository.isFavourite(details.id)
|
val isFavorite = favouritesRepository.isFavourite(details.id)
|
||||||
@ -62,21 +68,6 @@ class PokeSearchViewModel(private val favouritesRepository: FavouritesRepository
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun List<PokemonSpecies>.binarySearch(query: String): Int {
|
|
||||||
var low = 0
|
|
||||||
var high = this.size - 1
|
|
||||||
while (low <= high) {
|
|
||||||
val mid = (low + high) ushr 1
|
|
||||||
val comparison = this[mid].name.compareTo(query, ignoreCase = true)
|
|
||||||
when {
|
|
||||||
comparison == 0 -> return mid
|
|
||||||
comparison < 0 -> low = mid + 1
|
|
||||||
else -> high = mid - 1
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return low
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun extractPokemonId(url: String): Int {
|
private fun extractPokemonId(url: String): Int {
|
||||||
val parts = url.split("/")
|
val parts = url.split("/")
|
||||||
return parts[parts.size - 2].toInt()
|
return parts[parts.size - 2].toInt()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user