Remove useless stuff
This commit is contained in:
parent
e433bad681
commit
d82cedd526
39
app/src/main/java/com/ti/mobpo/data/PokeModels.kt
Normal file
39
app/src/main/java/com/ti/mobpo/data/PokeModels.kt
Normal file
@ -0,0 +1,39 @@
|
||||
package com.ti.mobpo.data
|
||||
|
||||
import com.google.gson.annotations.SerializedName
|
||||
|
||||
data class PokemonSpecies(
|
||||
@SerializedName("name") val name: String,
|
||||
@SerializedName("url") val url: String
|
||||
)
|
||||
|
||||
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
|
||||
)
|
@ -2,63 +2,17 @@ package com.ti.mobpo.ui
|
||||
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import com.google.gson.annotations.SerializedName
|
||||
import com.ti.mobpo.data.PokemonDetails
|
||||
import com.ti.mobpo.network.PokeApiService
|
||||
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/";
|
||||
private const val SHOW_LIMIT = 20;
|
||||
data class PokemonSpecies(
|
||||
@SerializedName("name") val name: String,
|
||||
@SerializedName("url") val url: String
|
||||
)
|
||||
|
||||
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=2000")
|
||||
suspend fun getPokemon(): PokemonResponse
|
||||
|
||||
@GET("pokemon/{id}")
|
||||
suspend fun getPokemonDetails(@Path("id") id: Int): PokemonDetails
|
||||
}
|
||||
private const val BASE_URL = "https://pokeapi.co/api/v2/"
|
||||
|
||||
class PokeSearchViewModel : ViewModel() {
|
||||
private val retrofit = Retrofit.Builder()
|
||||
@ -68,43 +22,16 @@ class PokeSearchViewModel : ViewModel() {
|
||||
|
||||
private val service = retrofit.create(PokeApiService::class.java)
|
||||
|
||||
private val _initialPokemonList = MutableStateFlow<List<PokemonSpecies>?>(null)
|
||||
private val _filteredPokemonList = MutableStateFlow<List<PokemonSpecies>?>(null)
|
||||
private val _pokemonDetails = MutableStateFlow<List<PokemonDetails>?>(null)
|
||||
val pokemonDetails: StateFlow<List<PokemonDetails>?> = _pokemonDetails.asStateFlow()
|
||||
|
||||
val pokemonList: StateFlow<List<PokemonSpecies>?> = _filteredPokemonList.asStateFlow()
|
||||
|
||||
init {
|
||||
fetchPokemonSpecies()
|
||||
}
|
||||
|
||||
private fun fetchPokemonSpecies() {
|
||||
fun search(query: String) {
|
||||
viewModelScope.launch {
|
||||
try {
|
||||
val response = service.getPokemon()
|
||||
_initialPokemonList.value = response.results
|
||||
} catch (e: IOException) {
|
||||
/*TODO*/
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun search(query: String) {
|
||||
val initialPokemonList = _initialPokemonList.value
|
||||
if (query.isNotBlank() && initialPokemonList != null) {
|
||||
val filteredList = initialPokemonList.filter { it.name.contains(query, ignoreCase = true) }
|
||||
_filteredPokemonList.value = filteredList.take(SHOW_LIMIT);
|
||||
fetchPokemonDetailsForFilteredPokemonList()
|
||||
}
|
||||
}
|
||||
val pokemonDetails: StateFlow<List<PokemonDetails>?> = _pokemonDetails.asStateFlow()
|
||||
private fun fetchPokemonDetailsForFilteredPokemonList() {
|
||||
val filteredPokemonList = _filteredPokemonList.value ?: return
|
||||
|
||||
viewModelScope.launch {
|
||||
try {
|
||||
val filteredList = response.results.filter { it.name.contains(query, ignoreCase = true) }
|
||||
val detailsList = mutableListOf<PokemonDetails>()
|
||||
for (pokemonSpecies in filteredPokemonList) {
|
||||
for (pokemonSpecies in filteredList) {
|
||||
val details = service.getPokemonDetails(extractPokemonId(pokemonSpecies.url))
|
||||
detailsList.add(details)
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user