Remove useless stuff
This commit is contained in:
		
							
								
								
									
										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.ViewModel
 | 
				
			||||||
import androidx.lifecycle.viewModelScope
 | 
					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.MutableStateFlow
 | 
				
			||||||
import kotlinx.coroutines.flow.StateFlow
 | 
					import kotlinx.coroutines.flow.StateFlow
 | 
				
			||||||
import kotlinx.coroutines.flow.asStateFlow
 | 
					import kotlinx.coroutines.flow.asStateFlow
 | 
				
			||||||
import kotlinx.coroutines.flow.forEach
 | 
					 | 
				
			||||||
import kotlinx.coroutines.launch
 | 
					import kotlinx.coroutines.launch
 | 
				
			||||||
import retrofit2.Retrofit
 | 
					import retrofit2.Retrofit
 | 
				
			||||||
import retrofit2.converter.gson.GsonConverterFactory
 | 
					import retrofit2.converter.gson.GsonConverterFactory
 | 
				
			||||||
import retrofit2.http.GET
 | 
					 | 
				
			||||||
import retrofit2.http.Path
 | 
					 | 
				
			||||||
import java.io.IOException
 | 
					import java.io.IOException
 | 
				
			||||||
 | 
					
 | 
				
			||||||
private const val BASE_URL = "https://pokeapi.co/api/v2/";
 | 
					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
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
class PokeSearchViewModel : ViewModel() {
 | 
					class PokeSearchViewModel : ViewModel() {
 | 
				
			||||||
    private val retrofit = Retrofit.Builder()
 | 
					    private val retrofit = Retrofit.Builder()
 | 
				
			||||||
@@ -68,43 +22,16 @@ class PokeSearchViewModel : ViewModel() {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    private val service = retrofit.create(PokeApiService::class.java)
 | 
					    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)
 | 
					    private val _pokemonDetails = MutableStateFlow<List<PokemonDetails>?>(null)
 | 
				
			||||||
 | 
					    val pokemonDetails: StateFlow<List<PokemonDetails>?> = _pokemonDetails.asStateFlow()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    val pokemonList: StateFlow<List<PokemonSpecies>?> = _filteredPokemonList.asStateFlow()
 | 
					    fun search(query: String) {
 | 
				
			||||||
 | 
					 | 
				
			||||||
    init {
 | 
					 | 
				
			||||||
        fetchPokemonSpecies()
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    private fun fetchPokemonSpecies() {
 | 
					 | 
				
			||||||
        viewModelScope.launch {
 | 
					        viewModelScope.launch {
 | 
				
			||||||
            try {
 | 
					            try {
 | 
				
			||||||
                val response = service.getPokemon()
 | 
					                val response = service.getPokemon()
 | 
				
			||||||
                _initialPokemonList.value = response.results
 | 
					                val filteredList = response.results.filter { it.name.contains(query, ignoreCase = true) }
 | 
				
			||||||
            } 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 detailsList = mutableListOf<PokemonDetails>()
 | 
					                val detailsList = mutableListOf<PokemonDetails>()
 | 
				
			||||||
                for (pokemonSpecies in filteredPokemonList) {
 | 
					                for (pokemonSpecies in filteredList) {
 | 
				
			||||||
                    val details = service.getPokemonDetails(extractPokemonId(pokemonSpecies.url))
 | 
					                    val details = service.getPokemonDetails(extractPokemonId(pokemonSpecies.url))
 | 
				
			||||||
                    detailsList.add(details)
 | 
					                    detailsList.add(details)
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
@@ -119,4 +46,4 @@ class PokeSearchViewModel : ViewModel() {
 | 
				
			|||||||
        val parts = url.split("/")
 | 
					        val parts = url.split("/")
 | 
				
			||||||
        return parts[parts.size - 2].toInt()
 | 
					        return parts[parts.size - 2].toInt()
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user