Allow writing allergies when generating recipe

This commit is contained in:
Reimar 2025-05-15 15:05:39 +02:00
parent 4856bf5403
commit 9c364b2493
Signed by: Reimar
GPG Key ID: 93549FA07F0AE268
5 changed files with 43 additions and 6 deletions

View File

@ -3,6 +3,7 @@ package tech.mercantec.easyeat.helpers
import android.content.Context
import android.content.Intent
import android.util.Log
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.Serializable
import kotlinx.serialization.SerializationException
import tech.mercantec.easyeat.BuildConfig
@ -65,6 +66,9 @@ fun request(ctx: Context, method: String, path: String, data: String?, autoRefre
@Serializable
class HttpErrorResponse(val message: String)
@OptIn(ExperimentalSerializationApi::class)
val json = Json { explicitNulls = false }
inline fun <reified Req, reified Res> requestJson(ctx: Context, method: String, path: String, data: Req?): Res {
val requestJson =
if (data != null)
@ -96,7 +100,7 @@ inline fun <reified Req, reified Res> requestJson(ctx: Context, method: String,
}
try {
return Json.decodeFromString<Res>(response.body)
return json.decodeFromString<Res>(response.body)
} catch (e: SerializationException) {
if (e.message != null)
Log.e("EasyEat", e.message!!)

View File

@ -7,10 +7,10 @@ import tech.mercantec.easyeat.models.Ingredient
import tech.mercantec.easyeat.models.Recipe
@Serializable
data class GenerateRecipeRequest(val dish: String, val language: String, val numberOfRecipes: Int, val allergi: Array<String>)
data class GenerateRecipeRequest(val dish: String, val language: String, val numberOfRecipes: Int, val allergi: List<String>)
fun generateRecipeWithAI(ctx: Context, title: String, language: String): Recipe {
return requestJson<GenerateRecipeRequest, Recipe>(ctx, "POST", "/api/Recipe/chatbot", GenerateRecipeRequest(title, language, 1, arrayOf()))
fun generateRecipeWithAI(ctx: Context, title: String, language: String, allergies: List<String>): Recipe {
return requestJson<GenerateRecipeRequest, Recipe>(ctx, "POST", "/api/Recipe/chatbot", GenerateRecipeRequest(title, language, 1, allergies))
}
fun createRecipe(ctx: Context, recipe: Recipe) {

View File

@ -3,7 +3,9 @@ package tech.mercantec.easyeat.ui.dishes
import android.app.Activity
import android.app.ProgressDialog
import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.CheckBox
import android.widget.EditText
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
@ -19,8 +21,19 @@ class GenerateRecipeActivity : AppCompatActivity() {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_generate_recipe)
val allergiesInput = findViewById<EditText>(R.id.allergies)
findViewById<CheckBox>(R.id.has_allergies).setOnCheckedChangeListener { _, checked ->
allergiesInput.visibility = if (checked) View.VISIBLE else View.GONE
}
findViewById<Button>(R.id.generate).setOnClickListener {
val name = findViewById<EditText>(R.id.dish_title).text.toString()
val allergies =
if (findViewById<CheckBox>(R.id.has_allergies).isChecked)
allergiesInput.text.split(Regex(",\\s*"))
else
listOf()
val progressDialog = ProgressDialog(this)
progressDialog.setMessage("Generating...")
@ -28,7 +41,7 @@ class GenerateRecipeActivity : AppCompatActivity() {
thread {
try {
val recipe = generateRecipeWithAI(this, name, Locale.getDefault().displayLanguage)
val recipe = generateRecipeWithAI(this, name, Locale.getDefault().displayLanguage, allergies)
runOnUiThread {
progressDialog.setMessage("Saving...")

View File

@ -31,9 +31,27 @@
android:inputType="text"
/>
<CheckBox
android:id="@+id/has_allergies"
android:layout_marginTop="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/allergies_label"
/>
<EditText
android:id="@+id/allergies"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/allergies_hint"
android:importantForAutofill="no"
android:inputType="text"
android:visibility="gone"
/>
<Button
android:id="@+id/generate"
android:layout_marginTop="20sp"
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/generate_recipe_label"

View File

@ -58,6 +58,8 @@
<string name="decrement_multiplier_desc">Decrement portion size</string>
<string name="edit_recipe">Edit Recipe</string>
<string name="empty_dishes_list">You have not created any dishes yet</string>
<string name="allergies_label">Allergies</string>
<string name="allergies_hint">Lactose, Gluten</string>
<string-array name="units">
<item></item>
<item>g</item>