Implement adding items to shopping list

This commit is contained in:
Reimar 2025-05-07 12:04:41 +02:00
parent 050ff896e1
commit f9296f05c1
Signed by: Reimar
GPG Key ID: 93549FA07F0AE268
4 changed files with 57 additions and 5 deletions

View File

@ -0,0 +1,20 @@
package tech.mercantec.easyeat.helpers
import android.content.Context
import kotlinx.serialization.Serializable
@Serializable
data class ShoppingListItem(val id: Int, val name: String, val amount: Double?, val unit: String?, val checked: Boolean)
fun getShoppingList(ctx: Context): Array<ShoppingListItem> {
return requestJson<Unit, Array<ShoppingListItem>>(ctx, "GET", "/api/ShoppingList/get", null)
}
@Serializable
data class AddShoppingItemRequest(val name: String, val amount: Double?, val unit: String?, val checked: Boolean)
fun addShoppingItem(ctx: Context, name: String, amount: Double?, unit: String?) {
val request = AddShoppingItemRequest(name, amount, unit, false)
requestJson<AddShoppingItemRequest, Boolean>(ctx, "POST", "/api/ShoppingList/add", request)
}

View File

@ -1,15 +1,21 @@
package tech.mercantec.easyeat.ui.shopping_list
import android.app.AlertDialog
import android.app.Dialog
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ArrayAdapter
import android.widget.EditText
import android.widget.Spinner
import android.widget.Toast
import androidx.fragment.app.Fragment
import tech.mercantec.easyeat.R
import tech.mercantec.easyeat.databinding.FragmentShoppingListBinding
import tech.mercantec.easyeat.helpers.ApiRequestException
import tech.mercantec.easyeat.helpers.addShoppingItem
import kotlin.concurrent.thread
class ShoppingListFragment : Fragment() {
@ -32,12 +38,33 @@ class ShoppingListFragment : Fragment() {
val dialog = AlertDialog.Builder(activity)
.setView(view)
.setPositiveButton(R.string.add_label, { dialog, id ->
.setPositiveButton(R.string.add_label) { dialog, id ->
val dialog = dialog as AlertDialog
})
.setNegativeButton(R.string.cancel_label, { dialog, id ->
val amount = view.findViewById<EditText>(R.id.amount).text.toString().toDouble()
val unit = view.findViewById<Spinner>(R.id.unit_selector).selectedItem.toString().ifEmpty { null }
val name = view.findViewById<EditText>(R.id.name).text.toString()
dialog.getButton(AlertDialog.BUTTON_POSITIVE).isEnabled = false
dialog.getButton(AlertDialog.BUTTON_NEGATIVE).isEnabled = false
thread {
try {
addShoppingItem(requireContext(), name, amount, unit)
} catch (e: ApiRequestException) {
activity?.runOnUiThread {
Toast.makeText(context, e.message, Toast.LENGTH_LONG).show()
}
} finally {
activity?.runOnUiThread {
dialog.dismiss()
}
}
}
}
.setNegativeButton(R.string.cancel_label) { dialog, _ ->
dialog.cancel()
})
}
.create()
dialog.show()

View File

@ -30,12 +30,14 @@
android:layout_height="wrap_content"
android:layout_below="@id/amount_label"
android:layout_alignParentStart="true"
android:inputType="numberDecimal"
android:importantForAutofill="no"
android:hint="@string/ingredient_amount_hint"
/>
<Spinner
android:id="@+id/unit_selector"
android:layout_width="72dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toEndOf="@id/amount"
android:layout_below="@+id/amount_label"
@ -57,6 +59,8 @@
android:layout_below="@id/name_label"
android:layout_toEndOf="@id/unit_selector"
android:layout_alignParentEnd="true"
android:inputType="text"
android:importantForAutofill="no"
android:hint="@string/ingredient_name_hint"
/>

View File

@ -39,6 +39,7 @@
<string name="change_password_label">Change password</string>
<string name="logout_label">Log out</string>
<string-array name="units">
<item></item>
<item>g</item>
<item>kg</item>
<item>ml</item>