Add buttons for incrementing/decrementing portion size, fix decimal numbers

This commit is contained in:
Reimar 2025-05-14 08:40:36 +02:00
parent 48b3622542
commit 6e72f549cf
Signed by: Reimar
GPG Key ID: 93549FA07F0AE268
7 changed files with 73 additions and 10 deletions

View File

@ -17,6 +17,7 @@ import tech.mercantec.easyeat.helpers.RecipeDetailsResponse
import tech.mercantec.easyeat.helpers.getRecipeDetails
import kotlin.concurrent.thread
import android.widget.EditText
import android.widget.ImageButton
import android.widget.TextView
import androidx.core.widget.doAfterTextChanged
import tech.mercantec.easyeat.helpers.AddRecipeToShoppingList
@ -28,7 +29,7 @@ class DishDetailsActivity : AppCompatActivity() {
setContentView(R.layout.activity_dish_details)
val ingredientsContainer = findViewById<LinearLayout>(R.id.ingredients)
val multiplierEditText = findViewById<EditText>(R.id.ingredientMultiplier)
val multiplierEditText = findViewById<EditText>(R.id.ingredient_multiplier)
val dishId = intent.getIntExtra("dish_id", -1)
if (dishId == -1) {
@ -37,8 +38,6 @@ class DishDetailsActivity : AppCompatActivity() {
return
}
thread {
val recipe: RecipeDetailsResponse
try {
@ -74,7 +73,9 @@ class DishDetailsActivity : AppCompatActivity() {
for (ingredient in ingredients) {
val row = TextView(this)
val amount = (ingredient.amount ?: 0.0) * multiplier
row.text = Html.fromHtml("&#8226; ${ingredient.name}: ${"%.2f".format(amount)} ${ingredient.unit ?: ""}", Html.FROM_HTML_MODE_LEGACY)
val amountStr = amount.toBigDecimal().stripTrailingZeros().toPlainString()
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)
@ -100,19 +101,33 @@ class DishDetailsActivity : AppCompatActivity() {
displayIngredients(recipe.ingredients, multiplier, ingredientsContainer)
}
findViewById<ImageButton>(R.id.increment_multiplier).setOnClickListener {
multiplier++
multiplierEditText.setText(multiplier.toString(), TextView.BufferType.EDITABLE)
displayIngredients(recipe.ingredients, multiplier, ingredientsContainer)
}
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)
}
// 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.ingredientMultiplier)
val multiplierEditText = findViewById<EditText>(R.id.ingredient_multiplier)
val multiplierText = multiplierEditText.text.toString()
AddRecipeToShoppingList(this, dishId, multiplierText.toIntOrNull() ?: 1)
} catch (e: ApiRequestException) {

View File

@ -222,7 +222,7 @@ class ShoppingListFragment : Fragment() {
// Pre-fill dialog inputs with current item if applicable
item?.let { item ->
view.findViewById<EditText>(R.id.amount).setText(item.amount?.toBigDecimal()?.stripTrailingZeros().toString(), TextView.BufferType.EDITABLE)
view.findViewById<EditText>(R.id.amount).setText(item.amount?.toBigDecimal()?.stripTrailingZeros()?.toPlainString(), TextView.BufferType.EDITABLE)
view.findViewById<Spinner>(R.id.unit_selector).setSelection(adapter.getPosition(item.unit))
view.findViewById<EditText>(R.id.name).setText(item.name, TextView.BufferType.EDITABLE)

View File

@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="960"
android:viewportHeight="960"
android:tint="?attr/colorControlNormal">
<path
android:fillColor="@android:color/white"
android:pathData="M480,600L280,400L680,400L480,600Z"/>
</vector>

View File

@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="960"
android:viewportHeight="960"
android:tint="?attr/colorControlNormal">
<path
android:fillColor="@android:color/white"
android:pathData="M280,560L480,360L680,560L280,560Z"/>
</vector>

View File

@ -39,15 +39,41 @@
android:layout_height="match_parent"
android:textSize="16sp"
android:paddingTop="8dp"
android:labelFor="@id/ingredient_multiplier"
android:text="@string/portions_label"
/>
<EditText
android:id="@+id/ingredientMultiplier"
android:id="@+id/ingredient_multiplier"
android:layout_marginStart="20dp"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:importantForAutofill="no"
android:inputType="number"
android:text="1"
/>
<ImageButton
android:id="@+id/decrement_multiplier"
android:layout_marginStart="20dp"
android:layout_width="32dp"
android:layout_height="40dp"
android:scaleType="fitXY"
android:src="@drawable/ic_arrow_drop_down_24px"
android:contentDescription="@string/decrement_multiplier_desc"
android:background="?android:attr/selectableItemBackgroundBorderless"
/>
<ImageButton
android:id="@+id/increment_multiplier"
android:layout_marginStart="5dp"
android:layout_width="32dp"
android:layout_height="40dp"
android:scaleType="fitXY"
android:src="@drawable/ic_arrow_drop_up_24px"
android:contentDescription="@string/increment_multiplier_desc"
android:background="?android:attr/selectableItemBackgroundBorderless"
/>
</LinearLayout>

View File

@ -52,7 +52,7 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/password_label"
/>
/>
<EditText
android:id="@+id/password"

View File

@ -54,6 +54,8 @@
<string name="create_ai_label">Generate recipe using AI</string>
<string name="portions_label">Portions</string>
<string name="instructions_label">Instructions</string>
<string name="increment_multiplier_desc">Increment portion count</string>
<string name="decrement_multiplier_desc">Decrement portion size</string>
<string-array name="units">
<item></item>
<item>g</item>