diff --git a/app/src/main/java/com/ti/mobpo/ui/PokeSearchViewModel.kt b/app/src/main/java/com/ti/mobpo/ui/PokeSearchViewModel.kt index 8087606..f04ba50 100644 --- a/app/src/main/java/com/ti/mobpo/ui/PokeSearchViewModel.kt +++ b/app/src/main/java/com/ti/mobpo/ui/PokeSearchViewModel.kt @@ -6,10 +6,12 @@ import com.google.gson.annotations.SerializedName import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.asStateFlow +import kotlinx.coroutines.flow.forEach import kotlinx.coroutines.launch import retrofit2.Retrofit import retrofit2.converter.gson.GsonConverterFactory import retrofit2.http.GET +import retrofit2.http.Path import java.io.IOException private const val BASE_URL = "https://pokeapi.co/api/v2/"; @@ -23,9 +25,39 @@ data class PokemonResponse( @SerializedName("results") val results: List ) +data class PokemonDetails( + val id: Int, + val name: String, + val types: List, + @SerializedName("sprites") val sprites: Sprites +) + +data class Type( + @SerializedName("type") val typeName: TypeName +) + +data class TypeName( + @SerializedName("name") val name: String +) + +data class Sprites( + @SerializedName("other") val other: Other +) + +data class Other( + @SerializedName("official-artwork") val officialArtwork: OfficialArtwork +) + +data class OfficialArtwork( + @SerializedName("front_default") val frontDefault: String +) + interface PokeApiService { - @GET("pokemon/?offset=0&limit=1025") + @GET("pokemon/?offset=0&limit=2000") suspend fun getPokemon(): PokemonResponse + + @GET("pokemon/{id}") + suspend fun getPokemonDetails(@Path("id") id: Int): PokemonDetails } class PokeSearchViewModel : ViewModel() { @@ -38,6 +70,7 @@ class PokeSearchViewModel : ViewModel() { private val _initialPokemonList = MutableStateFlow?>(null) private val _filteredPokemonList = MutableStateFlow?>(null) + private val _pokemonDetails = MutableStateFlow?>(null) val pokemonList: StateFlow?> = _filteredPokemonList.asStateFlow() @@ -50,7 +83,6 @@ class PokeSearchViewModel : ViewModel() { try { val response = service.getPokemon() _initialPokemonList.value = response.results - _filteredPokemonList.value = listOf(PokemonSpecies("Please enter a search term", "")); } catch (e: IOException) { /*TODO*/ } @@ -62,8 +94,29 @@ class PokeSearchViewModel : ViewModel() { if (query.isNotBlank() && initialPokemonList != null) { val filteredList = initialPokemonList.filter { it.name.contains(query, ignoreCase = true) } _filteredPokemonList.value = filteredList.take(SHOW_LIMIT); - } else { - _filteredPokemonList.value = listOf(PokemonSpecies("Please enter a search term", "")); + fetchPokemonDetailsForFilteredPokemonList() } } + val pokemonDetails: StateFlow?> = _pokemonDetails.asStateFlow() + private fun fetchPokemonDetailsForFilteredPokemonList() { + val filteredPokemonList = _filteredPokemonList.value ?: return + + viewModelScope.launch { + try { + val detailsList = mutableListOf() + for (pokemonSpecies in filteredPokemonList) { + val details = service.getPokemonDetails(extractPokemonId(pokemonSpecies.url)) + detailsList.add(details) + } + _pokemonDetails.value = detailsList + } catch (e: IOException) { + /* Handle error */ + } + } + } + + private fun extractPokemonId(url: String): Int { + val parts = url.split("/") + return parts[parts.size - 2].toInt() + } } \ No newline at end of file diff --git a/app/src/main/java/com/ti/mobpo/ui/screens/PokeSearch.kt b/app/src/main/java/com/ti/mobpo/ui/screens/PokeSearch.kt index f7e3a02..c50fe64 100644 --- a/app/src/main/java/com/ti/mobpo/ui/screens/PokeSearch.kt +++ b/app/src/main/java/com/ti/mobpo/ui/screens/PokeSearch.kt @@ -27,7 +27,7 @@ fun PokeSearchScreen(pokeSearchViewModel: PokeSearchViewModel) { @Composable fun PokeSearch(pokeSearchViewModel: PokeSearchViewModel) { - val searchResults by pokeSearchViewModel.pokemonList.collectAsState() + val searchResults by pokeSearchViewModel.pokemonDetails.collectAsState() Column( verticalArrangement = Arrangement.Top,