Fully move api according to dev.android.com basics
This commit is contained in:
parent
e7d53f67b0
commit
1f17e6066f
@ -2,9 +2,18 @@ package com.ti.mobpo.network
|
||||
|
||||
import com.ti.mobpo.data.PokemonDetails
|
||||
import com.ti.mobpo.data.PokemonResponse
|
||||
import retrofit2.Retrofit
|
||||
import retrofit2.converter.gson.GsonConverterFactory
|
||||
import retrofit2.http.GET
|
||||
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 {
|
||||
@GET("pokemon/?offset=0&limit=2000")
|
||||
suspend fun getPokemon(): PokemonResponse
|
||||
@ -12,3 +21,9 @@ interface PokeApiService {
|
||||
@GET("pokemon/{id}")
|
||||
suspend fun getPokemonDetails(@Path("id") id: Int): PokemonDetails
|
||||
}
|
||||
|
||||
object PokeApi {
|
||||
val retrofitService : PokeApiService by lazy {
|
||||
retrofit.create(PokeApiService::class.java)
|
||||
}
|
||||
}
|
||||
|
@ -3,6 +3,7 @@ package com.ti.mobpo.ui
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import com.ti.mobpo.data.PokemonDetails
|
||||
import com.ti.mobpo.network.PokeApi
|
||||
import com.ti.mobpo.network.PokeApiService
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
@ -12,15 +13,10 @@ import retrofit2.Retrofit
|
||||
import retrofit2.converter.gson.GsonConverterFactory
|
||||
import java.io.IOException
|
||||
|
||||
private const val BASE_URL = "https://pokeapi.co/api/v2/"
|
||||
|
||||
|
||||
class PokeSearchViewModel : ViewModel() {
|
||||
private val retrofit = Retrofit.Builder()
|
||||
.baseUrl(BASE_URL)
|
||||
.addConverterFactory(GsonConverterFactory.create())
|
||||
.build()
|
||||
|
||||
private val service = retrofit.create(PokeApiService::class.java)
|
||||
private val service = PokeApi.retrofitService;
|
||||
|
||||
private val _pokemonDetails = MutableStateFlow<List<PokemonDetails>?>(null)
|
||||
val pokemonDetails: StateFlow<List<PokemonDetails>?> = _pokemonDetails.asStateFlow()
|
||||
|
Loading…
x
Reference in New Issue
Block a user