Fully move api according to dev.android.com basics

This commit is contained in:
Joren 2024-04-29 17:33:47 +02:00
parent e7d53f67b0
commit 1f17e6066f
Signed by untrusted user who does not match committer: Joren
GPG Key ID: 280E33DFBC0F1B55
2 changed files with 18 additions and 7 deletions

View File

@ -2,9 +2,18 @@ package com.ti.mobpo.network
import com.ti.mobpo.data.PokemonDetails import com.ti.mobpo.data.PokemonDetails
import com.ti.mobpo.data.PokemonResponse import com.ti.mobpo.data.PokemonResponse
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import retrofit2.http.GET import retrofit2.http.GET
import retrofit2.http.Path import retrofit2.http.Path
private const val BASE_URL = "https://pokeapi.co/api/v2/"
private val retrofit = Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build()
interface PokeApiService { interface PokeApiService {
@GET("pokemon/?offset=0&limit=2000") @GET("pokemon/?offset=0&limit=2000")
suspend fun getPokemon(): PokemonResponse suspend fun getPokemon(): PokemonResponse
@ -12,3 +21,9 @@ interface PokeApiService {
@GET("pokemon/{id}") @GET("pokemon/{id}")
suspend fun getPokemonDetails(@Path("id") id: Int): PokemonDetails suspend fun getPokemonDetails(@Path("id") id: Int): PokemonDetails
} }
object PokeApi {
val retrofitService : PokeApiService by lazy {
retrofit.create(PokeApiService::class.java)
}
}

View File

@ -3,6 +3,7 @@ package com.ti.mobpo.ui
import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope import androidx.lifecycle.viewModelScope
import com.ti.mobpo.data.PokemonDetails import com.ti.mobpo.data.PokemonDetails
import com.ti.mobpo.network.PokeApi
import com.ti.mobpo.network.PokeApiService 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
@ -12,15 +13,10 @@ import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory import retrofit2.converter.gson.GsonConverterFactory
import java.io.IOException import java.io.IOException
private const val BASE_URL = "https://pokeapi.co/api/v2/"
class PokeSearchViewModel : ViewModel() { class PokeSearchViewModel : ViewModel() {
private val retrofit = Retrofit.Builder() private val service = PokeApi.retrofitService;
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build()
private val service = retrofit.create(PokeApiService::class.java)
private val _pokemonDetails = MutableStateFlow<List<PokemonDetails>?>(null) private val _pokemonDetails = MutableStateFlow<List<PokemonDetails>?>(null)
val pokemonDetails: StateFlow<List<PokemonDetails>?> = _pokemonDetails.asStateFlow() val pokemonDetails: StateFlow<List<PokemonDetails>?> = _pokemonDetails.asStateFlow()