editrecipe works

This commit is contained in:
LilleBRG 2025-05-15 11:47:59 +02:00
parent 571a258c18
commit 2088de994f
8 changed files with 90 additions and 88 deletions

View File

@ -21,6 +21,7 @@ android {
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
buildConfigField("String", "API_BASE_URL", project.property("API_BASE_URL").toString())
buildConfigField("String", "API_LOCALHOST_URL", project.property("API_LOCALHOST_URL").toString())
}

View File

@ -1 +1,2 @@
API_BASE_URL="https://easyeat.mercantec.tech"
API_LOCALHOST_URL="http://10.0.2.2:5000"

View File

@ -13,6 +13,7 @@
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.EasyEat"
android:networkSecurityConfig="@xml/network_security_config"
tools:targetApi="31">
<activity
android:name=".SplashActivity"

View File

@ -18,7 +18,7 @@ class ApiRequestException(message: String, cause: Throwable?) : Exception(messag
class HttpResponse(val body: String, val code: Int)
fun request(ctx: Context, method: String, path: String, data: String?, autoRefresh: Boolean = true): HttpResponse {
val url = URL(BuildConfig.API_BASE_URL + path)
val url = URL(BuildConfig.API_LOCALHOST_URL + path)
val prefs = ctx.getSharedPreferences("easyeat", Context.MODE_PRIVATE)
val authToken = prefs.getString("auth-token", null)

View File

@ -28,9 +28,6 @@ fun getAllRecipes(ctx: Context): List<GetAllRecipesResponse> {
return requestJson<Unit, List<GetAllRecipesResponse>>(ctx, "GET", "/api/Recipe/getall", null)
}
@Serializable
data class RecipeDetailsResponse(val recipe: Recipe)
fun getRecipeDetails(ctx: Context, id: Int): Recipe {
return requestJson<Unit, Recipe>(ctx, "GET", "/api/Recipe/get/$id", null)
}

View File

@ -18,20 +18,21 @@ import kotlin.concurrent.thread
import android.widget.EditText
import android.widget.ImageButton
import android.widget.TextView
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import androidx.core.widget.doAfterTextChanged
import tech.mercantec.easyeat.helpers.AddRecipeToShoppingList
import tech.mercantec.easyeat.models.Ingredient
import tech.mercantec.easyeat.models.Recipe
private lateinit var editRecipeLauncher: ActivityResultLauncher<Intent>
class DishDetailsActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_dish_details)
val ingredientsContainer = findViewById<LinearLayout>(R.id.ingredients)
val multiplierEditText = findViewById<EditText>(R.id.ingredient_multiplier)
val dishId = intent.getIntExtra("dish_id", -1)
if (dishId == -1) {
Toast.makeText(this, "No dish ID provided", Toast.LENGTH_SHORT).show()
@ -39,6 +40,50 @@ class DishDetailsActivity : AppCompatActivity() {
return
}
editRecipeLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
if (result.resultCode == Activity.RESULT_OK) {
loadRecipe(dishId)
}
}
findViewById<Button>(R.id.addDishToShoppingList).setOnClickListener {
val progressDialog = ProgressDialog(this)
progressDialog.setMessage("Loading...")
progressDialog.show()
thread {
try {
val multiplierText = findViewById<EditText>(R.id.ingredient_multiplier).text.toString()
AddRecipeToShoppingList(this, dishId, multiplierText)
} catch (e: ApiRequestException) {
runOnUiThread {
Toast.makeText(this, e.message, Toast.LENGTH_LONG).show()
}
return@thread
} finally {
runOnUiThread {
progressDialog.dismiss()
}
}
runOnUiThread {
Toast.makeText(this, "Recipe ingredients added to shopping", Toast.LENGTH_LONG).show()
setResult(Activity.RESULT_OK)
finish()
}
}
}
// Load the recipe
loadRecipe(dishId)
}
private fun loadRecipe(dishId: Int) {
val ingredientsContainer = findViewById<LinearLayout>(R.id.ingredients)
val multiplierEditText = findViewById<EditText>(R.id.ingredient_multiplier)
val instructionsLayout = findViewById<LinearLayout>(R.id.instructions)
val editRecipeBtn: Button = findViewById(R.id.editRecipeBtn)
thread {
val recipe: Recipe
try {
@ -49,15 +94,33 @@ class DishDetailsActivity : AppCompatActivity() {
}
return@thread
}
Log.i("DISH", recipe.ingredients.toString())
val instructionsLayout = findViewById<LinearLayout>(R.id.instructions)
// Example data: recipe.ingredients and recipe.directions
fun displayIngredients(ingredients: List<Ingredient>, multiplier: Int) {
runOnUiThread {
ingredientsContainer.removeAllViews()
for (ingredient in ingredients) {
val row = TextView(this)
val amount = ingredient.amount?.times(multiplier)
?.toBigDecimal()?.stripTrailingZeros()?.toPlainString()
val amountStr = if (amount != null) ": $amount" else ""
row.text = Html.fromHtml(
"&#8226; ${ingredient.name}$amountStr ${ingredient.unit ?: ""}",
Html.FROM_HTML_MODE_LEGACY
)
row.textSize = 18f
row.setPadding(0, 8, 0, 8)
ingredientsContainer.addView(row)
}
}
}
runOnUiThread {
findViewById<TextView>(R.id.dishDetailName).text = recipe.name
findViewById<TextView>(R.id.dishDetailDescription).text = recipe.description
instructionsLayout.removeAllViews()
// Populate Instructions (if directions are strings)
recipe.directions.forEachIndexed { index, direction ->
val textView = TextView(this).apply {
text = "${index + 1}. $direction"
@ -66,99 +129,37 @@ class DishDetailsActivity : AppCompatActivity() {
}
instructionsLayout.addView(textView)
}
}
fun displayIngredients(ingredients: List<Ingredient>, multiplier: Int, container: LinearLayout) {
container.removeAllViews() // clear previous views
for (ingredient in ingredients) {
val row = TextView(this)
val amount = ingredient.amount?.times(multiplier)?.toBigDecimal()?.stripTrailingZeros()?.toPlainString()
val amountStr = if (amount != null) ": $amount" else ""
row.text = Html.fromHtml("&#8226; ${ingredient.name}$amountStr ${ingredient.unit ?: ""}", Html.FROM_HTML_MODE_LEGACY)
row.textSize = 18f
row.setPadding(0, 8, 0, 8)
container.addView(row)
}
}
runOnUiThread {
val nameView = findViewById<TextView>(R.id.dishDetailName)
val descView = findViewById<TextView>(R.id.dishDetailDescription)
nameView.text = recipe.name
descView.text = recipe.description
// Default multiplier
var multiplier = 1
displayIngredients(recipe.ingredients, multiplier)
// Initial display
displayIngredients(recipe.ingredients, multiplier, ingredientsContainer)
// Listen for user input changes
multiplierEditText.doAfterTextChanged {
multiplier = it.toString().toIntOrNull() ?: 1
displayIngredients(recipe.ingredients, multiplier, ingredientsContainer)
displayIngredients(recipe.ingredients, multiplier)
}
findViewById<ImageButton>(R.id.increment_multiplier).setOnClickListener {
multiplier++
multiplierEditText.setText(multiplier.toString(), TextView.BufferType.EDITABLE)
displayIngredients(recipe.ingredients, multiplier, ingredientsContainer)
displayIngredients(recipe.ingredients, multiplier)
}
findViewById<ImageButton>(R.id.decrement_multiplier).setOnClickListener {
if (multiplier <= 1) return@setOnClickListener
multiplier--
multiplierEditText.setText(multiplier.toString(), TextView.BufferType.EDITABLE)
displayIngredients(recipe.ingredients, multiplier, ingredientsContainer)
if (multiplier > 1) {
multiplier--
multiplierEditText.setText(multiplier.toString(), TextView.BufferType.EDITABLE)
displayIngredients(recipe.ingredients, multiplier)
}
}
// You can do the same for directions if needed
}
val saveButton: Button = findViewById(R.id.addDishToShoppingList)
saveButton.setOnClickListener {
val progressDialog = ProgressDialog(this)
progressDialog.setMessage("Loading...")
progressDialog.show()
thread {
try {
val multiplierEditText = findViewById<EditText>(R.id.ingredient_multiplier)
val multiplierText = multiplierEditText.text.toString()
AddRecipeToShoppingList(this, dishId, multiplierText)
} catch (e: ApiRequestException) {
runOnUiThread {
Toast.makeText(this, e.message, Toast.LENGTH_LONG).show()
}
return@thread
} finally {
runOnUiThread {
progressDialog.hide()
}
}
runOnUiThread {
Toast.makeText(this, "Recipe ingredients added to shopping", Toast.LENGTH_LONG).show()
}
setResult(Activity.RESULT_OK)
finish()
editRecipeBtn.setOnClickListener {
val jsonRecipe = Json.encodeToString(recipe)
val intent = Intent(this, EditDishActivity::class.java)
intent.putExtra("recipe_json", jsonRecipe)
editRecipeLauncher.launch(intent)
}
}
val editRecipeBtn: Button = findViewById(R.id.editRecipeBtn)
editRecipeBtn.setOnClickListener {
val jsonRecipe = Json.encodeToString(recipe) // convert Recipe to JSON string
Log.i("jsonrecipe", jsonRecipe)
val intent = Intent(this, EditDishActivity::class.java)
intent.putExtra("recipe_json", jsonRecipe) // put as string
startActivity(intent)
}
}
}
}

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<domain-config cleartextTrafficPermitted="true">
<domain includeSubdomains="true">localhost</domain>
<domain includeSubdomains="true">10.0.2.2</domain>
</domain-config>
</network-security-config>

View File

@ -120,6 +120,7 @@ namespace API.BusinessLogic
dish.Name = recipe.Name;
dish.Description = recipe.Description;
dish.Directions = new List<Directions>();
dish.Ingredients = new List<Ingredient>();
foreach (var item in recipe.Ingredients)
{
Ingredient ingredient = new Ingredient();