Skip to content

Commit

Permalink
Issue
Browse files Browse the repository at this point in the history
  • Loading branch information
Rohanraj123 committed Apr 27, 2024
1 parent 2f91c58 commit d6f2c6a
Show file tree
Hide file tree
Showing 21 changed files with 172 additions and 154 deletions.
6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ dependencies {
// Retrofit
implementation(libs.retrofit)
implementation(libs.retrofit.gson.converter)
implementation(libs.okhttp3)

// Compose dependencies
implementation(libs.viewmodel.compose)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,19 @@ import retrofit2.Call
import retrofit2.http.GET
import retrofit2.http.Query

/**
* Interface defining the API endpoints for the CookBook application.
*/
interface RetrofitApi {
@GET("recipes/random")

/**
* Fetches a random recipe from the Spoonacular API
*
* @param apiKey The API key to use for authentication.
* @return A Call object containing the asynchronous response data.
*/
@GET("random")
fun getRandomRecipe(
@Query("api_key") apiKey: String,
@Query("number") number: Int
@Query("api_key") apiKey: String
): Call<RandomRecipeResponse>
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.example.cookbook.data.models.randomrecipemodel

import com.google.gson.annotations.SerializedName

data class AnalyzedInstruction(
@SerializedName("name" ) var name : String? = null,
@SerializedName("steps") var steps : ArrayList<Step> = arrayListOf()
)
val name: String,
val steps: List<Step>
)
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package com.example.cookbook.data.models.randomrecipemodel

import com.google.gson.annotations.SerializedName

data class Equipment(
@SerializedName("id" ) val id : Int,
@SerializedName("name" ) val name : String,
@SerializedName("localizedName") val localizedName : String,
@SerializedName("image" ) val image : String
)
val id: Int,
val image: String,
val localizedName: String,
val name: String,
val temperature: Temperature
)
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
package com.example.cookbook.data.models.randomrecipemodel

import com.google.gson.annotations.SerializedName

data class ExtendedIngredient(
@SerializedName("id" ) var id : Int? = null,
@SerializedName("aisle" ) var aisle : String? = null,
@SerializedName("image" ) var image : String? = null,
@SerializedName("consistency" ) var consistency : String? = null,
@SerializedName("name" ) var name : String? = null,
@SerializedName("nameClean" ) var nameClean : String? = null,
@SerializedName("original" ) var original : String? = null,
@SerializedName("originalName") var originalName : Int? = null,
@SerializedName("amount" ) var amount : Double? = null,
@SerializedName("unit" ) var unit : String? = null,
@SerializedName("meta" ) var meta : ArrayList<String>? = null,
@SerializedName("measures" ) var measures : Measures? = null
)
val aisle: String,
val amount: Double,
val consistency: String,
val id: Int,
val image: String,
val measures: Measures,
val meta: List<String>,
val name: String,
val nameClean: String,
val original: String,
val originalName: String,
val unit: String
)
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package com.example.cookbook.data.models.randomrecipemodel

import com.google.gson.annotations.SerializedName

data class Ingredient(
@SerializedName("id" ) var id : Int? = null,
@SerializedName("name" ) var name : String? = null,
@SerializedName("localizedName") var localizedName : String? = null,
@SerializedName("image" ) var image : String? = null,
)
val id: Int,
val image: String,
val localizedName: String,
val name: String
)
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.example.cookbook.data.models.randomrecipemodel

import com.google.gson.annotations.SerializedName

data class Length(
@SerializedName("number") val number: Int,
@SerializedName("unit" ) val unit : String
)
val number: Int,
val unit: String
)

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.example.cookbook.data.models.randomrecipemodel

import com.google.gson.annotations.SerializedName

data class Measures(
@SerializedName("us" ) var us : Us,
@SerializedName("metric") var metric : Measure
)
val metric: Metric,
val us: Us
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.example.cookbook.data.models.randomrecipemodel

data class Metric(
val amount: Double,
val unitLong: String,
val unitShort: String
)
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.example.cookbook.data.models.randomrecipemodel

import com.google.gson.annotations.SerializedName

data class RandomRecipeResponse(
@SerializedName("recipes") var recipes: ArrayList<Recipe> = arrayListOf()
)
val recipes: List<Recipe>
)
Original file line number Diff line number Diff line change
@@ -1,43 +1,41 @@
package com.example.cookbook.data.models.randomrecipemodel

import com.google.gson.annotations.SerializedName

data class Recipe(
@SerializedName("vegetarian" ) var vegetarian : Boolean? = null,
@SerializedName("vegan" ) var vegan : Boolean? = null,
@SerializedName("glutenFree" ) var glutenFree : Boolean? = null,
@SerializedName("dairyFree" ) var dairyFree : Boolean? = null,
@SerializedName("veryHealthy" ) var veryHealthy : Boolean? = null,
@SerializedName("cheap" ) var cheap : Boolean? = null,
@SerializedName("veryPopular" ) var veryPopular : Boolean? = null,
@SerializedName("sustainable" ) var sustainable : Boolean? = null,
@SerializedName("lowFodmap" ) var lowFodmap : Boolean? = null,
@SerializedName("weightWatcherSmartPoints" ) var weightWatcherSmartPoints : Int? = null,
@SerializedName("gaps" ) var gaps : String? = null,
@SerializedName("preparationMinutes" ) var preparationMinutes : Int? = null,
@SerializedName("cookingMinutes" ) var cookingMinutes : Int? = null,
@SerializedName("aggregateLikes" ) var aggregateLikes : Int? = null,
@SerializedName("healthScore" ) var healthScore : Int? = null,
@SerializedName("creditsText" ) var creditsText : String? = null,
@SerializedName("sourceName" ) var sourceName : String? = null,
@SerializedName("pricePerServing" ) var pricePerServing : Double? = null,
@SerializedName("extendedIngredients" ) var extendedIngredients : ArrayList<ExtendedIngredient> = arrayListOf(),
@SerializedName("id" ) var id : Int? = null,
@SerializedName("title" ) var title : String? = null,
@SerializedName("readyInMinutes" ) var readyInMinutes : Int? = null,
@SerializedName("servings" ) var servings : Int? = null,
@SerializedName("sourceUrl" ) var sourceUrl : String? = null,
@SerializedName("image" ) var image : String? = null,
@SerializedName("imageType" ) var imageType : String? = null,
@SerializedName("summary" ) var summary : String? = null,
@SerializedName("cuisines" ) var cuisines : ArrayList<String> = arrayListOf(),
@SerializedName("dishTypes" ) var dishTypes : ArrayList<String> = arrayListOf(),
@SerializedName("diets" ) var diets : ArrayList<String> = arrayListOf(),
@SerializedName("occasions" ) var occasions : ArrayList<String> = arrayListOf(),
@SerializedName("instructions" ) var instructions : String? = null,
@SerializedName("analyzedInstructions" ) var analyzedInstructions : ArrayList<AnalyzedInstruction> = arrayListOf(),
@SerializedName("originalId" ) var originalId : String? = null,
@SerializedName("spoonacularScore" ) var spoonacularScore : Double? = null,
@SerializedName("spoonacularSourceUrl" ) var spoonacularSourceUrl : String? = null

val aggregateLikes: Int,
val analyzedInstructions: List<AnalyzedInstruction>,
val cheap: Boolean,
val cookingMinutes: Int,
val creditsText: String,
val cuisines: List<String>,
val dairyFree: Boolean,
val diets: List<String>,
val dishTypes: List<String>,
val extendedIngredients: List<ExtendedIngredient>,
val gaps: String,
val glutenFree: Boolean,
val healthScore: Int,
val id: Int,
val image: String,
val imageType: String,
val instructions: String,
val license: String,
val lowFodmap: Boolean,
val occasions: List<Any>,
val originalId: Any,
val preparationMinutes: Int,
val pricePerServing: Double,
val readyInMinutes: Int,
val servings: Int,
val sourceName: String,
val sourceUrl: String,
val spoonacularScore: Double,
val spoonacularSourceUrl: String,
val summary: String,
val sustainable: Boolean,
val title: String,
val vegan: Boolean,
val vegetarian: Boolean,
val veryHealthy: Boolean,
val veryPopular: Boolean,
val weightWatcherSmartPoints: Int
)
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package com.example.cookbook.data.models.randomrecipemodel

import com.google.gson.annotations.SerializedName

data class Step(
@SerializedName("number" ) var number : Int? = null,
@SerializedName("step" ) var step : String? = null,
@SerializedName("ingredients") var ingredients : ArrayList<String>? = arrayListOf(),
@SerializedName("equipment" ) var equipment : ArrayList<Ingredient>? = arrayListOf(),
@SerializedName("length" ) var length : Length?
)
val equipment: List<Equipment>,
val ingredients: List<Ingredient>,
val length: Length,
val number: Int,
val step: String
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.example.cookbook.data.models.randomrecipemodel

data class Temperature(
val number: Double,
val unit: String
)
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package com.example.cookbook.data.models.randomrecipemodel

import com.google.gson.annotations.SerializedName

data class Us(
@SerializedName("amount" ) var amount : Double? = null,
@SerializedName("unitShort") var unitShort : String? = null,
@SerializedName("unitLong" ) var unitLong : String? = null
)
val amount: Double,
val unitLong: String,
val unitShort: String
)
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package com.example.cookbook.data.reposiitory

import com.example.cookbook.data.models.randomrecipemodel.RandomRecipeResponse
import retrofit2.Callback

interface RandomRecipeRepository {
suspend fun getRandomRecipe(apiKey: String, number: Int) : Result<RandomRecipeResponse>
suspend fun getRandomRecipe(apiKey: String) : Result<RandomRecipeResponse>
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,20 @@ package com.example.cookbook.data.reposiitory

import com.example.cookbook.data.datasource.api.RetrofitApi
import com.example.cookbook.data.models.randomrecipemodel.RandomRecipeResponse
import com.example.cookbook.data.models.randomrecipemodel.Recipe
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import retrofit2.await
import retrofit2.awaitResponse
import java.io.IOException

class RandomRecipeRepositoryImpl(
private val retrofitApi: RetrofitApi
) : RandomRecipeRepository {
override suspend fun getRandomRecipe(apiKey: String, number: Int): Result<RandomRecipeResponse> {
/*
override suspend fun getRandomRecipe(apiKey: String): Result<RandomRecipeResponse> {
return try {
val response = retrofitApi.getRandomRecipe(apiKey, number).awaitResponse()
val response = retrofitApi.getRandomRecipe(apiKey).awaitResponse()
Log.d("RepoImpl", "getRandomRecipe is called of retrofitApi")
Log.d("RepoImpl", "Request Parameters: apiKey - $apiKey")
Log.d("RepoImpl", "response : ${response.body()}")
if (response.isSuccessful) {
Log.d("ResponseStatus", "response status is : ${response.message()}")
val data = response.body()
if (data != null) {
Result.success(data)
Expand All @@ -27,7 +26,33 @@ class RandomRecipeRepositoryImpl(
Result.failure(Exception("Failed to fetch the network data with status code: ${response.code()}"))
}
} catch (e: Exception) {
e.printStackTrace()
Log.e("RandomRecipeRepository", "Exception occurred: ${e.message}")
Result.failure(e)
} catch (ioException: IOException) {
ioException.printStackTrace()
Log.e("RandomRecipeRepository", "Exception occurred: ${ioException.message}")
Result.failure(ioException)
} catch (httpException: HttpException) {
httpException.printStackTrace()
Log.e("RandomRecipeRepository", "Exception occurred: ${httpException.message}")
Result.failure(httpException)
}
}
*/
override suspend fun getRandomRecipe(apiKey: String): Result<RandomRecipeResponse> {
return try {
val response = retrofitApi.getRandomRecipe(apiKey).awaitResponse() // Await the asynchronous response
if (response.isSuccessful) {
Result.success(response.body()!!)
} else {
Result.failure(Exception("Failed to fetch recipe (code: ${response.code()})")) // Include error code
}
} catch (e: Exception) {
Result.failure(e) // Handle other exceptions
}
}


}
Loading

0 comments on commit d6f2c6a

Please sign in to comment.