Fetch pokemon
This commit is contained in:
parent
5ff9afaa87
commit
57cc5c827b
@ -66,4 +66,6 @@ dependencies {
|
||||
debugImplementation(libs.androidx.ui.test.manifest)
|
||||
implementation("androidx.navigation:navigation-compose:$nav_version")
|
||||
implementation("androidx.lifecycle:lifecycle-viewmodel-compose:2.6.1")
|
||||
implementation("com.squareup.retrofit2:retrofit:2.9.0")
|
||||
implementation("com.squareup.retrofit2:converter-gson:2.9.0")
|
||||
}
|
@ -1,6 +1,7 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:tools="http://schemas.android.com/tools">
|
||||
<uses-permission android:name="android.permission.INTERNET" />
|
||||
|
||||
<application
|
||||
android:allowBackup="true"
|
||||
|
@ -1,11 +1,60 @@
|
||||
package com.ti.mobpo.ui
|
||||
|
||||
import androidx.lifecycle.ViewModel
|
||||
import com.google.gson.annotations.SerializedName
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
import kotlinx.coroutines.flow.asStateFlow
|
||||
import retrofit2.Call
|
||||
import retrofit2.Retrofit
|
||||
import retrofit2.converter.gson.GsonConverterFactory
|
||||
import retrofit2.http.GET
|
||||
|
||||
// Data class to hold Pokemon species information
|
||||
data class PokemonSpecies(
|
||||
@SerializedName("name") val name: String,
|
||||
@SerializedName("url") val url: String
|
||||
)
|
||||
|
||||
// Data class to hold the response from the Pokemon species API
|
||||
data class PokemonSpeciesResponse(
|
||||
@SerializedName("results") val results: List<PokemonSpecies>
|
||||
)
|
||||
|
||||
interface PokeApiService {
|
||||
@GET("pokemon-species/?offset=0&limit=1025")
|
||||
fun getPokemonSpecies(): Call<PokemonSpeciesResponse>
|
||||
}
|
||||
|
||||
class PokeSearchViewModel : ViewModel() {
|
||||
private val retrofit = Retrofit.Builder()
|
||||
.baseUrl("https://pokeapi.co/api/v2/")
|
||||
.addConverterFactory(GsonConverterFactory.create())
|
||||
.build()
|
||||
|
||||
private val service = retrofit.create(PokeApiService::class.java)
|
||||
|
||||
private val _uiState = MutableStateFlow(PokeSearchUiState())
|
||||
val uiState: StateFlow<PokeSearchUiState> = _uiState.asStateFlow()
|
||||
|
||||
}
|
||||
private var pokemonList: List<PokemonSpecies>? = null
|
||||
|
||||
init {
|
||||
fetchPokemonSpecies()
|
||||
}
|
||||
|
||||
private fun fetchPokemonSpecies() {
|
||||
service.getPokemonSpecies().enqueue(object : retrofit2.Callback<PokemonSpeciesResponse> {
|
||||
override fun onResponse(call: Call<PokemonSpeciesResponse>, response: retrofit2.Response<PokemonSpeciesResponse>) {
|
||||
if (response.isSuccessful) {
|
||||
val speciesResponse = response.body()
|
||||
pokemonList = speciesResponse?.results
|
||||
}
|
||||
}
|
||||
|
||||
override fun onFailure(call: Call<PokemonSpeciesResponse>, t: Throwable) {
|
||||
/*TODO*/
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user