Skip to content

Commit

Permalink
Punk api is dead. Replace it with rick and morty api
Browse files Browse the repository at this point in the history
  • Loading branch information
Drjacky committed Jun 16, 2024
1 parent a42ee9a commit ce8d0fa
Show file tree
Hide file tree
Showing 12 changed files with 88 additions and 65 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ Available workflows listed as follows:

## References 🧷

- [The Punk API](https://punkapi.com/)
- [Rick and Morty API](https://rickandmortyapi.com/)
- [Right or Left animation by Marco Martina on LottieFiles](https://lottiefiles.com/21141-right-or-left)
- [Loading Beer animation by Hashim Irfan on LottieFiles](https://lottiefiles.com/30697-loading-beer-animation)

Expand Down
4 changes: 2 additions & 2 deletions data/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@ android {
buildConfigField(
"String",
"BASE_URL",
"\"" + "https://api.punkapi.com/v2/" + "\""
"\"" + "https://rickandmortyapi.com/api/" + "\""
)
}
named("release") {
isMinifyEnabled = false
buildConfigField(
"String",
"BASE_URL",
"\"" + "https://api.punkapi.com/v2/" + "\""
"\"" + "https://rickandmortyapi.com/api/" + "\""
)
setProguardFiles(
listOf(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class ProductsPagingSource @Inject constructor(
.map { response ->
when (response) {
is NetworkResponse.Success -> {
val list = response.body.map(BeerResponse::mapIt)
val list = response.body.results.map(BeerResponse::mapIt)

toLoadResult(list, position)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class ProductsPagingSourceByCoroutine @Inject constructor(
val position = params.key ?: STARTING_PAGE_INDEX

return try {
val response = productsApi.getBeersListByCoroutine(position)
val response = productsApi.getBeersListByCoroutine(position).results
.map(BeerResponse::mapIt)

toLoadResult(response, position)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,36 @@ package app.web.drjackycv.data.products.entity
import app.web.drjackycv.domain.products.entity.Beer
import com.google.gson.annotations.SerializedName

data class CharactersResponse(
val info: Info,
val results: List<BeerResponse>
) {
data class Info(
val count: Int,
val pages: Int,
val next: String?,
val prev: String?,
)
}

data class BeerResponse(
@SerializedName("id") val id: Int,
@SerializedName("name") val name: String,
@SerializedName("tagline") val tagline: String,
@SerializedName("description") val description: String,
@SerializedName("image_url") val imageUrl: String?,
@SerializedName("abv") val abv: Double,
@SerializedName("status") val status: String,
@SerializedName("species") val species: String,
@SerializedName("gender") val gender: String,
@SerializedName("image") val image: String,
@SerializedName("url") val url: String
)


fun BeerResponse.mapIt(): Beer =
Beer(
id = id,
name = name,
tagline = tagline,
description = description,
imageUrl = imageUrl,
abv = abv
status = status,
species = species,
gender = gender,
image = image,
url = url,
)
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,34 @@ package app.web.drjackycv.data.products.remote
import app.web.drjackycv.data.network.BaseApiService
import app.web.drjackycv.data.network.GenericNetworkResponse
import app.web.drjackycv.data.products.entity.BeerResponse
import app.web.drjackycv.data.products.entity.CharactersResponse
import io.reactivex.rxjava3.core.Single
import retrofit2.http.GET
import retrofit2.http.Path
import retrofit2.http.Query

interface ProductsApi : BaseApiService {

@GET("beers")
@GET("character")
fun getBeersList(
@Query("page") page: Int = 1,
@Query("per_page") perPage: Int = 40,
): Single<GenericNetworkResponse<List<BeerResponse>>>
): Single<GenericNetworkResponse<CharactersResponse>>

@GET("beers/{beer_id}")
@GET("character/{beer_id}")
fun getBeer(
@Path("beer_id") id: String?,
): Single<GenericNetworkResponse<List<BeerResponse>>>
): Single<GenericNetworkResponse<BeerResponse>>

@GET("beers")
@GET("character")
suspend fun getBeersListByCoroutine(
@Query("page") page: Int = 1,
@Query("per_page") perPage: Int = 40,
): List<BeerResponse>
): CharactersResponse

@GET("beers/{beer_id}")
@GET("character/{beer_id}")
suspend fun getBeerByCoroutine(
@Path("beer_id") id: String?,
): List<BeerResponse>
): BeerResponse

}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class ProductsListRepositoryImpl @Inject constructor(
override fun getBeer(id: String): Flowable<Beer> { //TODO: Use Rx
return flow {
val response = productsApi.getBeerByCoroutine(id)
val result = response.first().mapIt()
val result = response.mapIt()

emit(result)
}.asFlowable()
Expand All @@ -67,7 +67,7 @@ class ProductsListRepositoryImpl @Inject constructor(
override fun getBeerByCoroutine(id: String): Flow<Beer> = //TODO: Handle exception
flow {
val response = productsApi.getBeerByCoroutine(id)
val result = response.first().mapIt()
val result = response.mapIt()

emit(result)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ package app.web.drjackycv.domain.products.entity
data class Beer(
val id: Int,
val name: String,
val tagline: String,
val description: String,
val imageUrl: String?,
val abv: Double,
val status: String,
val species: String,
val gender: String,
val image: String,
val url: String
)
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,20 @@ import kotlinx.parcelize.Parcelize
data class BeerUI(
val id: Int,
val name: String,
val tagline: String,
val description: String,
val imageUrl: String?,
val abv: Double,
val status: String,
val species: String,
val gender: String,
val image: String,
val url: String
) : Parcelable

fun Beer.mapIt(): BeerUI =
BeerUI(
id = id,
name = name,
tagline = tagline,
description = description,
imageUrl = imageUrl,
abv = abv
status = status,
species = species,
gender = gender,
image = image,
url = url,
)
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ fun ProductDetailView(
) {
Text(
modifier = Modifier.padding(16.dp),
text = "${uiState.item?.description}",
text = "${uiState.item?.species}",
style = TextStyle(fontSize = 17.sp),
overflow = TextOverflow.Ellipsis,
maxLines = 1
Expand Down Expand Up @@ -138,12 +138,13 @@ fun ProductDetailView(
private fun ProductDetailViewPreview() {
val uiState = ProductUiState.Success(
item = BeerUI(
id = 1,
name = "name",
tagline = "tagline",
description = "description",
imageUrl = "https://images.punkapi.com/v2/5.png",
abv = 4.9
id = 361,
name = "Toxic Rick",
status = "Dead",
species = "Humanoid",
gender = "Male",
image = "https://rickandmortyapi.com/api/character/avatar/361.jpeg",
url = "https://rickandmortyapi.com/api/character/361",
)
)
ProductDetailView(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ fun ProductRowView(
) {
val imagePainter = rememberAsyncImagePainter(
ImageRequest.Builder(LocalContext.current)
.data(data = product.imageUrl)
.data(data = product.image)
.crossfade(true)
.allowHardware(false)
.build()
Expand Down Expand Up @@ -187,12 +187,13 @@ private fun changeCardColor(
private fun ProductRowViewPreview() {
ProductRowView(
product = BeerUI(
id = 1,
name = "name",
tagline = "tagline",
description = "description",
imageUrl = "https://images.punkapi.com/v2/5.png",
abv = 4.9
id = 361,
name = "Toxic Rick",
status = "Dead",
species = "Humanoid",
gender = "Male",
image = "https://rickandmortyapi.com/api/character/avatar/361.jpeg",
url = "https://rickandmortyapi.com/api/character/361",
),
isShimmerVisible = false,
navigateToProduct = {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,11 @@ fun ProductsListContent(
val beerUI = BeerUI(
id = -1,
name = "",
tagline = "",
description = "",
imageUrl = "",
abv = 0.0
status = "",
species = "",
gender = "",
image = "",
url = "",
)
items(10) {
ProductRowView(
Expand Down Expand Up @@ -217,20 +218,22 @@ fun ProductsListContent(
private fun ProductsListContentPreview() {
val items = listOf(
BeerUI(
id = 1,
name = "name",
tagline = "tagline",
description = "description",
imageUrl = "https://images.punkapi.com/v2/5.png",
abv = 4.9
id = 361,
name = "Toxic Rick",
status = "Dead",
species = "Humanoid",
gender = "Male",
image = "https://rickandmortyapi.com/api/character/avatar/361.jpeg",
url = "https://rickandmortyapi.com/api/character/361",
),
BeerUI(
id = 2,
name = "name",
tagline = "tagline",
description = "description",
imageUrl = "https://images.punkapi.com/v2/5.png",
abv = 4.9
id = 362,
name = "Traflorkian",
status = "Alive",
species = "Alien",
gender = "unknown",
image = "https://rickandmortyapi.com/api/character/avatar/362.jpeg",
url = "https://rickandmortyapi.com/api/character/362",
)
)
val uiState = ProductsUiState.Success(items = PagingData.from(items))
Expand Down

0 comments on commit ce8d0fa

Please sign in to comment.