From 00cc8101f1b9d551302627e5f17b0138893260de Mon Sep 17 00:00:00 2001 From: LilleBRG Date: Tue, 13 May 2025 15:11:57 +0200 Subject: [PATCH] need to merge but trying to add recipe to shoppinglist --- .../tech/mercantec/easyeat/helpers/auth.kt | 15 ++- .../mercantec/easyeat/models/CreateRecipe.kt | 8 +- .../easyeat/ui/dishes/CreateDishAIActivity.kt | 4 +- .../easyeat/ui/dishes/CreateDishActivity.kt | 1 - .../easyeat/ui/dishes/DishDetailsActivity.kt | 116 ++++++++++++++++-- .../easyeat/ui/dishes/InstructionsAdapter.kt | 7 +- .../activity_create_dish_ingredient_row.xml | 4 +- .../main/res/layout/activity_dish_details.xml | 54 ++++++-- app/app/src/main/res/values/strings.xml | 1 + 9 files changed, 174 insertions(+), 36 deletions(-) diff --git a/app/app/src/main/java/tech/mercantec/easyeat/helpers/auth.kt b/app/app/src/main/java/tech/mercantec/easyeat/helpers/auth.kt index 6ce46f1..f152195 100644 --- a/app/app/src/main/java/tech/mercantec/easyeat/helpers/auth.kt +++ b/app/app/src/main/java/tech/mercantec/easyeat/helpers/auth.kt @@ -3,10 +3,8 @@ package tech.mercantec.easyeat.helpers import android.content.Context import android.util.Log import kotlinx.serialization.Serializable -import tech.mercantec.easyeat.models.CreateDirection import tech.mercantec.easyeat.models.CreateIngredient import tech.mercantec.easyeat.models.CreateRecipe -import tech.mercantec.easyeat.models.Direction import tech.mercantec.easyeat.models.Ingredient @Serializable @@ -116,7 +114,7 @@ fun changePassword(ctx: Context, oldPassword: String, newPassword: String) { } @Serializable -data class CreateRecipeRequest(val name: String, val description: String, val directions: List, val ingredients: List) +data class CreateRecipeRequest(val name: String, val description: String, val directions: List, val ingredients: List) fun createRecipe(ctx: Context, recipe: CreateRecipe) { val request = CreateRecipeRequest(recipe.name, recipe.description, recipe.directions, recipe.ingredients) @@ -132,8 +130,17 @@ fun getAllRecipies(ctx: Context): List { } @Serializable -data class RecipeDetailsResponse(val id: Int, val name: String, val description: String, val directions: List, val ingredients: List) +data class RecipeDetailsResponse(val id: Int, val name: String, val description: String, val directions: List, val ingredients: List) fun getRecipeDetails(ctx: Context, id: Int): RecipeDetailsResponse { return requestJson(ctx, "GET", "/api/Recipe/get/$id", null) } + +@Serializable +data class ShoppingListAddRecipeRequest(val id: Int, val multiplier: Int) + +fun AddRecipeToShoppingList(ctx: Context, id: Int, multiplier: Int) { + val request = ShoppingListAddRecipeRequest(id, multiplier) + + return requestJson(ctx, "ADD", "/api/ShoppingList/recipeadd", request) +} diff --git a/app/app/src/main/java/tech/mercantec/easyeat/models/CreateRecipe.kt b/app/app/src/main/java/tech/mercantec/easyeat/models/CreateRecipe.kt index 782f449..e7661bb 100644 --- a/app/app/src/main/java/tech/mercantec/easyeat/models/CreateRecipe.kt +++ b/app/app/src/main/java/tech/mercantec/easyeat/models/CreateRecipe.kt @@ -6,16 +6,10 @@ import kotlinx.serialization.Serializable data class CreateRecipe( val name: String, val description: String, - val directions: List, + val directions: List, val ingredients: List ) -@Serializable -data class CreateDirection( - val instructions: String -) - - @Serializable data class CreateIngredient( val amount: Double?, diff --git a/app/app/src/main/java/tech/mercantec/easyeat/ui/dishes/CreateDishAIActivity.kt b/app/app/src/main/java/tech/mercantec/easyeat/ui/dishes/CreateDishAIActivity.kt index fcf4bdc..6b46c5e 100644 --- a/app/app/src/main/java/tech/mercantec/easyeat/ui/dishes/CreateDishAIActivity.kt +++ b/app/app/src/main/java/tech/mercantec/easyeat/ui/dishes/CreateDishAIActivity.kt @@ -9,8 +9,8 @@ import android.widget.Toast import androidx.appcompat.app.AppCompatActivity import tech.mercantec.easyeat.R import tech.mercantec.easyeat.helpers.ApiRequestException -import tech.mercantec.easyeat.helpers.GenerateRecipeResponse import tech.mercantec.easyeat.helpers.generateRecipeWithAI +import tech.mercantec.easyeat.models.Recipe import java.util.Locale import kotlin.concurrent.thread @@ -27,7 +27,7 @@ class CreateDishAIActivity : AppCompatActivity() { progressDialog.show() thread { - val response: GenerateRecipeResponse + val response: Recipe try { response = generateRecipeWithAI(this, name, Locale.getDefault().displayLanguage) } catch (e: ApiRequestException) { diff --git a/app/app/src/main/java/tech/mercantec/easyeat/ui/dishes/CreateDishActivity.kt b/app/app/src/main/java/tech/mercantec/easyeat/ui/dishes/CreateDishActivity.kt index 2ece5a7..e5b72a9 100644 --- a/app/app/src/main/java/tech/mercantec/easyeat/ui/dishes/CreateDishActivity.kt +++ b/app/app/src/main/java/tech/mercantec/easyeat/ui/dishes/CreateDishActivity.kt @@ -20,7 +20,6 @@ import tech.mercantec.easyeat.helpers.ApiRequestException import tech.mercantec.easyeat.helpers.changePassword import tech.mercantec.easyeat.helpers.createRecipe import tech.mercantec.easyeat.helpers.request -import tech.mercantec.easyeat.models.CreateDirection import tech.mercantec.easyeat.models.CreateIngredient import tech.mercantec.easyeat.models.CreateRecipe import kotlin.concurrent.thread diff --git a/app/app/src/main/java/tech/mercantec/easyeat/ui/dishes/DishDetailsActivity.kt b/app/app/src/main/java/tech/mercantec/easyeat/ui/dishes/DishDetailsActivity.kt index 3f41774..a87bc46 100644 --- a/app/app/src/main/java/tech/mercantec/easyeat/ui/dishes/DishDetailsActivity.kt +++ b/app/app/src/main/java/tech/mercantec/easyeat/ui/dishes/DishDetailsActivity.kt @@ -1,7 +1,12 @@ package tech.mercantec.easyeat.ui.dishes +import android.app.Activity +import android.app.ProgressDialog import android.os.Bundle import android.util.Log +import android.view.View +import android.widget.Button +import android.widget.LinearLayout import android.widget.Toast import androidx.appcompat.app.AppCompatActivity import tech.mercantec.easyeat.R @@ -9,14 +14,22 @@ import tech.mercantec.easyeat.helpers.ApiRequestException import tech.mercantec.easyeat.helpers.RecipeDetailsResponse import tech.mercantec.easyeat.helpers.getRecipeDetails import kotlin.concurrent.thread -import android.widget.ListView +import android.widget.EditText import android.widget.TextView +import androidx.core.widget.doAfterTextChanged +import tech.mercantec.easyeat.helpers.AddRecipeToShoppingList +import tech.mercantec.easyeat.helpers.createRecipe +import tech.mercantec.easyeat.models.CreateRecipe +import tech.mercantec.easyeat.models.Ingredient class DishDetailsActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_dish_details) + val ingredientsContainer = findViewById(R.id.dishDetailIngredients) + val multiplierEditText = findViewById(R.id.ingredientMultiplier) + val dishId = intent.getIntExtra("dish_id", -1) if (dishId == -1) { Toast.makeText(this, "No dish ID provided", Toast.LENGTH_SHORT).show() @@ -24,6 +37,8 @@ class DishDetailsActivity : AppCompatActivity() { return } + + thread { val recipe: RecipeDetailsResponse try { @@ -35,20 +50,101 @@ class DishDetailsActivity : AppCompatActivity() { return@thread } Log.i("DISH", recipe.ingredients.toString()) + val ingredientsLayout = findViewById(R.id.dishDetailIngredients) + val instructionsLayout = findViewById(R.id.dishDetailInstructions) + +// Example data: recipe.ingredients and recipe.directions runOnUiThread { - // Set title and description findViewById(R.id.dishDetailName).text = recipe.name findViewById(R.id.dishDetailDescription).text = recipe.description + // Populate Ingredients + recipe.ingredients.forEach { ingredient -> + val textView = TextView(this).apply { + text = "${ingredient.name} ${ingredient.amount ?: ""} ${ingredient.unit ?: ""}" + textSize = 18f + } + textView.textAlignment = View.TEXT_ALIGNMENT_CENTER + ingredientsLayout.addView(textView) + } - // Set up the ingredient list - val ingredientListView = findViewById(R.id.dishDetailIngredients) - val ingredientAdapter = IngredientAdapter(this, recipe.ingredients) - ingredientListView.adapter = ingredientAdapter - - val instructionsListView = findViewById(R.id.dishDetailInstructions) - val instructionsAdapter = InstructionsAdapter(this, recipe.directions) - instructionsListView.adapter = instructionsAdapter + // Populate Instructions (if directions are strings) + recipe.directions.forEachIndexed { index, direction -> + val textView = TextView(this).apply { + text = "${index + 1}. $direction" + textSize = 18f + } + textView.textAlignment = View.TEXT_ALIGNMENT_CENTER + instructionsLayout.addView(textView) + } } + + fun displayIngredients(ingredients: List, multiplier: Int, container: LinearLayout) { + container.removeAllViews() // clear previous views + + for (ingredient in ingredients) { + val row = TextView(this) + val amount = (ingredient.amount ?: 0.0) * multiplier + row.text = "${ingredient.name}: ${"%.2f".format(amount)} ${ingredient.unit ?: ""}" + row.textSize = 18f + row.setPadding(0, 8, 0, 8) + row.textAlignment = View.TEXT_ALIGNMENT_CENTER + container.addView(row) + } + } + + runOnUiThread { + val nameView = findViewById(R.id.dishDetailName) + val descView = findViewById(R.id.dishDetailDescription) + + nameView.text = recipe.name + descView.text = recipe.description + + // Default multiplier + var multiplier = 1 + + // Initial display + displayIngredients(recipe.ingredients, multiplier, ingredientsContainer) + + // Listen for user input changes + multiplierEditText.doAfterTextChanged { + multiplier = it.toString().toIntOrNull() ?: 1 + displayIngredients(recipe.ingredients, multiplier, ingredientsContainer) + } + + // You can do the same for directions if needed + } + + val saveButton: Button = findViewById(R.id.saveDishButton) + saveButton.setOnClickListener { + + + val progressDialog = ProgressDialog(this) + progressDialog.setMessage("Loading...") + progressDialog.show() + thread { + try { + AddRecipeToShoppingList(this, dishId, multiplierEditText.text.toString().toIntOrNull() ?: 1) + } catch (e: ApiRequestException) { + runOnUiThread { + Toast.makeText(this, e.message, Toast.LENGTH_LONG).show() + } + + return@thread + } finally { + runOnUiThread { + progressDialog.hide() + } + } + + runOnUiThread { + Toast.makeText(this, "Password changed successfully", Toast.LENGTH_LONG).show() + } + + setResult(Activity.RESULT_OK) + finish() + } + } + } } } \ No newline at end of file diff --git a/app/app/src/main/java/tech/mercantec/easyeat/ui/dishes/InstructionsAdapter.kt b/app/app/src/main/java/tech/mercantec/easyeat/ui/dishes/InstructionsAdapter.kt index e0a2fee..161f58c 100644 --- a/app/app/src/main/java/tech/mercantec/easyeat/ui/dishes/InstructionsAdapter.kt +++ b/app/app/src/main/java/tech/mercantec/easyeat/ui/dishes/InstructionsAdapter.kt @@ -7,10 +7,9 @@ import android.view.ViewGroup import android.widget.ArrayAdapter import android.widget.TextView import tech.mercantec.easyeat.R -import tech.mercantec.easyeat.models.Direction -class InstructionsAdapter (context: Context, instructions: List) - : ArrayAdapter(context, 0, instructions) { +class InstructionsAdapter (context: Context, instructions: List) + : ArrayAdapter(context, 0, instructions) { override fun getView(position: Int, convertView: View?, parent: ViewGroup): View { val ingredient = getItem(position) @@ -19,7 +18,7 @@ class InstructionsAdapter (context: Context, instructions: List) val nameView = view.findViewById(R.id.instructionText) - nameView.text = ingredient?.instruktions ?: "" + nameView.text = ingredient?: "" return view } diff --git a/app/app/src/main/res/layout/activity_create_dish_ingredient_row.xml b/app/app/src/main/res/layout/activity_create_dish_ingredient_row.xml index c9d590b..6d2a736 100644 --- a/app/app/src/main/res/layout/activity_create_dish_ingredient_row.xml +++ b/app/app/src/main/res/layout/activity_create_dish_ingredient_row.xml @@ -43,7 +43,9 @@ + android:layout_height="48dp" + android:inputType="numberDecimal" + /> diff --git a/app/app/src/main/res/layout/activity_dish_details.xml b/app/app/src/main/res/layout/activity_dish_details.xml index 584e952..d431ab4 100644 --- a/app/app/src/main/res/layout/activity_dish_details.xml +++ b/app/app/src/main/res/layout/activity_dish_details.xml @@ -19,13 +19,16 @@ android:textSize="30sp" android:textAlignment="center" android:layout_marginBottom="10dp" - android:textStyle="bold" /> + android:textStyle="bold" + android:layout_marginHorizontal="10sp"/> + /> + android:textAlignment="center" + android:layout_marginHorizontal="10sp"/> - + + + + + + + android:orientation="vertical" + android:layout_marginHorizontal="10sp"> + + + - + android:orientation="vertical" + android:layout_marginHorizontal="10sp"/> + +