Skip to content

Commit

Permalink
Merge pull request #1 from Rohanraj123/Development-day6
Browse files Browse the repository at this point in the history
Development day6
  • Loading branch information
Rohanraj123 authored May 4, 2024
2 parents d6f2c6a + c05844d commit e123b18
Show file tree
Hide file tree
Showing 46 changed files with 950 additions and 275 deletions.
1 change: 1 addition & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ dependencies {
implementation(libs.sharedpreferences)
implementation(libs.lifecycle.compose)
implementation(libs.runtime.livedata)
implementation(libs.swipe.refresh)

// Dagger-hilt
implementation("com.google.dagger:hilt-android:2.48")
Expand Down
7 changes: 3 additions & 4 deletions app/src/main/java/com/example/cookbook/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@ import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavHostController
import androidx.navigation.compose.rememberNavController
import com.example.cookbook.presentation.view.navigation.Navigation
import com.example.cookbook.presentation.viewmodel.HomeScreenViewModel
import com.example.cookbook.presentation.viewmodel.LogInScreenViewModel
import com.example.cookbook.presentation.viewmodel.RecipeDetailScreenViewModel
import com.example.cookbook.presentation.viewmodel.RegisterScreenViewModel
import com.example.cookbook.ui.theme.CookBookTheme
import dagger.hilt.android.AndroidEntryPoint
Expand All @@ -28,16 +26,17 @@ class MainActivity : ComponentActivity() {
val registerScreenViewModel = hiltViewModel<RegisterScreenViewModel>()
val logInScreenViewModel = hiltViewModel<LogInScreenViewModel>()
val homeScreenViewModel = hiltViewModel<HomeScreenViewModel>()
val recipeDetailScreenViewModel = hiltViewModel<RecipeDetailScreenViewModel>()

logInScreenViewModel.setNavController(navController)

Navigation(
navController = navController,
registerScreenViewModel = registerScreenViewModel,
logInScreenViewModel = logInScreenViewModel,
recipeDetailScreenViewModel,
homeScreenViewModel
)

}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ interface RetrofitApi {
*/
@GET("random")
fun getRandomRecipe(
@Query("api_key") apiKey: String
@Query("apiKey") apiKey: String,
@Query("number") number: Int
): Call<RandomRecipeResponse>

@GET("complexSearch")
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.example.cookbook.data.models.randomrecipemodel

import com.google.gson.annotations.SerializedName

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

import com.google.gson.annotations.SerializedName

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

import com.google.gson.annotations.SerializedName

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

import com.google.gson.annotations.SerializedName

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

import com.google.gson.annotations.SerializedName

data class Length(
val number: Int,
val unit: String
@SerializedName("number" ) val number : Int,
@SerializedName("unit" ) val unit : String
)
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.example.cookbook.data.models.randomrecipemodel

import com.google.gson.annotations.SerializedName

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

import com.google.gson.annotations.SerializedName

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

import com.google.gson.annotations.SerializedName

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

import com.google.gson.Gson
import com.google.gson.annotations.SerializedName

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

import com.google.gson.annotations.SerializedName

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

import com.google.gson.annotations.SerializedName

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

import com.google.gson.annotations.SerializedName

data class Us(
val amount: Double,
val unitLong: String,
val unitShort: String
@SerializedName("amount" ) val amount : Double,
@SerializedName("unitLong" ) val unitLong : String,
@SerializedName("unitShort" ) val unitShort : String
)
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ package com.example.cookbook.data.reposiitory
import com.example.cookbook.data.models.randomrecipemodel.RandomRecipeResponse

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

import com.example.cookbook.data.datasource.api.RetrofitApi
import com.example.cookbook.data.models.randomrecipemodel.RandomRecipeResponse
import retrofit2.HttpException
import retrofit2.awaitResponse
import java.io.IOException

class RandomRecipeRepositoryImpl(
private val retrofitApi: RetrofitApi
) : RandomRecipeRepository {
/*
override suspend fun getRandomRecipe(apiKey: String): Result<RandomRecipeResponse> {
override suspend fun getRandomRecipe(apiKey: String, number: Int):
Result<RandomRecipeResponse> {
return try {
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()}")
val response = retrofitApi
.getRandomRecipe(apiKey, number)
.awaitResponse()

if (response.isSuccessful) {
Log.d("ResponseStatus", "response status is : ${response.message()}")
val data = response.body()
if (data != null) {
Result.success(data)
} else {
Result.failure(Exception("Response body is null"))
}
} else {
Result.failure(Exception("Failed to fetch the network data with status code: ${response.code()}"))
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 e123b18

Please sign in to comment.