Get details from api, instaid of displaying message just return empty list
This commit is contained in:
parent
f7bd501921
commit
e433bad681
@ -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<PokemonSpecies>
|
||||
)
|
||||
|
||||
data class PokemonDetails(
|
||||
val id: Int,
|
||||
val name: String,
|
||||
val types: List<Type>,
|
||||
@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<List<PokemonSpecies>?>(null)
|
||||
private val _filteredPokemonList = MutableStateFlow<List<PokemonSpecies>?>(null)
|
||||
private val _pokemonDetails = MutableStateFlow<List<PokemonDetails>?>(null)
|
||||
|
||||
val pokemonList: StateFlow<List<PokemonSpecies>?> = _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<List<PokemonDetails>?> = _pokemonDetails.asStateFlow()
|
||||
private fun fetchPokemonDetailsForFilteredPokemonList() {
|
||||
val filteredPokemonList = _filteredPokemonList.value ?: return
|
||||
|
||||
viewModelScope.launch {
|
||||
try {
|
||||
val detailsList = mutableListOf<PokemonDetails>()
|
||||
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()
|
||||
}
|
||||
}
|
@ -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,
|
||||
|
Loading…
x
Reference in New Issue
Block a user